blob: 3b4f2c460dca7189c9883816f2e36b5c1f9d11f0 [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 Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
Bram Moolenaar314f11d2010-08-09 22:07:08 +020023#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
Bram Moolenaar33570922005-01-25 22:26:29 +000031#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000033#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
34 be freed. */
35
Bram Moolenaar071d4272004-06-13 20:20:40 +000036/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000037 * Structure returned by get_lval() and used by set_var_lval().
38 * For a plain name:
39 * "name" points to the variable name.
40 * "exp_name" is NULL.
41 * "tv" is NULL
42 * For a magic braces name:
43 * "name" points to the expanded variable name.
44 * "exp_name" is non-NULL, to be freed later.
45 * "tv" is NULL
46 * For an index in a list:
47 * "name" points to the (expanded) variable name.
48 * "exp_name" NULL or non-NULL, to be freed later.
49 * "tv" points to the (first) list item value
50 * "li" points to the (first) list item
51 * "range", "n1", "n2" and "empty2" indicate what items are used.
52 * For an existing Dict item:
53 * "name" points to the (expanded) variable name.
54 * "exp_name" NULL or non-NULL, to be freed later.
55 * "tv" points to the dict item value
56 * "newkey" is NULL
57 * For a non-existing Dict item:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000060 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000061 * "newkey" is the key for the new item.
62 */
63typedef struct lval_S
64{
65 char_u *ll_name; /* start of variable name (can be NULL) */
66 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000067 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000068 isn't NULL it's the Dict to which to add
69 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000070 listitem_T *ll_li; /* The list item or NULL. */
71 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 int ll_range; /* TRUE when a [i:j] range was used */
73 long ll_n1; /* First index for list */
74 long ll_n2; /* Second index for list range */
75 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000076 dict_T *ll_dict; /* The Dictionary or NULL */
77 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000078 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000079} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000081static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000082static char *e_undefvar = N_("E121: Undefined variable: %s");
83static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000084static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000085static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000086static char *e_listreq = N_("E714: List required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +020087#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020088static char *e_stringreq = N_("E928: String required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +020089#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +000090static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000091static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
92static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
93static char *e_funcdict = N_("E717: Dictionary entry already exists");
94static char *e_funcref = N_("E718: Funcref required");
95static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
96static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +000097static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +000098static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020099#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200100static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200101#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000102
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100103#define NAMESPACE_CHAR (char_u *)"abglstvw"
104
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200105static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000106#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
108/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000109 * Old Vim variables such as "v:version" are also available without the "v:".
110 * Also in functions. We need a special hashtable for them.
111 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000112static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000113
114/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000115 * When recursively copying lists and dicts we need to remember which ones we
116 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000117 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000118 */
119static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +0200120
Bram Moolenaard9fba312005-06-26 22:34:35 +0000121/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000122 * Array to hold the hashtab with variables local to each sourced script.
123 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000125typedef struct
126{
127 dictitem_T sv_var;
128 dict_T sv_dict;
129} scriptvar_T;
130
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200131static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
132#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
133#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134
135static int echo_attr = 0; /* attributes used for ":echo" */
136
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000137/* Values for trans_function_name() argument: */
138#define TFN_INT 1 /* internal function name OK */
139#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100140#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
141
142/* Values for get_lval() flags argument: */
143#define GLV_QUIET TFN_QUIET /* no error messages */
144#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000145
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146/*
147 * Structure to hold info for a user function.
148 */
149typedef struct ufunc ufunc_T;
150
151struct ufunc
152{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000153 int uf_varargs; /* variable nr of arguments */
154 int uf_flags;
155 int uf_calls; /* nr of active calls */
156 garray_T uf_args; /* arguments */
157 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000158#ifdef FEAT_PROFILE
159 int uf_profiling; /* TRUE when func is being profiled */
160 /* profiling the function as a whole */
161 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000162 proftime_T uf_tm_total; /* time spent in function + children */
163 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000164 proftime_T uf_tm_children; /* time spent in children this call */
165 /* profiling the function per line */
166 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000167 proftime_T *uf_tml_total; /* time spent in a line + children */
168 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000169 proftime_T uf_tml_start; /* start time for current line */
170 proftime_T uf_tml_children; /* time spent in children for this line */
171 proftime_T uf_tml_wait; /* start wait time for current line */
172 int uf_tml_idx; /* index of line being timed; -1 if none */
173 int uf_tml_execed; /* line being timed was executed */
174#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000175 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000177 int uf_refcount; /* for numbered function: reference count */
178 char_u uf_name[1]; /* name of function (actually longer); can
179 start with <SNR>123_ (<SNR> is K_SPECIAL
180 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181};
182
183/* function flags */
184#define FC_ABORT 1 /* abort function on error */
185#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000186#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187
188/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000189 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000191static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000193/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000194static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
195
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196/* From user function to hashitem and back. */
197static ufunc_T dumuf;
198#define UF2HIKEY(fp) ((fp)->uf_name)
199#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
200#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
201
202#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
203#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
Bram Moolenaar33570922005-01-25 22:26:29 +0000205#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
206#define VAR_SHORT_LEN 20 /* short variable name length */
207#define FIXVAR_CNT 12 /* number of fixed variables */
208
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000210typedef struct funccall_S funccall_T;
211
212struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213{
214 ufunc_T *func; /* function being called */
215 int linenr; /* next line to be executed */
216 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000217 struct /* fixed variables for arguments */
218 {
219 dictitem_T var; /* variable (without room for name) */
220 char_u room[VAR_SHORT_LEN]; /* room for the name */
221 } fixvar[FIXVAR_CNT];
222 dict_T l_vars; /* l: local function variables */
223 dictitem_T l_vars_var; /* variable for l: scope */
224 dict_T l_avars; /* a: argument variables */
225 dictitem_T l_avars_var; /* variable for a: scope */
226 list_T l_varlist; /* list for a:000 */
227 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
228 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 linenr_T breakpoint; /* next line with breakpoint or zero */
230 int dbg_tick; /* debug_tick when breakpoint was set */
231 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000232#ifdef FEAT_PROFILE
233 proftime_T prof_child; /* time spent in a child */
234#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000235 funccall_T *caller; /* calling function or NULL */
236};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237
238/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000239 * Info used by a ":for" loop.
240 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000241typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000242{
243 int fi_semicolon; /* TRUE if ending in '; var]' */
244 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000245 listwatch_T fi_lw; /* keep an eye on the item used. */
246 list_T *fi_list; /* list being used */
247} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000249/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000250 * Struct used by trans_function_name()
251 */
252typedef struct
253{
Bram Moolenaar33570922005-01-25 22:26:29 +0000254 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000255 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000256 dictitem_T *fd_di; /* Dictionary item used */
257} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000258
Bram Moolenaara7043832005-01-21 11:56:39 +0000259
260/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000261 * Array to hold the value of v: variables.
262 * The value is in a dictitem, so that it can also be used in the v: scope.
263 * The reason to use this table anyway is for very quick access to the
264 * variables with the VV_ defines.
265 */
266#include "version.h"
267
268/* values for vv_flags: */
269#define VV_COMPAT 1 /* compatible, also used without "v:" */
270#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000271#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000272
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100273#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000274
275static struct vimvar
276{
277 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100278 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
280} vimvars[VV_LEN] =
281{
282 /*
283 * The order here must match the VV_ defines in vim.h!
284 * Initializing a union does not work, leave tv.vval empty to get zero's.
285 */
286 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
287 {VV_NAME("count1", VAR_NUMBER), VV_RO},
288 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
289 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
290 {VV_NAME("warningmsg", VAR_STRING), 0},
291 {VV_NAME("statusmsg", VAR_STRING), 0},
292 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
293 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
294 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
295 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
296 {VV_NAME("termresponse", VAR_STRING), VV_RO},
297 {VV_NAME("fname", VAR_STRING), VV_RO},
298 {VV_NAME("lang", VAR_STRING), VV_RO},
299 {VV_NAME("lc_time", VAR_STRING), VV_RO},
300 {VV_NAME("ctype", VAR_STRING), VV_RO},
301 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
302 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
303 {VV_NAME("fname_in", VAR_STRING), VV_RO},
304 {VV_NAME("fname_out", VAR_STRING), VV_RO},
305 {VV_NAME("fname_new", VAR_STRING), VV_RO},
306 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
307 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
308 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
309 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
310 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
311 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
312 {VV_NAME("progname", VAR_STRING), VV_RO},
313 {VV_NAME("servername", VAR_STRING), VV_RO},
314 {VV_NAME("dying", VAR_NUMBER), VV_RO},
315 {VV_NAME("exception", VAR_STRING), VV_RO},
316 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
317 {VV_NAME("register", VAR_STRING), VV_RO},
318 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
319 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000320 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
321 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000322 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000323 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
324 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000325 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
326 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200327 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000328 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
329 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
330 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000331 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000332 {VV_NAME("swapname", VAR_STRING), VV_RO},
333 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000334 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200335 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000336 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200337 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000338 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
339 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000340 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000341 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100342 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000343 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200344 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200345 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200346 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200347 {VV_NAME("option_new", VAR_STRING), VV_RO},
348 {VV_NAME("option_old", VAR_STRING), VV_RO},
349 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100350 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100351 {VV_NAME("false", VAR_SPECIAL), VV_RO},
352 {VV_NAME("true", VAR_SPECIAL), VV_RO},
353 {VV_NAME("null", VAR_SPECIAL), VV_RO},
354 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100355 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200356 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000357};
358
359/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360#define vv_type vv_di.di_tv.v_type
361#define vv_nr vv_di.di_tv.vval.v_number
362#define vv_float vv_di.di_tv.vval.v_float
363#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000364#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200365#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000367
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200368static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000369#define vimvarht vimvardict.dv_hashtab
370
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100371static void prepare_vimvar(int idx, typval_T *save_tv);
372static void restore_vimvar(int idx, typval_T *save_tv);
373static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
374static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
375static char_u *skip_var_one(char_u *arg);
376static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
377static void list_glob_vars(int *first);
378static void list_buf_vars(int *first);
379static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000380#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100381static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000382#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100383static void list_vim_vars(int *first);
384static void list_script_vars(int *first);
385static void list_func_vars(int *first);
386static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
387static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
388static int check_changedtick(char_u *arg);
389static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
390static void clear_lval(lval_T *lp);
391static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
392static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100393static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
394static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
395static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
396static void item_lock(typval_T *tv, int deep, int lock);
397static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000398
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100399static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100400static int eval2(char_u **arg, typval_T *rettv, int evaluate);
401static int eval3(char_u **arg, typval_T *rettv, int evaluate);
402static int eval4(char_u **arg, typval_T *rettv, int evaluate);
403static int eval5(char_u **arg, typval_T *rettv, int evaluate);
404static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
405static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000406
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100407static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
408static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
409static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
410static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100411static int free_unref_items(int copyID);
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200412static int get_function_args(char_u **argp, char_u endchar, garray_T *newargs, int *varargs, int skip);
413static int get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100414static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100415static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
416static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100417static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
418static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, partial_T *partial, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100419static void emsg_funcname(char *ermsg, char_u *name);
420static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000421
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000422#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100423static void f_abs(typval_T *argvars, typval_T *rettv);
424static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000425#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100426static void f_add(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100427static void f_and(typval_T *argvars, typval_T *rettv);
428static void f_append(typval_T *argvars, typval_T *rettv);
429static void f_argc(typval_T *argvars, typval_T *rettv);
430static void f_argidx(typval_T *argvars, typval_T *rettv);
431static void f_arglistid(typval_T *argvars, typval_T *rettv);
432static void f_argv(typval_T *argvars, typval_T *rettv);
433static void f_assert_equal(typval_T *argvars, typval_T *rettv);
434static void f_assert_exception(typval_T *argvars, typval_T *rettv);
435static void f_assert_fails(typval_T *argvars, typval_T *rettv);
436static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200437static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200438static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
439static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100440static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000441#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100442static void f_asin(typval_T *argvars, typval_T *rettv);
443static void f_atan(typval_T *argvars, typval_T *rettv);
444static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000445#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100446static void f_browse(typval_T *argvars, typval_T *rettv);
447static void f_browsedir(typval_T *argvars, typval_T *rettv);
448static void f_bufexists(typval_T *argvars, typval_T *rettv);
449static void f_buflisted(typval_T *argvars, typval_T *rettv);
450static void f_bufloaded(typval_T *argvars, typval_T *rettv);
451static void f_bufname(typval_T *argvars, typval_T *rettv);
452static void f_bufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb3619a92016-06-04 17:58:52 +0200453static void f_bufwinid(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100454static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
455static void f_byte2line(typval_T *argvars, typval_T *rettv);
456static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
457static void f_byteidx(typval_T *argvars, typval_T *rettv);
458static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
459static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100461static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000462#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100463#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100464static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100465static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
466static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100467static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100468static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100469static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100470static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100471static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
472static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100473static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100474static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100475static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
476static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100477static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100478static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100479#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100480static void f_changenr(typval_T *argvars, typval_T *rettv);
481static void f_char2nr(typval_T *argvars, typval_T *rettv);
482static void f_cindent(typval_T *argvars, typval_T *rettv);
483static void f_clearmatches(typval_T *argvars, typval_T *rettv);
484static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000485#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100486static void f_complete(typval_T *argvars, typval_T *rettv);
487static void f_complete_add(typval_T *argvars, typval_T *rettv);
488static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000489#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100490static void f_confirm(typval_T *argvars, typval_T *rettv);
491static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000492#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100493static void f_cos(typval_T *argvars, typval_T *rettv);
494static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000495#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100496static void f_count(typval_T *argvars, typval_T *rettv);
497static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
498static void f_cursor(typval_T *argsvars, typval_T *rettv);
499static void f_deepcopy(typval_T *argvars, typval_T *rettv);
500static void f_delete(typval_T *argvars, typval_T *rettv);
501static void f_did_filetype(typval_T *argvars, typval_T *rettv);
502static void f_diff_filler(typval_T *argvars, typval_T *rettv);
503static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
504static void f_empty(typval_T *argvars, typval_T *rettv);
505static void f_escape(typval_T *argvars, typval_T *rettv);
506static void f_eval(typval_T *argvars, typval_T *rettv);
507static void f_eventhandler(typval_T *argvars, typval_T *rettv);
508static void f_executable(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79815f12016-07-09 17:07:29 +0200509static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100510static void f_exepath(typval_T *argvars, typval_T *rettv);
511static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200512#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100513static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200514#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100515static void f_expand(typval_T *argvars, typval_T *rettv);
516static void f_extend(typval_T *argvars, typval_T *rettv);
517static void f_feedkeys(typval_T *argvars, typval_T *rettv);
518static void f_filereadable(typval_T *argvars, typval_T *rettv);
519static void f_filewritable(typval_T *argvars, typval_T *rettv);
520static void f_filter(typval_T *argvars, typval_T *rettv);
521static void f_finddir(typval_T *argvars, typval_T *rettv);
522static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000523#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100524static void f_float2nr(typval_T *argvars, typval_T *rettv);
525static void f_floor(typval_T *argvars, typval_T *rettv);
526static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000527#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100528static void f_fnameescape(typval_T *argvars, typval_T *rettv);
529static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
530static void f_foldclosed(typval_T *argvars, typval_T *rettv);
531static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
532static void f_foldlevel(typval_T *argvars, typval_T *rettv);
533static void f_foldtext(typval_T *argvars, typval_T *rettv);
534static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
535static void f_foreground(typval_T *argvars, typval_T *rettv);
536static void f_function(typval_T *argvars, typval_T *rettv);
537static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
538static void f_get(typval_T *argvars, typval_T *rettv);
539static void f_getbufline(typval_T *argvars, typval_T *rettv);
540static void f_getbufvar(typval_T *argvars, typval_T *rettv);
541static void f_getchar(typval_T *argvars, typval_T *rettv);
542static void f_getcharmod(typval_T *argvars, typval_T *rettv);
543static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
544static void f_getcmdline(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200545#if defined(FEAT_CMDL_COMPL)
546static void f_getcompletion(typval_T *argvars, typval_T *rettv);
547#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100548static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
549static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
550static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
551static void f_getcwd(typval_T *argvars, typval_T *rettv);
552static void f_getfontname(typval_T *argvars, typval_T *rettv);
553static void f_getfperm(typval_T *argvars, typval_T *rettv);
554static void f_getfsize(typval_T *argvars, typval_T *rettv);
555static void f_getftime(typval_T *argvars, typval_T *rettv);
556static void f_getftype(typval_T *argvars, typval_T *rettv);
557static void f_getline(typval_T *argvars, typval_T *rettv);
558static void f_getmatches(typval_T *argvars, typval_T *rettv);
559static void f_getpid(typval_T *argvars, typval_T *rettv);
560static void f_getcurpos(typval_T *argvars, typval_T *rettv);
561static void f_getpos(typval_T *argvars, typval_T *rettv);
562static void f_getqflist(typval_T *argvars, typval_T *rettv);
563static void f_getreg(typval_T *argvars, typval_T *rettv);
564static void f_getregtype(typval_T *argvars, typval_T *rettv);
565static void f_gettabvar(typval_T *argvars, typval_T *rettv);
566static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
567static void f_getwinposx(typval_T *argvars, typval_T *rettv);
568static void f_getwinposy(typval_T *argvars, typval_T *rettv);
569static void f_getwinvar(typval_T *argvars, typval_T *rettv);
570static void f_glob(typval_T *argvars, typval_T *rettv);
571static void f_globpath(typval_T *argvars, typval_T *rettv);
572static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
573static void f_has(typval_T *argvars, typval_T *rettv);
574static void f_has_key(typval_T *argvars, typval_T *rettv);
575static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
576static void f_hasmapto(typval_T *argvars, typval_T *rettv);
577static void f_histadd(typval_T *argvars, typval_T *rettv);
578static void f_histdel(typval_T *argvars, typval_T *rettv);
579static void f_histget(typval_T *argvars, typval_T *rettv);
580static void f_histnr(typval_T *argvars, typval_T *rettv);
581static void f_hlID(typval_T *argvars, typval_T *rettv);
582static void f_hlexists(typval_T *argvars, typval_T *rettv);
583static void f_hostname(typval_T *argvars, typval_T *rettv);
584static void f_iconv(typval_T *argvars, typval_T *rettv);
585static void f_indent(typval_T *argvars, typval_T *rettv);
586static void f_index(typval_T *argvars, typval_T *rettv);
587static void f_input(typval_T *argvars, typval_T *rettv);
588static void f_inputdialog(typval_T *argvars, typval_T *rettv);
589static void f_inputlist(typval_T *argvars, typval_T *rettv);
590static void f_inputrestore(typval_T *argvars, typval_T *rettv);
591static void f_inputsave(typval_T *argvars, typval_T *rettv);
592static void f_inputsecret(typval_T *argvars, typval_T *rettv);
593static void f_insert(typval_T *argvars, typval_T *rettv);
594static void f_invert(typval_T *argvars, typval_T *rettv);
595static void f_isdirectory(typval_T *argvars, typval_T *rettv);
596static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100597#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
598static void f_isnan(typval_T *argvars, typval_T *rettv);
599#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100600static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100601#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100602static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100603static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100604static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100605static void f_job_start(typval_T *argvars, typval_T *rettv);
606static void f_job_stop(typval_T *argvars, typval_T *rettv);
607static void f_job_status(typval_T *argvars, typval_T *rettv);
608#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100609static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100610static void f_js_decode(typval_T *argvars, typval_T *rettv);
611static void f_js_encode(typval_T *argvars, typval_T *rettv);
612static void f_json_decode(typval_T *argvars, typval_T *rettv);
613static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100614static void f_keys(typval_T *argvars, typval_T *rettv);
615static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
616static void f_len(typval_T *argvars, typval_T *rettv);
617static void f_libcall(typval_T *argvars, typval_T *rettv);
618static void f_libcallnr(typval_T *argvars, typval_T *rettv);
619static void f_line(typval_T *argvars, typval_T *rettv);
620static void f_line2byte(typval_T *argvars, typval_T *rettv);
621static void f_lispindent(typval_T *argvars, typval_T *rettv);
622static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000623#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100624static void f_log(typval_T *argvars, typval_T *rettv);
625static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000626#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200627#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100628static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200629#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100630static void f_map(typval_T *argvars, typval_T *rettv);
631static void f_maparg(typval_T *argvars, typval_T *rettv);
632static void f_mapcheck(typval_T *argvars, typval_T *rettv);
633static void f_match(typval_T *argvars, typval_T *rettv);
634static void f_matchadd(typval_T *argvars, typval_T *rettv);
635static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
636static void f_matcharg(typval_T *argvars, typval_T *rettv);
637static void f_matchdelete(typval_T *argvars, typval_T *rettv);
638static void f_matchend(typval_T *argvars, typval_T *rettv);
639static void f_matchlist(typval_T *argvars, typval_T *rettv);
640static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200641static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100642static void f_max(typval_T *argvars, typval_T *rettv);
643static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000644#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100645static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000646#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100647static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100648#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100649static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100650#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100651static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
652static void f_nr2char(typval_T *argvars, typval_T *rettv);
653static void f_or(typval_T *argvars, typval_T *rettv);
654static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100655#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100656static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100657#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000658#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100659static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000660#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100661static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
662static void f_printf(typval_T *argvars, typval_T *rettv);
663static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200664#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100665static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200666#endif
667#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100668static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200669#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100670static void f_range(typval_T *argvars, typval_T *rettv);
671static void f_readfile(typval_T *argvars, typval_T *rettv);
672static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100673#ifdef FEAT_FLOAT
674static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
675#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100676static void f_reltimestr(typval_T *argvars, typval_T *rettv);
677static void f_remote_expr(typval_T *argvars, typval_T *rettv);
678static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
679static void f_remote_peek(typval_T *argvars, typval_T *rettv);
680static void f_remote_read(typval_T *argvars, typval_T *rettv);
681static void f_remote_send(typval_T *argvars, typval_T *rettv);
682static void f_remove(typval_T *argvars, typval_T *rettv);
683static void f_rename(typval_T *argvars, typval_T *rettv);
684static void f_repeat(typval_T *argvars, typval_T *rettv);
685static void f_resolve(typval_T *argvars, typval_T *rettv);
686static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000687#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100688static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000689#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_screenattr(typval_T *argvars, typval_T *rettv);
691static void f_screenchar(typval_T *argvars, typval_T *rettv);
692static void f_screencol(typval_T *argvars, typval_T *rettv);
693static void f_screenrow(typval_T *argvars, typval_T *rettv);
694static void f_search(typval_T *argvars, typval_T *rettv);
695static void f_searchdecl(typval_T *argvars, typval_T *rettv);
696static void f_searchpair(typval_T *argvars, typval_T *rettv);
697static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
698static void f_searchpos(typval_T *argvars, typval_T *rettv);
699static void f_server2client(typval_T *argvars, typval_T *rettv);
700static void f_serverlist(typval_T *argvars, typval_T *rettv);
701static void f_setbufvar(typval_T *argvars, typval_T *rettv);
702static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
703static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100704static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100705static void f_setline(typval_T *argvars, typval_T *rettv);
706static void f_setloclist(typval_T *argvars, typval_T *rettv);
707static void f_setmatches(typval_T *argvars, typval_T *rettv);
708static void f_setpos(typval_T *argvars, typval_T *rettv);
709static void f_setqflist(typval_T *argvars, typval_T *rettv);
710static void f_setreg(typval_T *argvars, typval_T *rettv);
711static void f_settabvar(typval_T *argvars, typval_T *rettv);
712static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
713static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100714#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100715static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100716#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100717static void f_shellescape(typval_T *argvars, typval_T *rettv);
718static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
719static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000720#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100721static void f_sin(typval_T *argvars, typval_T *rettv);
722static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000723#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100724static void f_sort(typval_T *argvars, typval_T *rettv);
725static void f_soundfold(typval_T *argvars, typval_T *rettv);
726static void f_spellbadword(typval_T *argvars, typval_T *rettv);
727static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
728static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000729#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100730static void f_sqrt(typval_T *argvars, typval_T *rettv);
731static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000732#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100733static void f_str2nr(typval_T *argvars, typval_T *rettv);
734static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000735#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100736static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000737#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200738static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100739static void f_stridx(typval_T *argvars, typval_T *rettv);
740static void f_string(typval_T *argvars, typval_T *rettv);
741static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200742static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100743static void f_strpart(typval_T *argvars, typval_T *rettv);
744static void f_strridx(typval_T *argvars, typval_T *rettv);
745static void f_strtrans(typval_T *argvars, typval_T *rettv);
746static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
747static void f_strwidth(typval_T *argvars, typval_T *rettv);
748static void f_submatch(typval_T *argvars, typval_T *rettv);
749static void f_substitute(typval_T *argvars, typval_T *rettv);
750static void f_synID(typval_T *argvars, typval_T *rettv);
751static void f_synIDattr(typval_T *argvars, typval_T *rettv);
752static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
753static void f_synstack(typval_T *argvars, typval_T *rettv);
754static void f_synconcealed(typval_T *argvars, typval_T *rettv);
755static void f_system(typval_T *argvars, typval_T *rettv);
756static void f_systemlist(typval_T *argvars, typval_T *rettv);
757static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
758static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
759static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
760static void f_taglist(typval_T *argvars, typval_T *rettv);
761static void f_tagfiles(typval_T *argvars, typval_T *rettv);
762static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200763static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5c719942016-07-09 23:40:45 +0200764static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200765static void f_test_disable_char_avail(typval_T *argvars, typval_T *rettv);
Bram Moolenaar574860b2016-05-24 17:33:34 +0200766static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
767#ifdef FEAT_JOB_CHANNEL
768static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
769#endif
770static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
771#ifdef FEAT_JOB_CHANNEL
772static void f_test_null_job(typval_T *argvars, typval_T *rettv);
773#endif
774static void f_test_null_list(typval_T *argvars, typval_T *rettv);
775static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
776static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +0200777static void f_test_settime(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200778#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100779static void f_tan(typval_T *argvars, typval_T *rettv);
780static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200781#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100782#ifdef FEAT_TIMERS
783static void f_timer_start(typval_T *argvars, typval_T *rettv);
784static void f_timer_stop(typval_T *argvars, typval_T *rettv);
785#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100786static void f_tolower(typval_T *argvars, typval_T *rettv);
787static void f_toupper(typval_T *argvars, typval_T *rettv);
788static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000789#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100790static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000791#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100792static void f_type(typval_T *argvars, typval_T *rettv);
793static void f_undofile(typval_T *argvars, typval_T *rettv);
794static void f_undotree(typval_T *argvars, typval_T *rettv);
795static void f_uniq(typval_T *argvars, typval_T *rettv);
796static void f_values(typval_T *argvars, typval_T *rettv);
797static void f_virtcol(typval_T *argvars, typval_T *rettv);
798static void f_visualmode(typval_T *argvars, typval_T *rettv);
799static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100800static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100801static void f_win_getid(typval_T *argvars, typval_T *rettv);
802static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
803static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
804static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100805static void f_winbufnr(typval_T *argvars, typval_T *rettv);
806static void f_wincol(typval_T *argvars, typval_T *rettv);
807static void f_winheight(typval_T *argvars, typval_T *rettv);
808static void f_winline(typval_T *argvars, typval_T *rettv);
809static void f_winnr(typval_T *argvars, typval_T *rettv);
810static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
811static void f_winrestview(typval_T *argvars, typval_T *rettv);
812static void f_winsaveview(typval_T *argvars, typval_T *rettv);
813static void f_winwidth(typval_T *argvars, typval_T *rettv);
814static void f_writefile(typval_T *argvars, typval_T *rettv);
815static void f_wordcount(typval_T *argvars, typval_T *rettv);
816static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000817
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100818static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
819static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
820static int get_env_len(char_u **arg);
821static int get_id_len(char_u **arg);
822static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
823static char_u *find_name_end(char_u *arg, char_u **expr_start, char_u **expr_end, int flags);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000824#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
825#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
826 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100827static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
828static int eval_isnamec(int c);
829static int eval_isnamec1(int c);
830static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
831static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100832static typval_T *alloc_string_tv(char_u *string);
833static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100834#ifdef FEAT_FLOAT
835static float_T get_tv_float(typval_T *varp);
836#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100837static linenr_T get_tv_lnum(typval_T *argvars);
838static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100839static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
840static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
841static hashtab_T *find_var_ht(char_u *name, char_u **varname);
842static funccall_T *get_funccal(void);
843static void vars_clear_ext(hashtab_T *ht, int free_val);
844static void delete_var(hashtab_T *ht, hashitem_T *hi);
845static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
846static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
847static void set_var(char_u *name, typval_T *varp, int copy);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100848static int var_check_fixed(int flags, char_u *name, int use_gettext);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100849static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100850static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd, partial_T **partial);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100851static int eval_fname_script(char_u *p);
852static int eval_fname_sid(char_u *p);
853static void list_func_head(ufunc_T *fp, int indent);
854static ufunc_T *find_func(char_u *name);
855static int function_exists(char_u *name);
856static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000857#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100858static void func_do_profile(ufunc_T *fp);
859static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
860static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000861static int
862# ifdef __BORLANDC__
863 _RTLENTRYF
864# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100865 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000866static int
867# ifdef __BORLANDC__
868 _RTLENTRYF
869# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100870 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000871#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100872static int script_autoload(char_u *name, int reload);
873static char_u *autoload_name(char_u *name);
874static void cat_func_name(char_u *buf, ufunc_T *fp);
875static void func_free(ufunc_T *fp);
876static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
877static int can_free_funccal(funccall_T *fc, int copyID) ;
878static void free_funccal(funccall_T *fc, int free_val);
879static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
880static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
881static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
882static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
883static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
884static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
885static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
886static int write_list(FILE *fd, list_T *list, int binary);
887static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000888
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200889
890#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100891static int compare_func_name(const void *s1, const void *s2);
892static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200893#endif
894
Bram Moolenaar33570922005-01-25 22:26:29 +0000895/*
896 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000897 */
898 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100899eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000900{
Bram Moolenaar33570922005-01-25 22:26:29 +0000901 int i;
902 struct vimvar *p;
903
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200904 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
905 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200906 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000907 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000908 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000909
910 for (i = 0; i < VV_LEN; ++i)
911 {
912 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200913 if (STRLEN(p->vv_name) > 16)
914 {
915 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
916 getout(1);
917 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000918 STRCPY(p->vv_di.di_key, p->vv_name);
919 if (p->vv_flags & VV_RO)
920 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
921 else if (p->vv_flags & VV_RO_SBX)
922 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
923 else
924 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000925
926 /* add to v: scope dict, unless the value is not always available */
927 if (p->vv_type != VAR_UNKNOWN)
928 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000929 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000930 /* add to compat scope dict */
931 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000932 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100933 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
934
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000935 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100936 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200937 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100938 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100939
940 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
941 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
942 set_vim_var_nr(VV_NONE, VVAL_NONE);
943 set_vim_var_nr(VV_NULL, VVAL_NULL);
944
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200945 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200946
947#ifdef EBCDIC
948 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100949 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200950 */
951 sortFunctions();
952#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000953}
954
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000955#if defined(EXITFREE) || defined(PROTO)
956 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100957eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000958{
959 int i;
960 struct vimvar *p;
961
962 for (i = 0; i < VV_LEN; ++i)
963 {
964 p = &vimvars[i];
965 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000966 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000967 vim_free(p->vv_str);
968 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000969 }
970 else if (p->vv_di.di_tv.v_type == VAR_LIST)
971 {
972 list_unref(p->vv_list);
973 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000974 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000975 }
976 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000977 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000978 hash_clear(&compat_hashtab);
979
Bram Moolenaard9fba312005-06-26 22:34:35 +0000980 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100981# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200982 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100983# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000984
985 /* global variables */
986 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000987
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000988 /* autoloaded script names */
989 ga_clear_strings(&ga_loaded);
990
Bram Moolenaarcca74132013-09-25 21:00:28 +0200991 /* Script-local variables. First clear all the variables and in a second
992 * loop free the scriptvar_T, because a variable in one script might hold
993 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200994 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200995 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200996 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200997 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200998 ga_clear(&ga_scripts);
999
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001000 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001001 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001002
1003 /* functions */
1004 free_all_functions();
1005 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001006}
1007#endif
1008
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 * Return the name of the executed function.
1011 */
1012 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001013func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001015 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016}
1017
1018/*
1019 * Return the address holding the next breakpoint line for a funccall cookie.
1020 */
1021 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001022func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023{
Bram Moolenaar33570922005-01-25 22:26:29 +00001024 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025}
1026
1027/*
1028 * Return the address holding the debug tick for a funccall cookie.
1029 */
1030 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001031func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032{
Bram Moolenaar33570922005-01-25 22:26:29 +00001033 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034}
1035
1036/*
1037 * Return the nesting level for a funccall cookie.
1038 */
1039 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001040func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041{
Bram Moolenaar33570922005-01-25 22:26:29 +00001042 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043}
1044
1045/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001046funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001048/* pointer to list of previously used funccal, still around because some
1049 * item in it is still being used. */
1050funccall_T *previous_funccal = NULL;
1051
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052/*
1053 * Return TRUE when a function was ended by a ":return" command.
1054 */
1055 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001056current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057{
1058 return current_funccal->returned;
1059}
1060
1061
1062/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 * Set an internal variable to a string value. Creates the variable if it does
1064 * not already exist.
1065 */
1066 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001067set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001069 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001070 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071
1072 val = vim_strsave(value);
1073 if (val != NULL)
1074 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001075 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001076 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001078 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001079 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 }
1081 }
1082}
1083
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001084static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +02001085#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001086static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087static char_u *redir_endp = NULL;
1088static char_u *redir_varname = NULL;
1089
1090/*
1091 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001092 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093 * Returns OK if successfully completed the setup. FAIL otherwise.
1094 */
1095 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001096var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001097{
1098 int save_emsg;
1099 int err;
1100 typval_T tv;
1101
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001102 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001103 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104 {
1105 EMSG(_(e_invarg));
1106 return FAIL;
1107 }
1108
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001109 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 redir_varname = vim_strsave(name);
1111 if (redir_varname == NULL)
1112 return FAIL;
1113
1114 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1115 if (redir_lval == NULL)
1116 {
1117 var_redir_stop();
1118 return FAIL;
1119 }
1120
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001121 /* The output is stored in growarray "redir_ga" until redirection ends. */
1122 ga_init2(&redir_ga, (int)sizeof(char), 500);
1123
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001124 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001125 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001126 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1128 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001129 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130 if (redir_endp != NULL && *redir_endp != NUL)
1131 /* Trailing characters are present after the variable name */
1132 EMSG(_(e_trailing));
1133 else
1134 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001135 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 var_redir_stop();
1137 return FAIL;
1138 }
1139
1140 /* check if we can write to the variable: set it to or append an empty
1141 * string */
1142 save_emsg = did_emsg;
1143 did_emsg = FALSE;
1144 tv.v_type = VAR_STRING;
1145 tv.vval.v_string = (char_u *)"";
1146 if (append)
1147 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1148 else
1149 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001150 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001151 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001152 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001153 if (err)
1154 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001155 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001156 var_redir_stop();
1157 return FAIL;
1158 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159
1160 return OK;
1161}
1162
1163/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001164 * Append "value[value_len]" to the variable set by var_redir_start().
1165 * The actual appending is postponed until redirection ends, because the value
1166 * appended may in fact be the string we write to, changing it may cause freed
1167 * memory to be used:
1168 * :redir => foo
1169 * :let foo
1170 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001171 */
1172 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001173var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001174{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001175 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001176
1177 if (redir_lval == NULL)
1178 return;
1179
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001180 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001181 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001182 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001183 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001184
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001185 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001186 {
1187 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001188 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001189 }
1190 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001191 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001192}
1193
1194/*
1195 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001196 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001197 */
1198 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001199var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001200{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001201 typval_T tv;
1202
Bram Moolenaar1e5e1232016-07-07 17:33:02 +02001203 if (EVALCMD_BUSY)
1204 {
1205 redir_lval = NULL;
1206 return;
1207 }
1208
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001209 if (redir_lval != NULL)
1210 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001211 /* If there was no error: assign the text to the variable. */
1212 if (redir_endp != NULL)
1213 {
1214 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1215 tv.v_type = VAR_STRING;
1216 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001217 /* Call get_lval() again, if it's inside a Dict or List it may
1218 * have changed. */
1219 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001220 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001221 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1222 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1223 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001224 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001225
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001226 /* free the collected output */
1227 vim_free(redir_ga.ga_data);
1228 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001229
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001230 vim_free(redir_lval);
1231 redir_lval = NULL;
1232 }
1233 vim_free(redir_varname);
1234 redir_varname = NULL;
1235}
1236
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237# if defined(FEAT_MBYTE) || defined(PROTO)
1238 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001239eval_charconvert(
1240 char_u *enc_from,
1241 char_u *enc_to,
1242 char_u *fname_from,
1243 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244{
1245 int err = FALSE;
1246
1247 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1248 set_vim_var_string(VV_CC_TO, enc_to, -1);
1249 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1250 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1251 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1252 err = TRUE;
1253 set_vim_var_string(VV_CC_FROM, NULL, -1);
1254 set_vim_var_string(VV_CC_TO, NULL, -1);
1255 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1256 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1257
1258 if (err)
1259 return FAIL;
1260 return OK;
1261}
1262# endif
1263
1264# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1265 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001266eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267{
1268 int err = FALSE;
1269
1270 set_vim_var_string(VV_FNAME_IN, fname, -1);
1271 set_vim_var_string(VV_CMDARG, args, -1);
1272 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1273 err = TRUE;
1274 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1275 set_vim_var_string(VV_CMDARG, NULL, -1);
1276
1277 if (err)
1278 {
1279 mch_remove(fname);
1280 return FAIL;
1281 }
1282 return OK;
1283}
1284# endif
1285
1286# if defined(FEAT_DIFF) || defined(PROTO)
1287 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001288eval_diff(
1289 char_u *origfile,
1290 char_u *newfile,
1291 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292{
1293 int err = FALSE;
1294
1295 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1296 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1297 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1298 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1299 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1300 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1301 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1302}
1303
1304 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001305eval_patch(
1306 char_u *origfile,
1307 char_u *difffile,
1308 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309{
1310 int err;
1311
1312 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1313 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1314 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1315 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1316 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1317 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1318 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1319}
1320# endif
1321
1322/*
1323 * Top level evaluation function, returning a boolean.
1324 * Sets "error" to TRUE if there was an error.
1325 * Return TRUE or FALSE.
1326 */
1327 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001328eval_to_bool(
1329 char_u *arg,
1330 int *error,
1331 char_u **nextcmd,
1332 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333{
Bram Moolenaar33570922005-01-25 22:26:29 +00001334 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001335 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336
1337 if (skip)
1338 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001339 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 else
1342 {
1343 *error = FALSE;
1344 if (!skip)
1345 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001346 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001347 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 }
1349 }
1350 if (skip)
1351 --emsg_skip;
1352
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001353 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354}
1355
1356/*
1357 * Top level evaluation function, returning a string. If "skip" is TRUE,
1358 * only parsing to "nextcmd" is done, without reporting errors. Return
1359 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1360 */
1361 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001362eval_to_string_skip(
1363 char_u *arg,
1364 char_u **nextcmd,
1365 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366{
Bram Moolenaar33570922005-01-25 22:26:29 +00001367 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 char_u *retval;
1369
1370 if (skip)
1371 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001372 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 retval = NULL;
1374 else
1375 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001376 retval = vim_strsave(get_tv_string(&tv));
1377 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 }
1379 if (skip)
1380 --emsg_skip;
1381
1382 return retval;
1383}
1384
1385/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001386 * Skip over an expression at "*pp".
1387 * Return FAIL for an error, OK otherwise.
1388 */
1389 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001390skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001391{
Bram Moolenaar33570922005-01-25 22:26:29 +00001392 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001393
1394 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001395 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001396}
1397
1398/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001400 * When "convert" is TRUE convert a List into a sequence of lines and convert
1401 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 * Return pointer to allocated memory, or NULL for failure.
1403 */
1404 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001405eval_to_string(
1406 char_u *arg,
1407 char_u **nextcmd,
1408 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409{
Bram Moolenaar33570922005-01-25 22:26:29 +00001410 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001412 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001413#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001414 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001417 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 retval = NULL;
1419 else
1420 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001421 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001422 {
1423 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001424 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001425 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02001426 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001427 if (tv.vval.v_list->lv_len > 0)
1428 ga_append(&ga, NL);
1429 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001430 ga_append(&ga, NUL);
1431 retval = (char_u *)ga.ga_data;
1432 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001433#ifdef FEAT_FLOAT
1434 else if (convert && tv.v_type == VAR_FLOAT)
1435 {
1436 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1437 retval = vim_strsave(numbuf);
1438 }
1439#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001440 else
1441 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001442 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 }
1444
1445 return retval;
1446}
1447
1448/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001449 * Call eval_to_string() without using current local variables and using
1450 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 */
1452 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001453eval_to_string_safe(
1454 char_u *arg,
1455 char_u **nextcmd,
1456 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457{
1458 char_u *retval;
1459 void *save_funccalp;
1460
1461 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001462 if (use_sandbox)
1463 ++sandbox;
1464 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001465 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001466 if (use_sandbox)
1467 --sandbox;
1468 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 restore_funccal(save_funccalp);
1470 return retval;
1471}
1472
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473/*
1474 * Top level evaluation function, returning a number.
1475 * Evaluates "expr" silently.
1476 * Returns -1 for an error.
1477 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001478 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001479eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480{
Bram Moolenaar33570922005-01-25 22:26:29 +00001481 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001482 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001483 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484
1485 ++emsg_off;
1486
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001487 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 retval = -1;
1489 else
1490 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001491 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001492 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 }
1494 --emsg_off;
1495
1496 return retval;
1497}
1498
Bram Moolenaara40058a2005-07-11 22:42:07 +00001499/*
1500 * Prepare v: variable "idx" to be used.
1501 * Save the current typeval in "save_tv".
1502 * When not used yet add the variable to the v: hashtable.
1503 */
1504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001505prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001506{
1507 *save_tv = vimvars[idx].vv_tv;
1508 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1509 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1510}
1511
1512/*
1513 * Restore v: variable "idx" to typeval "save_tv".
1514 * When no longer defined, remove the variable from the v: hashtable.
1515 */
1516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001517restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001518{
1519 hashitem_T *hi;
1520
Bram Moolenaara40058a2005-07-11 22:42:07 +00001521 vimvars[idx].vv_tv = *save_tv;
1522 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1523 {
1524 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1525 if (HASHITEM_EMPTY(hi))
1526 EMSG2(_(e_intern2), "restore_vimvar()");
1527 else
1528 hash_remove(&vimvarht, hi);
1529 }
1530}
1531
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001532#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001533/*
1534 * Evaluate an expression to a list with suggestions.
1535 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001536 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001537 */
1538 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001539eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001540{
1541 typval_T save_val;
1542 typval_T rettv;
1543 list_T *list = NULL;
1544 char_u *p = skipwhite(expr);
1545
1546 /* Set "v:val" to the bad word. */
1547 prepare_vimvar(VV_VAL, &save_val);
1548 vimvars[VV_VAL].vv_type = VAR_STRING;
1549 vimvars[VV_VAL].vv_str = badword;
1550 if (p_verbose == 0)
1551 ++emsg_off;
1552
1553 if (eval1(&p, &rettv, TRUE) == OK)
1554 {
1555 if (rettv.v_type != VAR_LIST)
1556 clear_tv(&rettv);
1557 else
1558 list = rettv.vval.v_list;
1559 }
1560
1561 if (p_verbose == 0)
1562 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001563 restore_vimvar(VV_VAL, &save_val);
1564
1565 return list;
1566}
1567
1568/*
1569 * "list" is supposed to contain two items: a word and a number. Return the
1570 * word in "pp" and the number as the return value.
1571 * Return -1 if anything isn't right.
1572 * Used to get the good word and score from the eval_spell_expr() result.
1573 */
1574 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001575get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001576{
1577 listitem_T *li;
1578
1579 li = list->lv_first;
1580 if (li == NULL)
1581 return -1;
1582 *pp = get_tv_string(&li->li_tv);
1583
1584 li = li->li_next;
1585 if (li == NULL)
1586 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001587 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001588}
1589#endif
1590
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001591/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001592 * Top level evaluation function.
1593 * Returns an allocated typval_T with the result.
1594 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001595 */
1596 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001597eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001598{
1599 typval_T *tv;
1600
1601 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001602 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001603 {
1604 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001605 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001606 }
1607
1608 return tv;
1609}
1610
1611
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001613 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001614 * Uses argv[argc] for the function arguments. Only Number and String
1615 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001616 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001618 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001619call_vim_function(
1620 char_u *func,
1621 int argc,
1622 char_u **argv,
1623 int safe, /* use the sandbox */
1624 int str_arg_only, /* all arguments are strings */
1625 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626{
Bram Moolenaar33570922005-01-25 22:26:29 +00001627 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001628 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 int len;
1630 int i;
1631 int doesrange;
1632 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001633 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001635 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001637 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
1639 for (i = 0; i < argc; i++)
1640 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001641 /* Pass a NULL or empty argument as an empty string */
1642 if (argv[i] == NULL || *argv[i] == NUL)
1643 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001644 argvars[i].v_type = VAR_STRING;
1645 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001646 continue;
1647 }
1648
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001649 if (str_arg_only)
1650 len = 0;
1651 else
1652 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001653 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 if (len != 0 && len == (int)STRLEN(argv[i]))
1655 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001656 argvars[i].v_type = VAR_NUMBER;
1657 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 }
1659 else
1660 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001661 argvars[i].v_type = VAR_STRING;
1662 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 }
1664 }
1665
1666 if (safe)
1667 {
1668 save_funccalp = save_funccal();
1669 ++sandbox;
1670 }
1671
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1673 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001675 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 if (safe)
1677 {
1678 --sandbox;
1679 restore_funccal(save_funccalp);
1680 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001681 vim_free(argvars);
1682
1683 if (ret == FAIL)
1684 clear_tv(rettv);
1685
1686 return ret;
1687}
1688
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001689/*
1690 * Call vimL function "func" and return the result as a number.
1691 * Returns -1 when calling the function fails.
1692 * Uses argv[argc] for the function arguments.
1693 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001694 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001695call_func_retnr(
1696 char_u *func,
1697 int argc,
1698 char_u **argv,
1699 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001700{
1701 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001702 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001703
1704 /* All arguments are passed as strings, no conversion to number. */
1705 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1706 return -1;
1707
1708 retval = get_tv_number_chk(&rettv, NULL);
1709 clear_tv(&rettv);
1710 return retval;
1711}
1712
1713#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1714 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1715
Bram Moolenaar4f688582007-07-24 12:34:30 +00001716# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001717/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001718 * Call vimL function "func" and return the result as a string.
1719 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001720 * Uses argv[argc] for the function arguments.
1721 */
1722 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001723call_func_retstr(
1724 char_u *func,
1725 int argc,
1726 char_u **argv,
1727 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001728{
1729 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001730 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001731
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001732 /* All arguments are passed as strings, no conversion to number. */
1733 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001734 return NULL;
1735
1736 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001737 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 return retval;
1739}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001740# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001741
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001742/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001743 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001744 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001745 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001746 */
1747 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001748call_func_retlist(
1749 char_u *func,
1750 int argc,
1751 char_u **argv,
1752 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001753{
1754 typval_T rettv;
1755
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001756 /* All arguments are passed as strings, no conversion to number. */
1757 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001758 return NULL;
1759
1760 if (rettv.v_type != VAR_LIST)
1761 {
1762 clear_tv(&rettv);
1763 return NULL;
1764 }
1765
1766 return rettv.vval.v_list;
1767}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768#endif
1769
1770/*
1771 * Save the current function call pointer, and set it to NULL.
1772 * Used when executing autocommands and for ":source".
1773 */
1774 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001775save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001777 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 current_funccal = NULL;
1780 return (void *)fc;
1781}
1782
1783 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001784restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001786 funccall_T *fc = (funccall_T *)vfc;
1787
1788 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789}
1790
Bram Moolenaar05159a02005-02-26 23:04:13 +00001791#if defined(FEAT_PROFILE) || defined(PROTO)
1792/*
1793 * Prepare profiling for entering a child or something else that is not
1794 * counted for the script/function itself.
1795 * Should always be called in pair with prof_child_exit().
1796 */
1797 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001798prof_child_enter(
1799 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001800{
1801 funccall_T *fc = current_funccal;
1802
1803 if (fc != NULL && fc->func->uf_profiling)
1804 profile_start(&fc->prof_child);
1805 script_prof_save(tm);
1806}
1807
1808/*
1809 * Take care of time spent in a child.
1810 * Should always be called after prof_child_enter().
1811 */
1812 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001813prof_child_exit(
1814 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001815{
1816 funccall_T *fc = current_funccal;
1817
1818 if (fc != NULL && fc->func->uf_profiling)
1819 {
1820 profile_end(&fc->prof_child);
1821 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1822 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1823 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1824 }
1825 script_prof_restore(tm);
1826}
1827#endif
1828
1829
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830#ifdef FEAT_FOLDING
1831/*
1832 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1833 * it in "*cp". Doesn't give error messages.
1834 */
1835 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001836eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837{
Bram Moolenaar33570922005-01-25 22:26:29 +00001838 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001839 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001841 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1842 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
1844 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001845 if (use_sandbox)
1846 ++sandbox;
1847 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001849 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 retval = 0;
1851 else
1852 {
1853 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001854 if (tv.v_type == VAR_NUMBER)
1855 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001856 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 retval = 0;
1858 else
1859 {
1860 /* If the result is a string, check if there is a non-digit before
1861 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001862 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 if (!VIM_ISDIGIT(*s) && *s != '-')
1864 *cp = *s++;
1865 retval = atol((char *)s);
1866 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 }
1869 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001870 if (use_sandbox)
1871 --sandbox;
1872 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001874 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875}
1876#endif
1877
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001879 * ":let" list all variable values
1880 * ":let var1 var2" list variable values
1881 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001882 * ":let var += expr" assignment command.
1883 * ":let var -= expr" assignment command.
1884 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001885 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 */
1887 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001888ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889{
1890 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001891 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001892 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 int var_count = 0;
1895 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001896 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001897 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001898 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899
Bram Moolenaardb552d602006-03-23 22:59:57 +00001900 argend = skip_var_list(arg, &var_count, &semicolon);
1901 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001902 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001903 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1904 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001905 expr = skipwhite(argend);
1906 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1907 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001909 /*
1910 * ":let" without "=": list variables
1911 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 if (*arg == '[')
1913 EMSG(_(e_invarg));
1914 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001915 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001916 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001918 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001920 list_glob_vars(&first);
1921 list_buf_vars(&first);
1922 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001923#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001924 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001925#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001926 list_script_vars(&first);
1927 list_func_vars(&first);
1928 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 eap->nextcmd = check_nextcmd(arg);
1931 }
1932 else
1933 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 op[0] = '=';
1935 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001936 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001937 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001938 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1939 op[0] = *expr; /* +=, -= or .= */
1940 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001941 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001942 else
1943 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001944
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 if (eap->skip)
1946 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001947 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 if (eap->skip)
1949 {
1950 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001951 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 --emsg_skip;
1953 }
1954 else if (i != FAIL)
1955 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001957 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 }
1960 }
1961}
1962
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001963/*
1964 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1965 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001966 * When "nextchars" is not NULL it points to a string with characters that
1967 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1968 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001969 * Returns OK or FAIL;
1970 */
1971 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001972ex_let_vars(
1973 char_u *arg_start,
1974 typval_T *tv,
1975 int copy, /* copy values from "tv", don't move */
1976 int semicolon, /* from skip_var_list() */
1977 int var_count, /* from skip_var_list() */
1978 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979{
1980 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001981 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001983 listitem_T *item;
1984 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001985
1986 if (*arg != '[')
1987 {
1988 /*
1989 * ":let var = expr" or ":for var in list"
1990 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001991 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001992 return FAIL;
1993 return OK;
1994 }
1995
1996 /*
1997 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1998 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001999 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002000 {
2001 EMSG(_(e_listreq));
2002 return FAIL;
2003 }
2004
2005 i = list_len(l);
2006 if (semicolon == 0 && var_count < i)
2007 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002008 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002009 return FAIL;
2010 }
2011 if (var_count - semicolon > i)
2012 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002013 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002014 return FAIL;
2015 }
2016
2017 item = l->lv_first;
2018 while (*arg != ']')
2019 {
2020 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002021 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002022 item = item->li_next;
2023 if (arg == NULL)
2024 return FAIL;
2025
2026 arg = skipwhite(arg);
2027 if (*arg == ';')
2028 {
2029 /* Put the rest of the list (may be empty) in the var after ';'.
2030 * Create a new list for this. */
2031 l = list_alloc();
2032 if (l == NULL)
2033 return FAIL;
2034 while (item != NULL)
2035 {
2036 list_append_tv(l, &item->li_tv);
2037 item = item->li_next;
2038 }
2039
2040 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002041 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002042 ltv.vval.v_list = l;
2043 l->lv_refcount = 1;
2044
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002045 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2046 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002047 clear_tv(&ltv);
2048 if (arg == NULL)
2049 return FAIL;
2050 break;
2051 }
2052 else if (*arg != ',' && *arg != ']')
2053 {
2054 EMSG2(_(e_intern2), "ex_let_vars()");
2055 return FAIL;
2056 }
2057 }
2058
2059 return OK;
2060}
2061
2062/*
2063 * Skip over assignable variable "var" or list of variables "[var, var]".
2064 * Used for ":let varvar = expr" and ":for varvar in expr".
2065 * For "[var, var]" increment "*var_count" for each variable.
2066 * for "[var, var; var]" set "semicolon".
2067 * Return NULL for an error.
2068 */
2069 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002070skip_var_list(
2071 char_u *arg,
2072 int *var_count,
2073 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002074{
2075 char_u *p, *s;
2076
2077 if (*arg == '[')
2078 {
2079 /* "[var, var]": find the matching ']'. */
2080 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002081 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002082 {
2083 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2084 s = skip_var_one(p);
2085 if (s == p)
2086 {
2087 EMSG2(_(e_invarg2), p);
2088 return NULL;
2089 }
2090 ++*var_count;
2091
2092 p = skipwhite(s);
2093 if (*p == ']')
2094 break;
2095 else if (*p == ';')
2096 {
2097 if (*semicolon == 1)
2098 {
2099 EMSG(_("Double ; in list of variables"));
2100 return NULL;
2101 }
2102 *semicolon = 1;
2103 }
2104 else if (*p != ',')
2105 {
2106 EMSG2(_(e_invarg2), p);
2107 return NULL;
2108 }
2109 }
2110 return p + 1;
2111 }
2112 else
2113 return skip_var_one(arg);
2114}
2115
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002116/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002117 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002118 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002119 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002120 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002121skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002122{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002123 if (*arg == '@' && arg[1] != NUL)
2124 return arg + 2;
2125 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2126 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002127}
2128
Bram Moolenaara7043832005-01-21 11:56:39 +00002129/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002130 * List variables for hashtab "ht" with prefix "prefix".
2131 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002132 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002134list_hashtable_vars(
2135 hashtab_T *ht,
2136 char_u *prefix,
2137 int empty,
2138 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002139{
Bram Moolenaar33570922005-01-25 22:26:29 +00002140 hashitem_T *hi;
2141 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002142 int todo;
2143
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002144 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002145 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2146 {
2147 if (!HASHITEM_EMPTY(hi))
2148 {
2149 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002150 di = HI2DI(hi);
2151 if (empty || di->di_tv.v_type != VAR_STRING
2152 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002153 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002154 }
2155 }
2156}
2157
2158/*
2159 * List global variables.
2160 */
2161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002162list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002163{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002165}
2166
2167/*
2168 * List buffer variables.
2169 */
2170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002171list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002172{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002173 char_u numbuf[NUMBUFLEN];
2174
Bram Moolenaar429fa852013-04-15 12:27:36 +02002175 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002176 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002177
2178 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2180 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002181}
2182
2183/*
2184 * List window variables.
2185 */
2186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002187list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002188{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002189 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002190 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002191}
2192
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002193#ifdef FEAT_WINDOWS
2194/*
2195 * List tab page variables.
2196 */
2197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002198list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002199{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002200 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002201 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002202}
2203#endif
2204
Bram Moolenaara7043832005-01-21 11:56:39 +00002205/*
2206 * List Vim variables.
2207 */
2208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002209list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002211 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212}
2213
2214/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002215 * List script-local variables, if there is a script.
2216 */
2217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002218list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002219{
2220 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002221 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2222 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002223}
2224
2225/*
2226 * List function variables, if there is a function.
2227 */
2228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002229list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002230{
2231 if (current_funccal != NULL)
2232 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002233 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002234}
2235
2236/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 * List variables in "arg".
2238 */
2239 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002240list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241{
2242 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002243 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002245 char_u *name_start;
2246 char_u *arg_subsc;
2247 char_u *tofree;
2248 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249
2250 while (!ends_excmd(*arg) && !got_int)
2251 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002252 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002254 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2256 {
2257 emsg_severe = TRUE;
2258 EMSG(_(e_trailing));
2259 break;
2260 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 /* get_name_len() takes care of expanding curly braces */
2265 name_start = name = arg;
2266 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2267 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002269 /* This is mainly to keep test 49 working: when expanding
2270 * curly braces fails overrule the exception error message. */
2271 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 emsg_severe = TRUE;
2274 EMSG2(_(e_invarg2), arg);
2275 break;
2276 }
2277 error = TRUE;
2278 }
2279 else
2280 {
2281 if (tofree != NULL)
2282 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002283 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 else
2286 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002287 /* handle d.key, l[idx], f(expr) */
2288 arg_subsc = arg;
2289 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002290 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002292 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002293 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002294 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002296 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002297 case 'g': list_glob_vars(first); break;
2298 case 'b': list_buf_vars(first); break;
2299 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002300#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002301 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002302#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002303 case 'v': list_vim_vars(first); break;
2304 case 's': list_script_vars(first); break;
2305 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002306 default:
2307 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002308 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002309 }
2310 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002311 {
2312 char_u numbuf[NUMBUFLEN];
2313 char_u *tf;
2314 int c;
2315 char_u *s;
2316
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002317 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002318 c = *arg;
2319 *arg = NUL;
2320 list_one_var_a((char_u *)"",
2321 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002322 tv.v_type,
2323 s == NULL ? (char_u *)"" : s,
2324 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002325 *arg = c;
2326 vim_free(tf);
2327 }
2328 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002329 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 }
2331 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002332
2333 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002335
2336 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 }
2338
2339 return arg;
2340}
2341
2342/*
2343 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2344 * Returns a pointer to the char just after the var name.
2345 * Returns NULL if there is an error.
2346 */
2347 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002348ex_let_one(
2349 char_u *arg, /* points to variable name */
2350 typval_T *tv, /* value to assign to variable */
2351 int copy, /* copy value from "tv" */
2352 char_u *endchars, /* valid chars after variable name or NULL */
2353 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002354{
2355 int c1;
2356 char_u *name;
2357 char_u *p;
2358 char_u *arg_end = NULL;
2359 int len;
2360 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002361 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002362
2363 /*
2364 * ":let $VAR = expr": Set environment variable.
2365 */
2366 if (*arg == '$')
2367 {
2368 /* Find the end of the name. */
2369 ++arg;
2370 name = arg;
2371 len = get_env_len(&arg);
2372 if (len == 0)
2373 EMSG2(_(e_invarg2), name - 1);
2374 else
2375 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002376 if (op != NULL && (*op == '+' || *op == '-'))
2377 EMSG2(_(e_letwrong), op);
2378 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002379 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002380 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002381 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002382 {
2383 c1 = name[len];
2384 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002385 p = get_tv_string_chk(tv);
2386 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387 {
2388 int mustfree = FALSE;
2389 char_u *s = vim_getenv(name, &mustfree);
2390
2391 if (s != NULL)
2392 {
2393 p = tofree = concat_str(s, p);
2394 if (mustfree)
2395 vim_free(s);
2396 }
2397 }
2398 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002399 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002400 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002401 if (STRICMP(name, "HOME") == 0)
2402 init_homedir();
2403 else if (didset_vim && STRICMP(name, "VIM") == 0)
2404 didset_vim = FALSE;
2405 else if (didset_vimruntime
2406 && STRICMP(name, "VIMRUNTIME") == 0)
2407 didset_vimruntime = FALSE;
2408 arg_end = arg;
2409 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002410 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002411 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002412 }
2413 }
2414 }
2415
2416 /*
2417 * ":let &option = expr": Set option value.
2418 * ":let &l:option = expr": Set local option value.
2419 * ":let &g:option = expr": Set global option value.
2420 */
2421 else if (*arg == '&')
2422 {
2423 /* Find the end of the name. */
2424 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002425 if (p == NULL || (endchars != NULL
2426 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002427 EMSG(_(e_letunexp));
2428 else
2429 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002430 long n;
2431 int opt_type;
2432 long numval;
2433 char_u *stringval = NULL;
2434 char_u *s;
2435
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002436 c1 = *p;
2437 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002439 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002440 s = get_tv_string_chk(tv); /* != NULL if number or string */
2441 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 {
2443 opt_type = get_option_value(arg, &numval,
2444 &stringval, opt_flags);
2445 if ((opt_type == 1 && *op == '.')
2446 || (opt_type == 0 && *op != '.'))
2447 EMSG2(_(e_letwrong), op);
2448 else
2449 {
2450 if (opt_type == 1) /* number */
2451 {
2452 if (*op == '+')
2453 n = numval + n;
2454 else
2455 n = numval - n;
2456 }
2457 else if (opt_type == 0 && stringval != NULL) /* string */
2458 {
2459 s = concat_str(stringval, s);
2460 vim_free(stringval);
2461 stringval = s;
2462 }
2463 }
2464 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002465 if (s != NULL)
2466 {
2467 set_option_value(arg, n, s, opt_flags);
2468 arg_end = p;
2469 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002471 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002472 }
2473 }
2474
2475 /*
2476 * ":let @r = expr": Set register contents.
2477 */
2478 else if (*arg == '@')
2479 {
2480 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002481 if (op != NULL && (*op == '+' || *op == '-'))
2482 EMSG2(_(e_letwrong), op);
2483 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002484 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002485 EMSG(_(e_letunexp));
2486 else
2487 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002488 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002489 char_u *s;
2490
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002491 p = get_tv_string_chk(tv);
2492 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002493 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002494 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002495 if (s != NULL)
2496 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002497 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002498 vim_free(s);
2499 }
2500 }
2501 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002502 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002503 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002504 arg_end = arg + 1;
2505 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002506 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507 }
2508 }
2509
2510 /*
2511 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002513 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002514 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002516 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002518 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2522 EMSG(_(e_letunexp));
2523 else
2524 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002525 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 arg_end = p;
2527 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002528 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002530 }
2531
2532 else
2533 EMSG2(_(e_invarg2), arg);
2534
2535 return arg_end;
2536}
2537
2538/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002539 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2540 */
2541 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002542check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002543{
2544 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2545 {
2546 EMSG2(_(e_readonlyvar), arg);
2547 return TRUE;
2548 }
2549 return FALSE;
2550}
2551
2552/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 * Get an lval: variable, Dict item or List item that can be assigned a value
2554 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2555 * "name.key", "name.key[expr]" etc.
2556 * Indexing only works if "name" is an existing List or Dictionary.
2557 * "name" points to the start of the name.
2558 * If "rettv" is not NULL it points to the value to be assigned.
2559 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2560 * wrong; must end in space or cmd separator.
2561 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002562 * flags:
2563 * GLV_QUIET: do not give error messages
2564 * GLV_NO_AUTOLOAD: do not use script autoloading
2565 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002567 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569 */
2570 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002571get_lval(
2572 char_u *name,
2573 typval_T *rettv,
2574 lval_T *lp,
2575 int unlet,
2576 int skip,
2577 int flags, /* GLV_ values */
2578 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002579{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002580 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 char_u *expr_start, *expr_end;
2582 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002583 dictitem_T *v;
2584 typval_T var1;
2585 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002587 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002588 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002589 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002590 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002591 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002592
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002594 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595
2596 if (skip)
2597 {
2598 /* When skipping just find the end of the name. */
2599 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002600 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 }
2602
2603 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002604 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (expr_start != NULL)
2606 {
2607 /* Don't expand the name when we already know there is an error. */
2608 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2609 && *p != '[' && *p != '.')
2610 {
2611 EMSG(_(e_trailing));
2612 return NULL;
2613 }
2614
2615 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2616 if (lp->ll_exp_name == NULL)
2617 {
2618 /* Report an invalid expression in braces, unless the
2619 * expression evaluation has been cancelled due to an
2620 * aborting error, an interrupt, or an exception. */
2621 if (!aborting() && !quiet)
2622 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002623 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 EMSG2(_(e_invarg2), name);
2625 return NULL;
2626 }
2627 }
2628 lp->ll_name = lp->ll_exp_name;
2629 }
2630 else
2631 lp->ll_name = name;
2632
2633 /* Without [idx] or .key we are done. */
2634 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2635 return p;
2636
2637 cc = *p;
2638 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002639 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 if (v == NULL && !quiet)
2641 EMSG2(_(e_undefvar), lp->ll_name);
2642 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002643 if (v == NULL)
2644 return NULL;
2645
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 /*
2647 * Loop until no more [idx] or .key is following.
2648 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002649 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002651 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2653 && !(lp->ll_tv->v_type == VAR_DICT
2654 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (!quiet)
2657 EMSG(_("E689: Can only index a List or Dictionary"));
2658 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002659 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002661 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 if (!quiet)
2663 EMSG(_("E708: [:] must come last"));
2664 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002665 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002666
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 len = -1;
2668 if (*p == '.')
2669 {
2670 key = p + 1;
2671 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2672 ;
2673 if (len == 0)
2674 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 if (!quiet)
2676 EMSG(_(e_emptykey));
2677 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 }
2679 p = key + len;
2680 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002681 else
2682 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002684 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 if (*p == ':')
2686 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002687 else
2688 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 empty1 = FALSE;
2690 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002692 if (get_tv_string_chk(&var1) == NULL)
2693 {
2694 /* not a number or string */
2695 clear_tv(&var1);
2696 return NULL;
2697 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 }
2699
2700 /* Optionally get the second index [ :expr]. */
2701 if (*p == ':')
2702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002706 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002707 if (!empty1)
2708 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002710 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (rettv != NULL && (rettv->v_type != VAR_LIST
2712 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (!quiet)
2715 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 if (!empty1)
2717 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 }
2720 p = skipwhite(p + 1);
2721 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 else
2724 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2727 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 if (!empty1)
2729 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002732 if (get_tv_string_chk(&var2) == NULL)
2733 {
2734 /* not a number or string */
2735 if (!empty1)
2736 clear_tv(&var1);
2737 clear_tv(&var2);
2738 return NULL;
2739 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002742 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002745
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002747 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 if (!quiet)
2749 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 if (!empty1)
2751 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 }
2756
2757 /* Skip to past ']'. */
2758 ++p;
2759 }
2760
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 {
2763 if (len == -1)
2764 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002766 key = get_tv_string_chk(&var1); /* is number or string */
2767 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 }
2772 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002773 lp->ll_list = NULL;
2774 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002775 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002776
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002777 /* When assigning to a scope dictionary check that a function and
2778 * variable name is valid (only variable name unless it is l: or
2779 * g: dictionary). Disallow overwriting a builtin function. */
2780 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002781 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002782 int prevval;
2783 int wrong;
2784
2785 if (len != -1)
2786 {
2787 prevval = key[len];
2788 key[len] = NUL;
2789 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002790 else
2791 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002792 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2793 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002794 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002795 || !valid_varname(key);
2796 if (len != -1)
2797 key[len] = prevval;
2798 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002799 return NULL;
2800 }
2801
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002802 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002803 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002804 /* Can't add "v:" variable. */
2805 if (lp->ll_dict == &vimvardict)
2806 {
2807 EMSG2(_(e_illvar), name);
2808 return NULL;
2809 }
2810
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002811 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002815 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 if (len == -1)
2817 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 }
2820 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 if (len == -1)
2825 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 p = NULL;
2828 break;
2829 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002830 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002831 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002832 return NULL;
2833
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 if (len == -1)
2835 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 }
2838 else
2839 {
2840 /*
2841 * Get the number and item for the only or first index of the List.
2842 */
2843 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002845 else
2846 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002847 lp->ll_n1 = (long)get_tv_number(&var1);
2848 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002849 clear_tv(&var1);
2850 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002851 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 lp->ll_list = lp->ll_tv->vval.v_list;
2853 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2854 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002856 if (lp->ll_n1 < 0)
2857 {
2858 lp->ll_n1 = 0;
2859 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2860 }
2861 }
2862 if (lp->ll_li == NULL)
2863 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002865 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002866 if (!quiet)
2867 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 }
2870
2871 /*
2872 * May need to find the item or absolute index for the second
2873 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 * When no index given: "lp->ll_empty2" is TRUE.
2875 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002879 lp->ll_n2 = (long)get_tv_number(&var2);
2880 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002881 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002883 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002885 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002886 {
2887 if (!quiet)
2888 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002890 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002892 }
2893
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2895 if (lp->ll_n1 < 0)
2896 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2897 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002898 {
2899 if (!quiet)
2900 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002902 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002903 }
2904
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002906 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002907 }
2908
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 return p;
2910}
2911
2912/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002913 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 */
2915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002916clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917{
2918 vim_free(lp->ll_exp_name);
2919 vim_free(lp->ll_newkey);
2920}
2921
2922/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002923 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002925 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 */
2927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002928set_var_lval(
2929 lval_T *lp,
2930 char_u *endp,
2931 typval_T *rettv,
2932 int copy,
2933 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934{
2935 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002936 listitem_T *ri;
2937 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938
2939 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002940 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002942 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943 cc = *endp;
2944 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002945 if (op != NULL && *op != '=')
2946 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002947 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002948
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002949 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002950 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002951 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002952 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002953 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002954 if ((di == NULL
2955 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2956 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2957 FALSE)))
2958 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002959 set_var(lp->ll_name, &tv, FALSE);
2960 clear_tv(&tv);
2961 }
2962 }
2963 else
2964 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002966 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002967 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002968 else if (tv_check_lock(lp->ll_newkey == NULL
2969 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002970 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002971 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972 else if (lp->ll_range)
2973 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002974 listitem_T *ll_li = lp->ll_li;
2975 int ll_n1 = lp->ll_n1;
2976
2977 /*
2978 * Check whether any of the list items is locked
2979 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002980 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002981 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002982 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002983 return;
2984 ri = ri->li_next;
2985 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2986 break;
2987 ll_li = ll_li->li_next;
2988 ++ll_n1;
2989 }
2990
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002991 /*
2992 * Assign the List values to the list items.
2993 */
2994 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002995 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002996 if (op != NULL && *op != '=')
2997 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2998 else
2999 {
3000 clear_tv(&lp->ll_li->li_tv);
3001 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3002 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003003 ri = ri->li_next;
3004 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3005 break;
3006 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003007 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003008 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003009 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003010 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003011 ri = NULL;
3012 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003013 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003014 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003015 lp->ll_li = lp->ll_li->li_next;
3016 ++lp->ll_n1;
3017 }
3018 if (ri != NULL)
3019 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003020 else if (lp->ll_empty2
3021 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003022 : lp->ll_n1 != lp->ll_n2)
3023 EMSG(_("E711: List value has not enough items"));
3024 }
3025 else
3026 {
3027 /*
3028 * Assign to a List or Dictionary item.
3029 */
3030 if (lp->ll_newkey != NULL)
3031 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003032 if (op != NULL && *op != '=')
3033 {
3034 EMSG2(_(e_letwrong), op);
3035 return;
3036 }
3037
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003038 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003039 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003040 if (di == NULL)
3041 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003042 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3043 {
3044 vim_free(di);
3045 return;
3046 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003047 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003048 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003049 else if (op != NULL && *op != '=')
3050 {
3051 tv_op(lp->ll_tv, rettv, op);
3052 return;
3053 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003054 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003055 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003056
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003057 /*
3058 * Assign the value to the variable or list item.
3059 */
3060 if (copy)
3061 copy_tv(rettv, lp->ll_tv);
3062 else
3063 {
3064 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003065 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003066 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003067 }
3068 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003069}
3070
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003071/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003072 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3073 * Returns OK or FAIL.
3074 */
3075 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003076tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003077{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003078 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003079 char_u numbuf[NUMBUFLEN];
3080 char_u *s;
3081
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003082 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3083 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3084 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003085 {
3086 switch (tv1->v_type)
3087 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003088 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003089 case VAR_DICT:
3090 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003091 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003092 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003093 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003094 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003095 break;
3096
3097 case VAR_LIST:
3098 if (*op != '+' || tv2->v_type != VAR_LIST)
3099 break;
3100 /* List += List */
3101 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3102 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3103 return OK;
3104
3105 case VAR_NUMBER:
3106 case VAR_STRING:
3107 if (tv2->v_type == VAR_LIST)
3108 break;
3109 if (*op == '+' || *op == '-')
3110 {
3111 /* nr += nr or nr -= nr*/
3112 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003113#ifdef FEAT_FLOAT
3114 if (tv2->v_type == VAR_FLOAT)
3115 {
3116 float_T f = n;
3117
3118 if (*op == '+')
3119 f += tv2->vval.v_float;
3120 else
3121 f -= tv2->vval.v_float;
3122 clear_tv(tv1);
3123 tv1->v_type = VAR_FLOAT;
3124 tv1->vval.v_float = f;
3125 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003126 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003127#endif
3128 {
3129 if (*op == '+')
3130 n += get_tv_number(tv2);
3131 else
3132 n -= get_tv_number(tv2);
3133 clear_tv(tv1);
3134 tv1->v_type = VAR_NUMBER;
3135 tv1->vval.v_number = n;
3136 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003137 }
3138 else
3139 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003140 if (tv2->v_type == VAR_FLOAT)
3141 break;
3142
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003143 /* str .= str */
3144 s = get_tv_string(tv1);
3145 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3146 clear_tv(tv1);
3147 tv1->v_type = VAR_STRING;
3148 tv1->vval.v_string = s;
3149 }
3150 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003151
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003152 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003153#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003154 {
3155 float_T f;
3156
3157 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3158 && tv2->v_type != VAR_NUMBER
3159 && tv2->v_type != VAR_STRING))
3160 break;
3161 if (tv2->v_type == VAR_FLOAT)
3162 f = tv2->vval.v_float;
3163 else
3164 f = get_tv_number(tv2);
3165 if (*op == '+')
3166 tv1->vval.v_float += f;
3167 else
3168 tv1->vval.v_float -= f;
3169 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003170#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003171 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003172 }
3173 }
3174
3175 EMSG2(_(e_letwrong), op);
3176 return FAIL;
3177}
3178
3179/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003180 * Evaluate the expression used in a ":for var in expr" command.
3181 * "arg" points to "var".
3182 * Set "*errp" to TRUE for an error, FALSE otherwise;
3183 * Return a pointer that holds the info. Null when there is an error.
3184 */
3185 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003186eval_for_line(
3187 char_u *arg,
3188 int *errp,
3189 char_u **nextcmdp,
3190 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191{
Bram Moolenaar33570922005-01-25 22:26:29 +00003192 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003194 typval_T tv;
3195 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003196
3197 *errp = TRUE; /* default: there is an error */
3198
Bram Moolenaar33570922005-01-25 22:26:29 +00003199 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003200 if (fi == NULL)
3201 return NULL;
3202
3203 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3204 if (expr == NULL)
3205 return fi;
3206
3207 expr = skipwhite(expr);
3208 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3209 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003210 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003211 return fi;
3212 }
3213
3214 if (skip)
3215 ++emsg_skip;
3216 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3217 {
3218 *errp = FALSE;
3219 if (!skip)
3220 {
3221 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003222 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003223 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003224 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003225 clear_tv(&tv);
3226 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003227 else if (l == NULL)
3228 {
3229 /* a null list is like an empty list: do nothing */
3230 clear_tv(&tv);
3231 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232 else
3233 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003234 /* No need to increment the refcount, it's already set for the
3235 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236 fi->fi_list = l;
3237 list_add_watch(l, &fi->fi_lw);
3238 fi->fi_lw.lw_item = l->lv_first;
3239 }
3240 }
3241 }
3242 if (skip)
3243 --emsg_skip;
3244
3245 return fi;
3246}
3247
3248/*
3249 * Use the first item in a ":for" list. Advance to the next.
3250 * Assign the values to the variable (list). "arg" points to the first one.
3251 * Return TRUE when a valid item was found, FALSE when at end of list or
3252 * something wrong.
3253 */
3254 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003255next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003256{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003257 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003258 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003259 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260
3261 item = fi->fi_lw.lw_item;
3262 if (item == NULL)
3263 result = FALSE;
3264 else
3265 {
3266 fi->fi_lw.lw_item = item->li_next;
3267 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3268 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3269 }
3270 return result;
3271}
3272
3273/*
3274 * Free the structure used to store info used by ":for".
3275 */
3276 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003277free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003278{
Bram Moolenaar33570922005-01-25 22:26:29 +00003279 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003281 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003282 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003284 list_unref(fi->fi_list);
3285 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003286 vim_free(fi);
3287}
3288
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3290
3291 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003292set_context_for_expression(
3293 expand_T *xp,
3294 char_u *arg,
3295 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296{
3297 int got_eq = FALSE;
3298 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003299 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301 if (cmdidx == CMD_let)
3302 {
3303 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003304 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003305 {
3306 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003307 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003308 {
3309 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003310 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003311 if (vim_iswhite(*p))
3312 break;
3313 }
3314 return;
3315 }
3316 }
3317 else
3318 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3319 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 while ((xp->xp_pattern = vim_strpbrk(arg,
3321 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3322 {
3323 c = *xp->xp_pattern;
3324 if (c == '&')
3325 {
3326 c = xp->xp_pattern[1];
3327 if (c == '&')
3328 {
3329 ++xp->xp_pattern;
3330 xp->xp_context = cmdidx != CMD_let || got_eq
3331 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3332 }
3333 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003334 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003336 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3337 xp->xp_pattern += 2;
3338
3339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 }
3341 else if (c == '$')
3342 {
3343 /* environment variable */
3344 xp->xp_context = EXPAND_ENV_VARS;
3345 }
3346 else if (c == '=')
3347 {
3348 got_eq = TRUE;
3349 xp->xp_context = EXPAND_EXPRESSION;
3350 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003351 else if (c == '#'
3352 && xp->xp_context == EXPAND_EXPRESSION)
3353 {
3354 /* Autoload function/variable contains '#'. */
3355 break;
3356 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003357 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 && xp->xp_context == EXPAND_FUNCTIONS
3359 && vim_strchr(xp->xp_pattern, '(') == NULL)
3360 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003361 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 break;
3363 }
3364 else if (cmdidx != CMD_let || got_eq)
3365 {
3366 if (c == '"') /* string */
3367 {
3368 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3369 if (c == '\\' && xp->xp_pattern[1] != NUL)
3370 ++xp->xp_pattern;
3371 xp->xp_context = EXPAND_NOTHING;
3372 }
3373 else if (c == '\'') /* literal string */
3374 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003375 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3377 /* skip */ ;
3378 xp->xp_context = EXPAND_NOTHING;
3379 }
3380 else if (c == '|')
3381 {
3382 if (xp->xp_pattern[1] == '|')
3383 {
3384 ++xp->xp_pattern;
3385 xp->xp_context = EXPAND_EXPRESSION;
3386 }
3387 else
3388 xp->xp_context = EXPAND_COMMANDS;
3389 }
3390 else
3391 xp->xp_context = EXPAND_EXPRESSION;
3392 }
3393 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003394 /* Doesn't look like something valid, expand as an expression
3395 * anyway. */
3396 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 arg = xp->xp_pattern;
3398 if (*arg != NUL)
3399 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3400 /* skip */ ;
3401 }
3402 xp->xp_pattern = arg;
3403}
3404
3405#endif /* FEAT_CMDL_COMPL */
3406
3407/*
3408 * ":1,25call func(arg1, arg2)" function call.
3409 */
3410 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003411ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412{
3413 char_u *arg = eap->arg;
3414 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003416 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003418 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 linenr_T lnum;
3420 int doesrange;
3421 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003422 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003423 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003425 if (eap->skip)
3426 {
3427 /* trans_function_name() doesn't work well when skipping, use eval0()
3428 * instead to skip to any following command, e.g. for:
3429 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003430 ++emsg_skip;
3431 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3432 clear_tv(&rettv);
3433 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003434 return;
3435 }
3436
Bram Moolenaar65639032016-03-16 21:40:30 +01003437 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003438 if (fudi.fd_newkey != NULL)
3439 {
3440 /* Still need to give an error message for missing key. */
3441 EMSG2(_(e_dictkey), fudi.fd_newkey);
3442 vim_free(fudi.fd_newkey);
3443 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003444 if (tofree == NULL)
3445 return;
3446
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003447 /* Increase refcount on dictionary, it could get deleted when evaluating
3448 * the arguments. */
3449 if (fudi.fd_dict != NULL)
3450 ++fudi.fd_dict->dv_refcount;
3451
Bram Moolenaar65639032016-03-16 21:40:30 +01003452 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3453 * contents. For VAR_PARTIAL get its partial, unless we already have one
3454 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003455 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003456 name = deref_func_name(tofree, &len,
3457 partial != NULL ? NULL : &partial, FALSE);
3458
Bram Moolenaar532c7802005-01-27 14:44:31 +00003459 /* Skip white space to allow ":call func ()". Not good, but required for
3460 * backward compatibility. */
3461 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003462 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463
3464 if (*startarg != '(')
3465 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003466 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 goto end;
3468 }
3469
3470 /*
3471 * When skipping, evaluate the function once, to find the end of the
3472 * arguments.
3473 * When the function takes a range, this is discovered after the first
3474 * call, and the loop is broken.
3475 */
3476 if (eap->skip)
3477 {
3478 ++emsg_skip;
3479 lnum = eap->line2; /* do it once, also with an invalid range */
3480 }
3481 else
3482 lnum = eap->line1;
3483 for ( ; lnum <= eap->line2; ++lnum)
3484 {
3485 if (!eap->skip && eap->addr_count > 0)
3486 {
3487 curwin->w_cursor.lnum = lnum;
3488 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003489#ifdef FEAT_VIRTUALEDIT
3490 curwin->w_cursor.coladd = 0;
3491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 }
3493 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003494 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003495 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003496 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 {
3498 failed = TRUE;
3499 break;
3500 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003501
3502 /* Handle a function returning a Funcref, Dictionary or List. */
3503 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3504 {
3505 failed = TRUE;
3506 break;
3507 }
3508
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003509 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 if (doesrange || eap->skip)
3511 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003512
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003514 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003515 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003516 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 if (aborting())
3518 break;
3519 }
3520 if (eap->skip)
3521 --emsg_skip;
3522
3523 if (!failed)
3524 {
3525 /* Check for trailing illegal characters and a following command. */
3526 if (!ends_excmd(*arg))
3527 {
3528 emsg_severe = TRUE;
3529 EMSG(_(e_trailing));
3530 }
3531 else
3532 eap->nextcmd = check_nextcmd(arg);
3533 }
3534
3535end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003536 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003537 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538}
3539
3540/*
3541 * ":unlet[!] var1 ... " command.
3542 */
3543 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003544ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003546 ex_unletlock(eap, eap->arg, 0);
3547}
3548
3549/*
3550 * ":lockvar" and ":unlockvar" commands
3551 */
3552 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003553ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003554{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003556 int deep = 2;
3557
3558 if (eap->forceit)
3559 deep = -1;
3560 else if (vim_isdigit(*arg))
3561 {
3562 deep = getdigits(&arg);
3563 arg = skipwhite(arg);
3564 }
3565
3566 ex_unletlock(eap, arg, deep);
3567}
3568
3569/*
3570 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3571 */
3572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003573ex_unletlock(
3574 exarg_T *eap,
3575 char_u *argstart,
3576 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003577{
3578 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003581 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
3583 do
3584 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003585 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003586 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003587 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003588 if (lv.ll_name == NULL)
3589 error = TRUE; /* error but continue parsing */
3590 if (name_end == NULL || (!vim_iswhite(*name_end)
3591 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003593 if (name_end != NULL)
3594 {
3595 emsg_severe = TRUE;
3596 EMSG(_(e_trailing));
3597 }
3598 if (!(eap->skip || error))
3599 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 break;
3601 }
3602
3603 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003604 {
3605 if (eap->cmdidx == CMD_unlet)
3606 {
3607 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3608 error = TRUE;
3609 }
3610 else
3611 {
3612 if (do_lock_var(&lv, name_end, deep,
3613 eap->cmdidx == CMD_lockvar) == FAIL)
3614 error = TRUE;
3615 }
3616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 if (!eap->skip)
3619 clear_lval(&lv);
3620
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 arg = skipwhite(name_end);
3622 } while (!ends_excmd(*arg));
3623
3624 eap->nextcmd = check_nextcmd(arg);
3625}
3626
Bram Moolenaar8c711452005-01-14 21:53:12 +00003627 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003628do_unlet_var(
3629 lval_T *lp,
3630 char_u *name_end,
3631 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003632{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003633 int ret = OK;
3634 int cc;
3635
3636 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003637 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003638 cc = *name_end;
3639 *name_end = NUL;
3640
3641 /* Normal name or expanded name. */
3642 if (check_changedtick(lp->ll_name))
3643 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003644 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003645 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003646 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003647 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003648 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003649 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003650 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003651 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003652 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003653 else if (lp->ll_range)
3654 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003655 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003656 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003657 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003658
3659 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3660 {
3661 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003662 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003663 return FAIL;
3664 ll_li = li;
3665 ++ll_n1;
3666 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003667
3668 /* Delete a range of List items. */
3669 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3670 {
3671 li = lp->ll_li->li_next;
3672 listitem_remove(lp->ll_list, lp->ll_li);
3673 lp->ll_li = li;
3674 ++lp->ll_n1;
3675 }
3676 }
3677 else
3678 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003680 /* unlet a List item. */
3681 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003682 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003683 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003684 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003685 }
3686
3687 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003688}
3689
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690/*
3691 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003692 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 */
3694 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003695do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696{
Bram Moolenaar33570922005-01-25 22:26:29 +00003697 hashtab_T *ht;
3698 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003699 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003700 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003701 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702
Bram Moolenaar33570922005-01-25 22:26:29 +00003703 ht = find_var_ht(name, &varname);
3704 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003706 if (ht == &globvarht)
3707 d = &globvardict;
3708 else if (current_funccal != NULL
3709 && ht == &current_funccal->l_vars.dv_hashtab)
3710 d = &current_funccal->l_vars;
3711 else if (ht == &compat_hashtab)
3712 d = &vimvardict;
3713 else
3714 {
3715 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3716 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3717 }
3718 if (d == NULL)
3719 {
3720 EMSG2(_(e_intern2), "do_unlet()");
3721 return FAIL;
3722 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003723 hi = hash_find(ht, varname);
3724 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003725 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003726 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003727 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003728 || var_check_ro(di->di_flags, name, FALSE)
3729 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003730 return FAIL;
3731
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003732 delete_var(ht, hi);
3733 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003736 if (forceit)
3737 return OK;
3738 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 return FAIL;
3740}
3741
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003742/*
3743 * Lock or unlock variable indicated by "lp".
3744 * "deep" is the levels to go (-1 for unlimited);
3745 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3746 */
3747 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003748do_lock_var(
3749 lval_T *lp,
3750 char_u *name_end,
3751 int deep,
3752 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003753{
3754 int ret = OK;
3755 int cc;
3756 dictitem_T *di;
3757
3758 if (deep == 0) /* nothing to do */
3759 return OK;
3760
3761 if (lp->ll_tv == NULL)
3762 {
3763 cc = *name_end;
3764 *name_end = NUL;
3765
3766 /* Normal name or expanded name. */
3767 if (check_changedtick(lp->ll_name))
3768 ret = FAIL;
3769 else
3770 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003771 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003772 if (di == NULL)
3773 ret = FAIL;
3774 else
3775 {
3776 if (lock)
3777 di->di_flags |= DI_FLAGS_LOCK;
3778 else
3779 di->di_flags &= ~DI_FLAGS_LOCK;
3780 item_lock(&di->di_tv, deep, lock);
3781 }
3782 }
3783 *name_end = cc;
3784 }
3785 else if (lp->ll_range)
3786 {
3787 listitem_T *li = lp->ll_li;
3788
3789 /* (un)lock a range of List items. */
3790 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3791 {
3792 item_lock(&li->li_tv, deep, lock);
3793 li = li->li_next;
3794 ++lp->ll_n1;
3795 }
3796 }
3797 else if (lp->ll_list != NULL)
3798 /* (un)lock a List item. */
3799 item_lock(&lp->ll_li->li_tv, deep, lock);
3800 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003801 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003802 item_lock(&lp->ll_di->di_tv, deep, lock);
3803
3804 return ret;
3805}
3806
3807/*
3808 * Lock or unlock an item. "deep" is nr of levels to go.
3809 */
3810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003811item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003812{
3813 static int recurse = 0;
3814 list_T *l;
3815 listitem_T *li;
3816 dict_T *d;
3817 hashitem_T *hi;
3818 int todo;
3819
3820 if (recurse >= DICT_MAXNEST)
3821 {
3822 EMSG(_("E743: variable nested too deep for (un)lock"));
3823 return;
3824 }
3825 if (deep == 0)
3826 return;
3827 ++recurse;
3828
3829 /* lock/unlock the item itself */
3830 if (lock)
3831 tv->v_lock |= VAR_LOCKED;
3832 else
3833 tv->v_lock &= ~VAR_LOCKED;
3834
3835 switch (tv->v_type)
3836 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003837 case VAR_UNKNOWN:
3838 case VAR_NUMBER:
3839 case VAR_STRING:
3840 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003841 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003842 case VAR_FLOAT:
3843 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003844 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003845 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003846 break;
3847
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003848 case VAR_LIST:
3849 if ((l = tv->vval.v_list) != NULL)
3850 {
3851 if (lock)
3852 l->lv_lock |= VAR_LOCKED;
3853 else
3854 l->lv_lock &= ~VAR_LOCKED;
3855 if (deep < 0 || deep > 1)
3856 /* recursive: lock/unlock the items the List contains */
3857 for (li = l->lv_first; li != NULL; li = li->li_next)
3858 item_lock(&li->li_tv, deep - 1, lock);
3859 }
3860 break;
3861 case VAR_DICT:
3862 if ((d = tv->vval.v_dict) != NULL)
3863 {
3864 if (lock)
3865 d->dv_lock |= VAR_LOCKED;
3866 else
3867 d->dv_lock &= ~VAR_LOCKED;
3868 if (deep < 0 || deep > 1)
3869 {
3870 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003871 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003872 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3873 {
3874 if (!HASHITEM_EMPTY(hi))
3875 {
3876 --todo;
3877 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3878 }
3879 }
3880 }
3881 }
3882 }
3883 --recurse;
3884}
3885
Bram Moolenaara40058a2005-07-11 22:42:07 +00003886/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003887 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3888 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003889 */
3890 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003891tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003892{
3893 return (tv->v_lock & VAR_LOCKED)
3894 || (tv->v_type == VAR_LIST
3895 && tv->vval.v_list != NULL
3896 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3897 || (tv->v_type == VAR_DICT
3898 && tv->vval.v_dict != NULL
3899 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3900}
3901
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3903/*
3904 * Delete all "menutrans_" variables.
3905 */
3906 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003907del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908{
Bram Moolenaar33570922005-01-25 22:26:29 +00003909 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003910 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911
Bram Moolenaar33570922005-01-25 22:26:29 +00003912 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003913 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003914 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003915 {
3916 if (!HASHITEM_EMPTY(hi))
3917 {
3918 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003919 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3920 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003921 }
3922 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003923 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924}
3925#endif
3926
3927#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3928
3929/*
3930 * Local string buffer for the next two functions to store a variable name
3931 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3932 * get_user_var_name().
3933 */
3934
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003935static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936
3937static char_u *varnamebuf = NULL;
3938static int varnamebuflen = 0;
3939
3940/*
3941 * Function to concatenate a prefix and a variable name.
3942 */
3943 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003944cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945{
3946 int len;
3947
3948 len = (int)STRLEN(name) + 3;
3949 if (len > varnamebuflen)
3950 {
3951 vim_free(varnamebuf);
3952 len += 10; /* some additional space */
3953 varnamebuf = alloc(len);
3954 if (varnamebuf == NULL)
3955 {
3956 varnamebuflen = 0;
3957 return NULL;
3958 }
3959 varnamebuflen = len;
3960 }
3961 *varnamebuf = prefix;
3962 varnamebuf[1] = ':';
3963 STRCPY(varnamebuf + 2, name);
3964 return varnamebuf;
3965}
3966
3967/*
3968 * Function given to ExpandGeneric() to obtain the list of user defined
3969 * (global/buffer/window/built-in) variable names.
3970 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003972get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003974 static long_u gdone;
3975 static long_u bdone;
3976 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003977#ifdef FEAT_WINDOWS
3978 static long_u tdone;
3979#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003980 static int vidx;
3981 static hashitem_T *hi;
3982 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983
3984 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003985 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003986 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003987#ifdef FEAT_WINDOWS
3988 tdone = 0;
3989#endif
3990 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003991
3992 /* Global variables */
3993 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003995 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003996 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003997 else
3998 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003999 while (HASHITEM_EMPTY(hi))
4000 ++hi;
4001 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4002 return cat_prefix_varname('g', hi->hi_key);
4003 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004005
4006 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004007 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004008 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004010 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004011 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004012 else
4013 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004014 while (HASHITEM_EMPTY(hi))
4015 ++hi;
4016 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004018 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004020 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 return (char_u *)"b:changedtick";
4022 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004023
4024 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004025 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004026 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004028 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004029 hi = ht->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 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004036
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004037#ifdef FEAT_WINDOWS
4038 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004039 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004040 if (tdone < ht->ht_used)
4041 {
4042 if (tdone++ == 0)
4043 hi = ht->ht_array;
4044 else
4045 ++hi;
4046 while (HASHITEM_EMPTY(hi))
4047 ++hi;
4048 return cat_prefix_varname('t', hi->hi_key);
4049 }
4050#endif
4051
Bram Moolenaar33570922005-01-25 22:26:29 +00004052 /* v: variables */
4053 if (vidx < VV_LEN)
4054 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055
4056 vim_free(varnamebuf);
4057 varnamebuf = NULL;
4058 varnamebuflen = 0;
4059 return NULL;
4060}
4061
4062#endif /* FEAT_CMDL_COMPL */
4063
4064/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004065 * Return TRUE if "pat" matches "text".
4066 * Does not use 'cpo' and always uses 'magic'.
4067 */
4068 static int
4069pattern_match(char_u *pat, char_u *text, int ic)
4070{
4071 int matches = FALSE;
4072 char_u *save_cpo;
4073 regmatch_T regmatch;
4074
4075 /* avoid 'l' flag in 'cpoptions' */
4076 save_cpo = p_cpo;
4077 p_cpo = (char_u *)"";
4078 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4079 if (regmatch.regprog != NULL)
4080 {
4081 regmatch.rm_ic = ic;
4082 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4083 vim_regfree(regmatch.regprog);
4084 }
4085 p_cpo = save_cpo;
4086 return matches;
4087}
4088
4089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 * types for expressions.
4091 */
4092typedef enum
4093{
4094 TYPE_UNKNOWN = 0
4095 , TYPE_EQUAL /* == */
4096 , TYPE_NEQUAL /* != */
4097 , TYPE_GREATER /* > */
4098 , TYPE_GEQUAL /* >= */
4099 , TYPE_SMALLER /* < */
4100 , TYPE_SEQUAL /* <= */
4101 , TYPE_MATCH /* =~ */
4102 , TYPE_NOMATCH /* !~ */
4103} exptype_T;
4104
4105/*
4106 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004107 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4109 */
4110
4111/*
4112 * Handle zero level expression.
4113 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004114 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004115 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 * Return OK or FAIL.
4117 */
4118 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004119eval0(
4120 char_u *arg,
4121 typval_T *rettv,
4122 char_u **nextcmd,
4123 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124{
4125 int ret;
4126 char_u *p;
4127
4128 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004129 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 if (ret == FAIL || !ends_excmd(*p))
4131 {
4132 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 /*
4135 * Report the invalid expression unless the expression evaluation has
4136 * been cancelled due to an aborting error, an interrupt, or an
4137 * exception.
4138 */
4139 if (!aborting())
4140 EMSG2(_(e_invexpr2), arg);
4141 ret = FAIL;
4142 }
4143 if (nextcmd != NULL)
4144 *nextcmd = check_nextcmd(p);
4145
4146 return ret;
4147}
4148
4149/*
4150 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004151 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 *
4153 * "arg" must point to the first non-white of the expression.
4154 * "arg" is advanced to the next non-white after the recognized expression.
4155 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004156 * Note: "rettv.v_lock" is not set.
4157 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 * Return OK or FAIL.
4159 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004160 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004161eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162{
4163 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004164 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165
4166 /*
4167 * Get the first variable.
4168 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 return FAIL;
4171
4172 if ((*arg)[0] == '?')
4173 {
4174 result = FALSE;
4175 if (evaluate)
4176 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004177 int error = FALSE;
4178
4179 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004181 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004182 if (error)
4183 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
4185
4186 /*
4187 * Get the second variable.
4188 */
4189 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 return FAIL;
4192
4193 /*
4194 * Check for the ":".
4195 */
4196 if ((*arg)[0] != ':')
4197 {
4198 EMSG(_("E109: Missing ':' after '?'"));
4199 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004200 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 return FAIL;
4202 }
4203
4204 /*
4205 * Get the third variable.
4206 */
4207 *arg = skipwhite(*arg + 1);
4208 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4209 {
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 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 }
4217
4218 return OK;
4219}
4220
4221/*
4222 * Handle first level expression:
4223 * expr2 || expr2 || expr2 logical OR
4224 *
4225 * "arg" must point to the first non-white of the expression.
4226 * "arg" is advanced to the next non-white after the recognized expression.
4227 *
4228 * Return OK or FAIL.
4229 */
4230 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004231eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232{
Bram Moolenaar33570922005-01-25 22:26:29 +00004233 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 long result;
4235 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004236 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237
4238 /*
4239 * Get the first variable.
4240 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004241 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 return FAIL;
4243
4244 /*
4245 * Repeat until there is no following "||".
4246 */
4247 first = TRUE;
4248 result = FALSE;
4249 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4250 {
4251 if (evaluate && first)
4252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004253 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004255 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004256 if (error)
4257 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 first = FALSE;
4259 }
4260
4261 /*
4262 * Get the second variable.
4263 */
4264 *arg = skipwhite(*arg + 2);
4265 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4266 return FAIL;
4267
4268 /*
4269 * Compute the result.
4270 */
4271 if (evaluate && !result)
4272 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004273 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004275 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004276 if (error)
4277 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 }
4279 if (evaluate)
4280 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004281 rettv->v_type = VAR_NUMBER;
4282 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
4284 }
4285
4286 return OK;
4287}
4288
4289/*
4290 * Handle second level expression:
4291 * expr3 && expr3 && expr3 logical AND
4292 *
4293 * "arg" must point to the first non-white of the expression.
4294 * "arg" is advanced to the next non-white after the recognized expression.
4295 *
4296 * Return OK or FAIL.
4297 */
4298 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004299eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300{
Bram Moolenaar33570922005-01-25 22:26:29 +00004301 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 long result;
4303 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004304 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
4306 /*
4307 * Get the first variable.
4308 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004309 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 return FAIL;
4311
4312 /*
4313 * Repeat until there is no following "&&".
4314 */
4315 first = TRUE;
4316 result = TRUE;
4317 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4318 {
4319 if (evaluate && first)
4320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004323 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004324 if (error)
4325 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 first = FALSE;
4327 }
4328
4329 /*
4330 * Get the second variable.
4331 */
4332 *arg = skipwhite(*arg + 2);
4333 if (eval4(arg, &var2, evaluate && result) == FAIL)
4334 return FAIL;
4335
4336 /*
4337 * Compute the result.
4338 */
4339 if (evaluate && result)
4340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004341 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004343 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004344 if (error)
4345 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 }
4347 if (evaluate)
4348 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004349 rettv->v_type = VAR_NUMBER;
4350 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
4352 }
4353
4354 return OK;
4355}
4356
4357/*
4358 * Handle third level expression:
4359 * var1 == var2
4360 * var1 =~ var2
4361 * var1 != var2
4362 * var1 !~ var2
4363 * var1 > var2
4364 * var1 >= var2
4365 * var1 < var2
4366 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004367 * var1 is var2
4368 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 *
4370 * "arg" must point to the first non-white of the expression.
4371 * "arg" is advanced to the next non-white after the recognized expression.
4372 *
4373 * Return OK or FAIL.
4374 */
4375 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004376eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377{
Bram Moolenaar33570922005-01-25 22:26:29 +00004378 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 char_u *p;
4380 int i;
4381 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004382 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004384 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 char_u *s1, *s2;
4386 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388
4389 /*
4390 * Get the first variable.
4391 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004392 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 return FAIL;
4394
4395 p = *arg;
4396 switch (p[0])
4397 {
4398 case '=': if (p[1] == '=')
4399 type = TYPE_EQUAL;
4400 else if (p[1] == '~')
4401 type = TYPE_MATCH;
4402 break;
4403 case '!': if (p[1] == '=')
4404 type = TYPE_NEQUAL;
4405 else if (p[1] == '~')
4406 type = TYPE_NOMATCH;
4407 break;
4408 case '>': if (p[1] != '=')
4409 {
4410 type = TYPE_GREATER;
4411 len = 1;
4412 }
4413 else
4414 type = TYPE_GEQUAL;
4415 break;
4416 case '<': if (p[1] != '=')
4417 {
4418 type = TYPE_SMALLER;
4419 len = 1;
4420 }
4421 else
4422 type = TYPE_SEQUAL;
4423 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004424 case 'i': if (p[1] == 's')
4425 {
4426 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4427 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004428 i = p[len];
4429 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004430 {
4431 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4432 type_is = TRUE;
4433 }
4434 }
4435 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 }
4437
4438 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004439 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 */
4441 if (type != TYPE_UNKNOWN)
4442 {
4443 /* extra question mark appended: ignore case */
4444 if (p[len] == '?')
4445 {
4446 ic = TRUE;
4447 ++len;
4448 }
4449 /* extra '#' appended: match case */
4450 else if (p[len] == '#')
4451 {
4452 ic = FALSE;
4453 ++len;
4454 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004455 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 else
4457 ic = p_ic;
4458
4459 /*
4460 * Get the second variable.
4461 */
4462 *arg = skipwhite(p + len);
4463 if (eval5(arg, &var2, evaluate) == FAIL)
4464 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004465 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 return FAIL;
4467 }
4468
4469 if (evaluate)
4470 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004471 if (type_is && rettv->v_type != var2.v_type)
4472 {
4473 /* For "is" a different type always means FALSE, for "notis"
4474 * it means TRUE. */
4475 n1 = (type == TYPE_NEQUAL);
4476 }
4477 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4478 {
4479 if (type_is)
4480 {
4481 n1 = (rettv->v_type == var2.v_type
4482 && rettv->vval.v_list == var2.vval.v_list);
4483 if (type == TYPE_NEQUAL)
4484 n1 = !n1;
4485 }
4486 else if (rettv->v_type != var2.v_type
4487 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4488 {
4489 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004490 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004491 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004492 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004493 clear_tv(rettv);
4494 clear_tv(&var2);
4495 return FAIL;
4496 }
4497 else
4498 {
4499 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004500 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4501 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004502 if (type == TYPE_NEQUAL)
4503 n1 = !n1;
4504 }
4505 }
4506
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004507 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4508 {
4509 if (type_is)
4510 {
4511 n1 = (rettv->v_type == var2.v_type
4512 && rettv->vval.v_dict == var2.vval.v_dict);
4513 if (type == TYPE_NEQUAL)
4514 n1 = !n1;
4515 }
4516 else if (rettv->v_type != var2.v_type
4517 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4518 {
4519 if (rettv->v_type != var2.v_type)
4520 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4521 else
4522 EMSG(_("E736: Invalid operation for Dictionary"));
4523 clear_tv(rettv);
4524 clear_tv(&var2);
4525 return FAIL;
4526 }
4527 else
4528 {
4529 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004530 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4531 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004532 if (type == TYPE_NEQUAL)
4533 n1 = !n1;
4534 }
4535 }
4536
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004537 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4538 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004539 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004540 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004541 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004542 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004543 clear_tv(rettv);
4544 clear_tv(&var2);
4545 return FAIL;
4546 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004547 if ((rettv->v_type == VAR_PARTIAL
4548 && rettv->vval.v_partial == NULL)
4549 || (var2.v_type == VAR_PARTIAL
4550 && var2.vval.v_partial == NULL))
4551 /* when a partial is NULL assume not equal */
4552 n1 = FALSE;
4553 else if (type_is)
4554 {
4555 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
4556 /* strings are considered the same if their value is
4557 * the same */
4558 n1 = tv_equal(rettv, &var2, ic, FALSE);
4559 else if (rettv->v_type == VAR_PARTIAL
4560 && var2.v_type == VAR_PARTIAL)
4561 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
4562 else
4563 n1 = FALSE;
4564 }
4565 else
4566 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004567 if (type == TYPE_NEQUAL)
4568 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004569 }
4570
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004571#ifdef FEAT_FLOAT
4572 /*
4573 * If one of the two variables is a float, compare as a float.
4574 * When using "=~" or "!~", always compare as string.
4575 */
4576 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4577 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4578 {
4579 float_T f1, f2;
4580
4581 if (rettv->v_type == VAR_FLOAT)
4582 f1 = rettv->vval.v_float;
4583 else
4584 f1 = get_tv_number(rettv);
4585 if (var2.v_type == VAR_FLOAT)
4586 f2 = var2.vval.v_float;
4587 else
4588 f2 = get_tv_number(&var2);
4589 n1 = FALSE;
4590 switch (type)
4591 {
4592 case TYPE_EQUAL: n1 = (f1 == f2); break;
4593 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4594 case TYPE_GREATER: n1 = (f1 > f2); break;
4595 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4596 case TYPE_SMALLER: n1 = (f1 < f2); break;
4597 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4598 case TYPE_UNKNOWN:
4599 case TYPE_MATCH:
4600 case TYPE_NOMATCH: break; /* avoid gcc warning */
4601 }
4602 }
4603#endif
4604
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 /*
4606 * If one of the two variables is a number, compare as a number.
4607 * When using "=~" or "!~", always compare as string.
4608 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004609 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4611 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004612 n1 = get_tv_number(rettv);
4613 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 switch (type)
4615 {
4616 case TYPE_EQUAL: n1 = (n1 == n2); break;
4617 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4618 case TYPE_GREATER: n1 = (n1 > n2); break;
4619 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4620 case TYPE_SMALLER: n1 = (n1 < n2); break;
4621 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4622 case TYPE_UNKNOWN:
4623 case TYPE_MATCH:
4624 case TYPE_NOMATCH: break; /* avoid gcc warning */
4625 }
4626 }
4627 else
4628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004629 s1 = get_tv_string_buf(rettv, buf1);
4630 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4632 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4633 else
4634 i = 0;
4635 n1 = FALSE;
4636 switch (type)
4637 {
4638 case TYPE_EQUAL: n1 = (i == 0); break;
4639 case TYPE_NEQUAL: n1 = (i != 0); break;
4640 case TYPE_GREATER: n1 = (i > 0); break;
4641 case TYPE_GEQUAL: n1 = (i >= 0); break;
4642 case TYPE_SMALLER: n1 = (i < 0); break;
4643 case TYPE_SEQUAL: n1 = (i <= 0); break;
4644
4645 case TYPE_MATCH:
4646 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004647 n1 = pattern_match(s2, s1, ic);
4648 if (type == TYPE_NOMATCH)
4649 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 break;
4651
4652 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4653 }
4654 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004655 clear_tv(rettv);
4656 clear_tv(&var2);
4657 rettv->v_type = VAR_NUMBER;
4658 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 }
4660 }
4661
4662 return OK;
4663}
4664
4665/*
4666 * Handle fourth level expression:
4667 * + number addition
4668 * - number subtraction
4669 * . string concatenation
4670 *
4671 * "arg" must point to the first non-white of the expression.
4672 * "arg" is advanced to the next non-white after the recognized expression.
4673 *
4674 * Return OK or FAIL.
4675 */
4676 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004677eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678{
Bram Moolenaar33570922005-01-25 22:26:29 +00004679 typval_T var2;
4680 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004682 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004683#ifdef FEAT_FLOAT
4684 float_T f1 = 0, f2 = 0;
4685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 char_u *s1, *s2;
4687 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4688 char_u *p;
4689
4690 /*
4691 * Get the first variable.
4692 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004693 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 return FAIL;
4695
4696 /*
4697 * Repeat computing, until no '+', '-' or '.' is following.
4698 */
4699 for (;;)
4700 {
4701 op = **arg;
4702 if (op != '+' && op != '-' && op != '.')
4703 break;
4704
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004705 if ((op != '+' || rettv->v_type != VAR_LIST)
4706#ifdef FEAT_FLOAT
4707 && (op == '.' || rettv->v_type != VAR_FLOAT)
4708#endif
4709 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004710 {
4711 /* For "list + ...", an illegal use of the first operand as
4712 * a number cannot be determined before evaluating the 2nd
4713 * operand: if this is also a list, all is ok.
4714 * For "something . ...", "something - ..." or "non-list + ...",
4715 * we know that the first operand needs to be a string or number
4716 * without evaluating the 2nd operand. So check before to avoid
4717 * side effects after an error. */
4718 if (evaluate && get_tv_string_chk(rettv) == NULL)
4719 {
4720 clear_tv(rettv);
4721 return FAIL;
4722 }
4723 }
4724
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 /*
4726 * Get the second variable.
4727 */
4728 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004729 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004731 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 return FAIL;
4733 }
4734
4735 if (evaluate)
4736 {
4737 /*
4738 * Compute the result.
4739 */
4740 if (op == '.')
4741 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004742 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4743 s2 = get_tv_string_buf_chk(&var2, buf2);
4744 if (s2 == NULL) /* type error ? */
4745 {
4746 clear_tv(rettv);
4747 clear_tv(&var2);
4748 return FAIL;
4749 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004750 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004751 clear_tv(rettv);
4752 rettv->v_type = VAR_STRING;
4753 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004755 else if (op == '+' && rettv->v_type == VAR_LIST
4756 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004757 {
4758 /* concatenate Lists */
4759 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4760 &var3) == FAIL)
4761 {
4762 clear_tv(rettv);
4763 clear_tv(&var2);
4764 return FAIL;
4765 }
4766 clear_tv(rettv);
4767 *rettv = var3;
4768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 else
4770 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004771 int error = FALSE;
4772
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004773#ifdef FEAT_FLOAT
4774 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004775 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004776 f1 = rettv->vval.v_float;
4777 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004778 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004779 else
4780#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004781 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004782 n1 = get_tv_number_chk(rettv, &error);
4783 if (error)
4784 {
4785 /* This can only happen for "list + non-list". For
4786 * "non-list + ..." or "something - ...", we returned
4787 * before evaluating the 2nd operand. */
4788 clear_tv(rettv);
4789 return FAIL;
4790 }
4791#ifdef FEAT_FLOAT
4792 if (var2.v_type == VAR_FLOAT)
4793 f1 = n1;
4794#endif
4795 }
4796#ifdef FEAT_FLOAT
4797 if (var2.v_type == VAR_FLOAT)
4798 {
4799 f2 = var2.vval.v_float;
4800 n2 = 0;
4801 }
4802 else
4803#endif
4804 {
4805 n2 = get_tv_number_chk(&var2, &error);
4806 if (error)
4807 {
4808 clear_tv(rettv);
4809 clear_tv(&var2);
4810 return FAIL;
4811 }
4812#ifdef FEAT_FLOAT
4813 if (rettv->v_type == VAR_FLOAT)
4814 f2 = n2;
4815#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004816 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004817 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004818
4819#ifdef FEAT_FLOAT
4820 /* If there is a float on either side the result is a float. */
4821 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4822 {
4823 if (op == '+')
4824 f1 = f1 + f2;
4825 else
4826 f1 = f1 - f2;
4827 rettv->v_type = VAR_FLOAT;
4828 rettv->vval.v_float = f1;
4829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004831#endif
4832 {
4833 if (op == '+')
4834 n1 = n1 + n2;
4835 else
4836 n1 = n1 - n2;
4837 rettv->v_type = VAR_NUMBER;
4838 rettv->vval.v_number = n1;
4839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004841 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 }
4843 }
4844 return OK;
4845}
4846
4847/*
4848 * Handle fifth level expression:
4849 * * number multiplication
4850 * / number division
4851 * % number modulo
4852 *
4853 * "arg" must point to the first non-white of the expression.
4854 * "arg" is advanced to the next non-white after the recognized expression.
4855 *
4856 * Return OK or FAIL.
4857 */
4858 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004859eval6(
4860 char_u **arg,
4861 typval_T *rettv,
4862 int evaluate,
4863 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864{
Bram Moolenaar33570922005-01-25 22:26:29 +00004865 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004867 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004868#ifdef FEAT_FLOAT
4869 int use_float = FALSE;
4870 float_T f1 = 0, f2;
4871#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004872 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873
4874 /*
4875 * Get the first variable.
4876 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004877 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 return FAIL;
4879
4880 /*
4881 * Repeat computing, until no '*', '/' or '%' is following.
4882 */
4883 for (;;)
4884 {
4885 op = **arg;
4886 if (op != '*' && op != '/' && op != '%')
4887 break;
4888
4889 if (evaluate)
4890 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004891#ifdef FEAT_FLOAT
4892 if (rettv->v_type == VAR_FLOAT)
4893 {
4894 f1 = rettv->vval.v_float;
4895 use_float = TRUE;
4896 n1 = 0;
4897 }
4898 else
4899#endif
4900 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004901 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004902 if (error)
4903 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 }
4905 else
4906 n1 = 0;
4907
4908 /*
4909 * Get the second variable.
4910 */
4911 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004912 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 return FAIL;
4914
4915 if (evaluate)
4916 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004917#ifdef FEAT_FLOAT
4918 if (var2.v_type == VAR_FLOAT)
4919 {
4920 if (!use_float)
4921 {
4922 f1 = n1;
4923 use_float = TRUE;
4924 }
4925 f2 = var2.vval.v_float;
4926 n2 = 0;
4927 }
4928 else
4929#endif
4930 {
4931 n2 = get_tv_number_chk(&var2, &error);
4932 clear_tv(&var2);
4933 if (error)
4934 return FAIL;
4935#ifdef FEAT_FLOAT
4936 if (use_float)
4937 f2 = n2;
4938#endif
4939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940
4941 /*
4942 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004943 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004945#ifdef FEAT_FLOAT
4946 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004948 if (op == '*')
4949 f1 = f1 * f2;
4950 else if (op == '/')
4951 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004952# ifdef VMS
4953 /* VMS crashes on divide by zero, work around it */
4954 if (f2 == 0.0)
4955 {
4956 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004957 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004958 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004959 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004960 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004961 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004962 }
4963 else
4964 f1 = f1 / f2;
4965# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004966 /* We rely on the floating point library to handle divide
4967 * by zero to result in "inf" and not a crash. */
4968 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004969# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004972 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004973 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004974 return FAIL;
4975 }
4976 rettv->v_type = VAR_FLOAT;
4977 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 }
4979 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004982 if (op == '*')
4983 n1 = n1 * n2;
4984 else if (op == '/')
4985 {
4986 if (n2 == 0) /* give an error message? */
4987 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004988#ifdef FEAT_NUM64
4989 if (n1 == 0)
4990 n1 = -0x7fffffffffffffff - 1; /* similar to NaN */
4991 else if (n1 < 0)
4992 n1 = -0x7fffffffffffffff;
4993 else
4994 n1 = 0x7fffffffffffffff;
4995#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004996 if (n1 == 0)
4997 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4998 else if (n1 < 0)
4999 n1 = -0x7fffffffL;
5000 else
5001 n1 = 0x7fffffffL;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005002#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005003 }
5004 else
5005 n1 = n1 / n2;
5006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005008 {
5009 if (n2 == 0) /* give an error message? */
5010 n1 = 0;
5011 else
5012 n1 = n1 % n2;
5013 }
5014 rettv->v_type = VAR_NUMBER;
5015 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
5018 }
5019
5020 return OK;
5021}
5022
5023/*
5024 * Handle sixth level expression:
5025 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005026 * "string" string constant
5027 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 * &option-name option value
5029 * @r register contents
5030 * identifier variable value
5031 * function() function call
5032 * $VAR environment variable
5033 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005034 * [expr, expr] List
5035 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 *
5037 * Also handle:
5038 * ! in front logical NOT
5039 * - in front unary minus
5040 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041 * trailing [] subscript in String or List
5042 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 *
5044 * "arg" must point to the first non-white of the expression.
5045 * "arg" is advanced to the next non-white after the recognized expression.
5046 *
5047 * Return OK or FAIL.
5048 */
5049 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005050eval7(
5051 char_u **arg,
5052 typval_T *rettv,
5053 int evaluate,
5054 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005056 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 int len;
5058 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 char_u *start_leader, *end_leader;
5060 int ret = OK;
5061 char_u *alias;
5062
5063 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005064 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005065 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005067 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068
5069 /*
5070 * Skip '!' and '-' characters. They are handled later.
5071 */
5072 start_leader = *arg;
5073 while (**arg == '!' || **arg == '-' || **arg == '+')
5074 *arg = skipwhite(*arg + 1);
5075 end_leader = *arg;
5076
5077 switch (**arg)
5078 {
5079 /*
5080 * Number constant.
5081 */
5082 case '0':
5083 case '1':
5084 case '2':
5085 case '3':
5086 case '4':
5087 case '5':
5088 case '6':
5089 case '7':
5090 case '8':
5091 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005092 {
5093#ifdef FEAT_FLOAT
5094 char_u *p = skipdigits(*arg + 1);
5095 int get_float = FALSE;
5096
5097 /* We accept a float when the format matches
5098 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005099 * strict to avoid backwards compatibility problems.
5100 * Don't look for a float after the "." operator, so that
5101 * ":let vers = 1.2.3" doesn't fail. */
5102 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005104 get_float = TRUE;
5105 p = skipdigits(p + 2);
5106 if (*p == 'e' || *p == 'E')
5107 {
5108 ++p;
5109 if (*p == '-' || *p == '+')
5110 ++p;
5111 if (!vim_isdigit(*p))
5112 get_float = FALSE;
5113 else
5114 p = skipdigits(p + 1);
5115 }
5116 if (ASCII_ISALPHA(*p) || *p == '.')
5117 get_float = FALSE;
5118 }
5119 if (get_float)
5120 {
5121 float_T f;
5122
5123 *arg += string2float(*arg, &f);
5124 if (evaluate)
5125 {
5126 rettv->v_type = VAR_FLOAT;
5127 rettv->vval.v_float = f;
5128 }
5129 }
5130 else
5131#endif
5132 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005133 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005134 *arg += len;
5135 if (evaluate)
5136 {
5137 rettv->v_type = VAR_NUMBER;
5138 rettv->vval.v_number = n;
5139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 }
5141 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143
5144 /*
5145 * String constant: "string".
5146 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005147 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 break;
5149
5150 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005151 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005153 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005154 break;
5155
5156 /*
5157 * List: [expr, expr]
5158 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005159 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 break;
5161
5162 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02005163 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00005164 * Dictionary: {key: val, key: val}
5165 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02005166 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
5167 if (ret == NOTDONE)
5168 ret = get_dict_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005169 break;
5170
5171 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005172 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005174 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 break;
5176
5177 /*
5178 * Environment variable: $VAR.
5179 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005180 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 break;
5182
5183 /*
5184 * Register contents: @r.
5185 */
5186 case '@': ++*arg;
5187 if (evaluate)
5188 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005189 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005190 rettv->vval.v_string = get_reg_contents(**arg,
5191 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 }
5193 if (**arg != NUL)
5194 ++*arg;
5195 break;
5196
5197 /*
5198 * nested expression: (expression).
5199 */
5200 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005201 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 if (**arg == ')')
5203 ++*arg;
5204 else if (ret == OK)
5205 {
5206 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005207 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 ret = FAIL;
5209 }
5210 break;
5211
Bram Moolenaar8c711452005-01-14 21:53:12 +00005212 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 break;
5214 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215
5216 if (ret == NOTDONE)
5217 {
5218 /*
5219 * Must be a variable or function name.
5220 * Can also be a curly-braces kind of name: {expr}.
5221 */
5222 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005223 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005224 if (alias != NULL)
5225 s = alias;
5226
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005227 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005228 ret = FAIL;
5229 else
5230 {
5231 if (**arg == '(') /* recursive! */
5232 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005233 partial_T *partial;
5234
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235 /* If "s" is the name of a variable of type VAR_FUNC
5236 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005237 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238
5239 /* Invoke the function. */
5240 ret = get_func_tv(s, len, rettv, arg,
5241 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005242 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005243
5244 /* If evaluate is FALSE rettv->v_type was not set in
5245 * get_func_tv, but it's needed in handle_subscript() to parse
5246 * what follows. So set it here. */
5247 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5248 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02005249 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01005250 rettv->v_type = VAR_FUNC;
5251 }
5252
Bram Moolenaar8c711452005-01-14 21:53:12 +00005253 /* Stop the expression evaluation when immediately
5254 * aborting on error, or when an interrupt occurred or
5255 * an exception was thrown but not caught. */
5256 if (aborting())
5257 {
5258 if (ret == OK)
5259 clear_tv(rettv);
5260 ret = FAIL;
5261 }
5262 }
5263 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005264 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005265 else
5266 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005268 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005269 }
5270
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 *arg = skipwhite(*arg);
5272
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005273 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5274 * expr(expr). */
5275 if (ret == OK)
5276 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277
5278 /*
5279 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5280 */
5281 if (ret == OK && evaluate && end_leader > start_leader)
5282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005283 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005284 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005285#ifdef FEAT_FLOAT
5286 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005287
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005288 if (rettv->v_type == VAR_FLOAT)
5289 f = rettv->vval.v_float;
5290 else
5291#endif
5292 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005293 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005295 clear_tv(rettv);
5296 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005298 else
5299 {
5300 while (end_leader > start_leader)
5301 {
5302 --end_leader;
5303 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005304 {
5305#ifdef FEAT_FLOAT
5306 if (rettv->v_type == VAR_FLOAT)
5307 f = !f;
5308 else
5309#endif
5310 val = !val;
5311 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005312 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005313 {
5314#ifdef FEAT_FLOAT
5315 if (rettv->v_type == VAR_FLOAT)
5316 f = -f;
5317 else
5318#endif
5319 val = -val;
5320 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005321 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005322#ifdef FEAT_FLOAT
5323 if (rettv->v_type == VAR_FLOAT)
5324 {
5325 clear_tv(rettv);
5326 rettv->vval.v_float = f;
5327 }
5328 else
5329#endif
5330 {
5331 clear_tv(rettv);
5332 rettv->v_type = VAR_NUMBER;
5333 rettv->vval.v_number = val;
5334 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 }
5337
5338 return ret;
5339}
5340
5341/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005342 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5343 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005344 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5345 */
5346 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005347eval_index(
5348 char_u **arg,
5349 typval_T *rettv,
5350 int evaluate,
5351 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352{
5353 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005354 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005356 long len = -1;
5357 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005359 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360
Bram Moolenaara03f2332016-02-06 18:09:59 +01005361 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005363 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005364 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005365 if (verbose)
5366 EMSG(_("E695: Cannot index a Funcref"));
5367 return FAIL;
5368 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005369#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005370 if (verbose)
5371 EMSG(_(e_float_as_string));
5372 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005373#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005374 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005375 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005376 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005377 if (verbose)
5378 EMSG(_("E909: Cannot index a special variable"));
5379 return FAIL;
5380 case VAR_UNKNOWN:
5381 if (evaluate)
5382 return FAIL;
5383 /* FALLTHROUGH */
5384
5385 case VAR_STRING:
5386 case VAR_NUMBER:
5387 case VAR_LIST:
5388 case VAR_DICT:
5389 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005390 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005392 init_tv(&var1);
5393 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005394 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005396 /*
5397 * dict.name
5398 */
5399 key = *arg + 1;
5400 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5401 ;
5402 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005404 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005405 }
5406 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005408 /*
5409 * something[idx]
5410 *
5411 * Get the (first) variable from inside the [].
5412 */
5413 *arg = skipwhite(*arg + 1);
5414 if (**arg == ':')
5415 empty1 = TRUE;
5416 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5417 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005418 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5419 {
5420 /* not a number or string */
5421 clear_tv(&var1);
5422 return FAIL;
5423 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005424
5425 /*
5426 * Get the second variable from inside the [:].
5427 */
5428 if (**arg == ':')
5429 {
5430 range = TRUE;
5431 *arg = skipwhite(*arg + 1);
5432 if (**arg == ']')
5433 empty2 = TRUE;
5434 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005436 if (!empty1)
5437 clear_tv(&var1);
5438 return FAIL;
5439 }
5440 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5441 {
5442 /* not a number or string */
5443 if (!empty1)
5444 clear_tv(&var1);
5445 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005446 return FAIL;
5447 }
5448 }
5449
5450 /* Check for the ']'. */
5451 if (**arg != ']')
5452 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005453 if (verbose)
5454 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005455 clear_tv(&var1);
5456 if (range)
5457 clear_tv(&var2);
5458 return FAIL;
5459 }
5460 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 }
5462
5463 if (evaluate)
5464 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005465 n1 = 0;
5466 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005468 n1 = get_tv_number(&var1);
5469 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 }
5471 if (range)
5472 {
5473 if (empty2)
5474 n2 = -1;
5475 else
5476 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005477 n2 = get_tv_number(&var2);
5478 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 }
5480 }
5481
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005482 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005483 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005484 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005485 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005486 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005487 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005488 case VAR_SPECIAL:
5489 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005490 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005491 break; /* not evaluating, skipping over subscript */
5492
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493 case VAR_NUMBER:
5494 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005495 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005496 len = (long)STRLEN(s);
5497 if (range)
5498 {
5499 /* The resulting variable is a substring. If the indexes
5500 * are out of range the result is empty. */
5501 if (n1 < 0)
5502 {
5503 n1 = len + n1;
5504 if (n1 < 0)
5505 n1 = 0;
5506 }
5507 if (n2 < 0)
5508 n2 = len + n2;
5509 else if (n2 >= len)
5510 n2 = len;
5511 if (n1 >= len || n2 < 0 || n1 > n2)
5512 s = NULL;
5513 else
5514 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5515 }
5516 else
5517 {
5518 /* The resulting variable is a string of a single
5519 * character. If the index is too big or negative the
5520 * result is empty. */
5521 if (n1 >= len || n1 < 0)
5522 s = NULL;
5523 else
5524 s = vim_strnsave(s + n1, 1);
5525 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005526 clear_tv(rettv);
5527 rettv->v_type = VAR_STRING;
5528 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005529 break;
5530
5531 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005532 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533 if (n1 < 0)
5534 n1 = len + n1;
5535 if (!empty1 && (n1 < 0 || n1 >= len))
5536 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005537 /* For a range we allow invalid values and return an empty
5538 * list. A list index out of range is an error. */
5539 if (!range)
5540 {
5541 if (verbose)
5542 EMSGN(_(e_listidx), n1);
5543 return FAIL;
5544 }
5545 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005546 }
5547 if (range)
5548 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005549 list_T *l;
5550 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005551
5552 if (n2 < 0)
5553 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005554 else if (n2 >= len)
5555 n2 = len - 1;
5556 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005557 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005558 l = list_alloc();
5559 if (l == NULL)
5560 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005561 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005562 n1 <= n2; ++n1)
5563 {
5564 if (list_append_tv(l, &item->li_tv) == FAIL)
5565 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005566 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005567 return FAIL;
5568 }
5569 item = item->li_next;
5570 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005571 clear_tv(rettv);
5572 rettv->v_type = VAR_LIST;
5573 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005574 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005575 }
5576 else
5577 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005578 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005579 clear_tv(rettv);
5580 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005581 }
5582 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005583
5584 case VAR_DICT:
5585 if (range)
5586 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005587 if (verbose)
5588 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005589 if (len == -1)
5590 clear_tv(&var1);
5591 return FAIL;
5592 }
5593 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005594 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005595
5596 if (len == -1)
5597 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005598 key = get_tv_string_chk(&var1);
5599 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005600 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005601 clear_tv(&var1);
5602 return FAIL;
5603 }
5604 }
5605
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005606 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005607
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005608 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005609 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005610 if (len == -1)
5611 clear_tv(&var1);
5612 if (item == NULL)
5613 return FAIL;
5614
5615 copy_tv(&item->di_tv, &var1);
5616 clear_tv(rettv);
5617 *rettv = var1;
5618 }
5619 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005620 }
5621 }
5622
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005623 return OK;
5624}
5625
5626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 * Get an option value.
5628 * "arg" points to the '&' or '+' before the option name.
5629 * "arg" is advanced to character after the option name.
5630 * Return OK or FAIL.
5631 */
5632 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005633get_option_tv(
5634 char_u **arg,
5635 typval_T *rettv, /* when NULL, only check if option exists */
5636 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
5638 char_u *option_end;
5639 long numval;
5640 char_u *stringval;
5641 int opt_type;
5642 int c;
5643 int working = (**arg == '+'); /* has("+option") */
5644 int ret = OK;
5645 int opt_flags;
5646
5647 /*
5648 * Isolate the option name and find its value.
5649 */
5650 option_end = find_option_end(arg, &opt_flags);
5651 if (option_end == NULL)
5652 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005653 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 EMSG2(_("E112: Option name missing: %s"), *arg);
5655 return FAIL;
5656 }
5657
5658 if (!evaluate)
5659 {
5660 *arg = option_end;
5661 return OK;
5662 }
5663
5664 c = *option_end;
5665 *option_end = NUL;
5666 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005667 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
5669 if (opt_type == -3) /* invalid name */
5670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005671 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 EMSG2(_("E113: Unknown option: %s"), *arg);
5673 ret = FAIL;
5674 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005675 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 {
5677 if (opt_type == -2) /* hidden string option */
5678 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005679 rettv->v_type = VAR_STRING;
5680 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 }
5682 else if (opt_type == -1) /* hidden number option */
5683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005684 rettv->v_type = VAR_NUMBER;
5685 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 }
5687 else if (opt_type == 1) /* number option */
5688 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005689 rettv->v_type = VAR_NUMBER;
5690 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 }
5692 else /* string option */
5693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005694 rettv->v_type = VAR_STRING;
5695 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 }
5697 }
5698 else if (working && (opt_type == -2 || opt_type == -1))
5699 ret = FAIL;
5700
5701 *option_end = c; /* put back for error messages */
5702 *arg = option_end;
5703
5704 return ret;
5705}
5706
5707/*
5708 * Allocate a variable for a string constant.
5709 * Return OK or FAIL.
5710 */
5711 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005712get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713{
5714 char_u *p;
5715 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 int extra = 0;
5717
5718 /*
5719 * Find the end of the string, skipping backslashed characters.
5720 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005721 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 {
5723 if (*p == '\\' && p[1] != NUL)
5724 {
5725 ++p;
5726 /* A "\<x>" form occupies at least 4 characters, and produces up
5727 * to 6 characters: reserve space for 2 extra */
5728 if (*p == '<')
5729 extra += 2;
5730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 }
5732
5733 if (*p != '"')
5734 {
5735 EMSG2(_("E114: Missing quote: %s"), *arg);
5736 return FAIL;
5737 }
5738
5739 /* If only parsing, set *arg and return here */
5740 if (!evaluate)
5741 {
5742 *arg = p + 1;
5743 return OK;
5744 }
5745
5746 /*
5747 * Copy the string into allocated memory, handling backslashed
5748 * characters.
5749 */
5750 name = alloc((unsigned)(p - *arg + extra));
5751 if (name == NULL)
5752 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 rettv->v_type = VAR_STRING;
5754 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 {
5758 if (*p == '\\')
5759 {
5760 switch (*++p)
5761 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 case 'b': *name++ = BS; ++p; break;
5763 case 'e': *name++ = ESC; ++p; break;
5764 case 'f': *name++ = FF; ++p; break;
5765 case 'n': *name++ = NL; ++p; break;
5766 case 'r': *name++ = CAR; ++p; break;
5767 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768
5769 case 'X': /* hex: "\x1", "\x12" */
5770 case 'x':
5771 case 'u': /* Unicode: "\u0023" */
5772 case 'U':
5773 if (vim_isxdigit(p[1]))
5774 {
5775 int n, nr;
5776 int c = toupper(*p);
5777
5778 if (c == 'X')
5779 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005780 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005782 else
5783 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 nr = 0;
5785 while (--n >= 0 && vim_isxdigit(p[1]))
5786 {
5787 ++p;
5788 nr = (nr << 4) + hex2nr(*p);
5789 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791#ifdef FEAT_MBYTE
5792 /* For "\u" store the number according to
5793 * 'encoding'. */
5794 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 else
5797#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 break;
5801
5802 /* octal: "\1", "\12", "\123" */
5803 case '0':
5804 case '1':
5805 case '2':
5806 case '3':
5807 case '4':
5808 case '5':
5809 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 case '7': *name = *p++ - '0';
5811 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 *name = (*name << 3) + *p++ - '0';
5814 if (*p >= '0' && *p <= '7')
5815 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005817 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 break;
5819
5820 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005821 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 if (extra != 0)
5823 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005824 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 break;
5826 }
5827 /* FALLTHROUGH */
5828
Bram Moolenaar8c711452005-01-14 21:53:12 +00005829 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 break;
5831 }
5832 }
5833 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005837 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 *arg = p + 1;
5839
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 return OK;
5841}
5842
5843/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005844 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 * Return OK or FAIL.
5846 */
5847 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005848get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849{
5850 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005851 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005852 int reduce = 0;
5853
5854 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005856 */
5857 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5858 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005860 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005862 break;
5863 ++reduce;
5864 ++p;
5865 }
5866 }
5867
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005869 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005870 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005871 return FAIL;
5872 }
5873
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005875 if (!evaluate)
5876 {
5877 *arg = p + 1;
5878 return OK;
5879 }
5880
5881 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005882 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005883 */
5884 str = alloc((unsigned)((p - *arg) - reduce));
5885 if (str == NULL)
5886 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005887 rettv->v_type = VAR_STRING;
5888 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 break;
5896 ++p;
5897 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005898 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005899 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005900 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005901 *arg = p + 1;
5902
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005903 return OK;
5904}
5905
Bram Moolenaarddecc252016-04-06 22:59:37 +02005906 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005907partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005908{
5909 int i;
5910
5911 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005912 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005913 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005914 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005915 func_unref(pt->pt_name);
5916 vim_free(pt->pt_name);
5917 vim_free(pt);
5918}
5919
5920/*
5921 * Unreference a closure: decrement the reference count and free it when it
5922 * becomes zero.
5923 */
5924 void
5925partial_unref(partial_T *pt)
5926{
5927 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005928 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005929}
5930
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005931static int tv_equal_recurse_limit;
5932
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005933 static int
5934func_equal(
5935 typval_T *tv1,
5936 typval_T *tv2,
5937 int ic) /* ignore case */
5938{
5939 char_u *s1, *s2;
5940 dict_T *d1, *d2;
5941 int a1, a2;
5942 int i;
5943
5944 /* empty and NULL function name considered the same */
5945 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
5946 : tv1->vval.v_partial->pt_name;
5947 if (s1 != NULL && *s1 == NUL)
5948 s1 = NULL;
5949 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
5950 : tv2->vval.v_partial->pt_name;
5951 if (s2 != NULL && *s2 == NUL)
5952 s2 = NULL;
5953 if (s1 == NULL || s2 == NULL)
5954 {
5955 if (s1 != s2)
5956 return FALSE;
5957 }
5958 else if (STRCMP(s1, s2) != 0)
5959 return FALSE;
5960
5961 /* empty dict and NULL dict is different */
5962 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5963 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5964 if (d1 == NULL || d2 == NULL)
5965 {
5966 if (d1 != d2)
5967 return FALSE;
5968 }
5969 else if (!dict_equal(d1, d2, ic, TRUE))
5970 return FALSE;
5971
5972 /* empty list and no list considered the same */
5973 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5974 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5975 if (a1 != a2)
5976 return FALSE;
5977 for (i = 0; i < a1; ++i)
5978 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5979 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5980 return FALSE;
5981
5982 return TRUE;
5983}
5984
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005985/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005986 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005987 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005988 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005989 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005990 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005991tv_equal(
5992 typval_T *tv1,
5993 typval_T *tv2,
5994 int ic, /* ignore case */
5995 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005996{
5997 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005998 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005999 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006000 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006001
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006002 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006003 * recursiveness to a limit. We guess they are equal then.
6004 * A fixed limit has the problem of still taking an awful long time.
6005 * Reduce the limit every time running into it. That should work fine for
6006 * deeply linked structures that are not recursively linked and catch
6007 * recursiveness quickly. */
6008 if (!recursive)
6009 tv_equal_recurse_limit = 1000;
6010 if (recursive_cnt >= tv_equal_recurse_limit)
6011 {
6012 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006013 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006014 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006015
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02006016 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
6017 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006018 if ((tv1->v_type == VAR_FUNC
6019 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6020 && (tv2->v_type == VAR_FUNC
6021 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6022 {
6023 ++recursive_cnt;
6024 r = func_equal(tv1, tv2, ic);
6025 --recursive_cnt;
6026 return r;
6027 }
6028
6029 if (tv1->v_type != tv2->v_type)
6030 return FALSE;
6031
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006032 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006033 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006034 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006035 ++recursive_cnt;
6036 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6037 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006038 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006039
6040 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006041 ++recursive_cnt;
6042 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6043 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006044 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006045
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006046 case VAR_NUMBER:
6047 return tv1->vval.v_number == tv2->vval.v_number;
6048
6049 case VAR_STRING:
6050 s1 = get_tv_string_buf(tv1, buf1);
6051 s2 = get_tv_string_buf(tv2, buf2);
6052 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006053
6054 case VAR_SPECIAL:
6055 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006056
6057 case VAR_FLOAT:
6058#ifdef FEAT_FLOAT
6059 return tv1->vval.v_float == tv2->vval.v_float;
6060#endif
6061 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006062#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006063 return tv1->vval.v_job == tv2->vval.v_job;
6064#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006065 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006066#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006067 return tv1->vval.v_channel == tv2->vval.v_channel;
6068#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006069 case VAR_FUNC:
6070 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006071 case VAR_UNKNOWN:
6072 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006073 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006074
Bram Moolenaara03f2332016-02-06 18:09:59 +01006075 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6076 * does not equal anything, not even itself. */
6077 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006078}
6079
6080/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006081 * Return the next (unique) copy ID.
6082 * Used for serializing nested structures.
6083 */
6084 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006085get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006086{
6087 current_copyID += COPYID_INC;
6088 return current_copyID;
6089}
6090
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006091/* Used by get_func_tv() */
6092static garray_T funcargs = GA_EMPTY;
6093
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006094/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006095 * Garbage collection for lists and dictionaries.
6096 *
6097 * We use reference counts to be able to free most items right away when they
6098 * are no longer used. But for composite items it's possible that it becomes
6099 * unused while the reference count is > 0: When there is a recursive
6100 * reference. Example:
6101 * :let l = [1, 2, 3]
6102 * :let d = {9: l}
6103 * :let l[1] = d
6104 *
6105 * Since this is quite unusual we handle this with garbage collection: every
6106 * once in a while find out which lists and dicts are not referenced from any
6107 * variable.
6108 *
6109 * Here is a good reference text about garbage collection (refers to Python
6110 * but it applies to all reference-counting mechanisms):
6111 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006112 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006113
6114/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006115 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02006116 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006117 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006118 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006119 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006120garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006121{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006122 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006123 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006124 buf_T *buf;
6125 win_T *wp;
6126 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006127 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006128 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006129 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006130#ifdef FEAT_WINDOWS
6131 tabpage_T *tp;
6132#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006133
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006134 if (!testing)
6135 {
6136 /* Only do this once. */
6137 want_garbage_collect = FALSE;
6138 may_garbage_collect = FALSE;
6139 garbage_collect_at_exit = FALSE;
6140 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006141
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006142 /* We advance by two because we add one for items referenced through
6143 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006144 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006145
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006146 /*
6147 * 1. Go through all accessible variables and mark all lists and dicts
6148 * with copyID.
6149 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006150
6151 /* Don't free variables in the previous_funccal list unless they are only
6152 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006153 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006154 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6155 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006156 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6157 NULL);
6158 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6159 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006160 }
6161
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006162 /* script-local variables */
6163 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006164 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006165
6166 /* buffer-local variables */
6167 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006168 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6169 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006170
6171 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006172 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006173 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6174 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006175#ifdef FEAT_AUTOCMD
6176 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006177 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6178 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006179#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006180
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006181#ifdef FEAT_WINDOWS
6182 /* tabpage-local variables */
6183 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006184 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6185 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006186#endif
6187
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006188 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006189 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006190
6191 /* function-local variables */
6192 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6193 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006194 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6195 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006196 }
6197
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006198 /* function call arguments, if v:testing is set. */
6199 for (i = 0; i < funcargs.ga_len; ++i)
6200 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
6201 copyID, NULL, NULL);
6202
Bram Moolenaard812df62008-11-09 12:46:09 +00006203 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006204 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006205
Bram Moolenaar1dced572012-04-05 16:54:08 +02006206#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006207 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006208#endif
6209
Bram Moolenaardb913952012-06-29 12:54:53 +02006210#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006211 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006212#endif
6213
6214#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006215 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006216#endif
6217
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006218#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02006219 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02006220 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006221#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02006222#ifdef FEAT_NETBEANS_INTG
6223 abort = abort || set_ref_in_nb_channel(copyID);
6224#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006225
Bram Moolenaare3188e22016-05-31 21:13:04 +02006226#ifdef FEAT_TIMERS
6227 abort = abort || set_ref_in_timer(copyID);
6228#endif
6229
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006230 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006231 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006232 /*
6233 * 2. Free lists and dictionaries that are not referenced.
6234 */
6235 did_free = free_unref_items(copyID);
6236
6237 /*
6238 * 3. Check if any funccal can be freed now.
6239 */
6240 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006241 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006242 if (can_free_funccal(*pfc, copyID))
6243 {
6244 fc = *pfc;
6245 *pfc = fc->caller;
6246 free_funccal(fc, TRUE);
6247 did_free = TRUE;
6248 did_free_funccal = TRUE;
6249 }
6250 else
6251 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006252 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006253 if (did_free_funccal)
6254 /* When a funccal was freed some more items might be garbage
6255 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006256 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006257 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006258 else if (p_verbose > 0)
6259 {
6260 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6261 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006262
6263 return did_free;
6264}
6265
6266/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006267 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006268 */
6269 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006270free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006271{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006272 int did_free = FALSE;
6273
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006274 /* Let all "free" functions know that we are here. This means no
6275 * dictionaries, lists, channels or jobs are to be freed, because we will
6276 * do that here. */
6277 in_free_unref_items = TRUE;
6278
6279 /*
6280 * PASS 1: free the contents of the items. We don't free the items
6281 * themselves yet, so that it is possible to decrement refcount counters
6282 */
6283
Bram Moolenaarcd524592016-07-17 14:57:05 +02006284 /* Go through the list of dicts and free items without the copyID. */
6285 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006286
Bram Moolenaarda861d62016-07-17 15:46:27 +02006287 /* Go through the list of lists and free items without the copyID. */
6288 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006289
6290#ifdef FEAT_JOB_CHANNEL
6291 /* Go through the list of jobs and free items without the copyID. This
6292 * must happen before doing channels, because jobs refer to channels, but
6293 * the reference from the channel to the job isn't tracked. */
6294 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
6295
6296 /* Go through the list of channels and free items without the copyID. */
6297 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
6298#endif
6299
6300 /*
6301 * PASS 2: free the items themselves.
6302 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006303 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02006304 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01006305
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006306#ifdef FEAT_JOB_CHANNEL
6307 /* Go through the list of jobs and free items without the copyID. This
6308 * must happen before doing channels, because jobs refer to channels, but
6309 * the reference from the channel to the job isn't tracked. */
6310 free_unused_jobs(copyID, COPYID_MASK);
6311
6312 /* Go through the list of channels and free items without the copyID. */
6313 free_unused_channels(copyID, COPYID_MASK);
6314#endif
6315
6316 in_free_unref_items = FALSE;
6317
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006318 return did_free;
6319}
6320
6321/*
6322 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006323 * "list_stack" is used to add lists to be marked. Can be NULL.
6324 *
6325 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006326 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006327 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006328set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006329{
6330 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006331 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006332 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006333 hashtab_T *cur_ht;
6334 ht_stack_T *ht_stack = NULL;
6335 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006336
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006337 cur_ht = ht;
6338 for (;;)
6339 {
6340 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006341 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006342 /* Mark each item in the hashtab. If the item contains a hashtab
6343 * it is added to ht_stack, if it contains a list it is added to
6344 * list_stack. */
6345 todo = (int)cur_ht->ht_used;
6346 for (hi = cur_ht->ht_array; todo > 0; ++hi)
6347 if (!HASHITEM_EMPTY(hi))
6348 {
6349 --todo;
6350 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
6351 &ht_stack, list_stack);
6352 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006353 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006354
6355 if (ht_stack == NULL)
6356 break;
6357
6358 /* take an item from the stack */
6359 cur_ht = ht_stack->ht;
6360 tempitem = ht_stack;
6361 ht_stack = ht_stack->prev;
6362 free(tempitem);
6363 }
6364
6365 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006366}
6367
6368/*
6369 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006370 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6371 *
6372 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006373 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006374 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006375set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006376{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006377 listitem_T *li;
6378 int abort = FALSE;
6379 list_T *cur_l;
6380 list_stack_T *list_stack = NULL;
6381 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006382
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006383 cur_l = l;
6384 for (;;)
6385 {
6386 if (!abort)
6387 /* Mark each item in the list. If the item contains a hashtab
6388 * it is added to ht_stack, if it contains a list it is added to
6389 * list_stack. */
6390 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
6391 abort = abort || set_ref_in_item(&li->li_tv, copyID,
6392 ht_stack, &list_stack);
6393 if (list_stack == NULL)
6394 break;
6395
6396 /* take an item from the stack */
6397 cur_l = list_stack->list;
6398 tempitem = list_stack;
6399 list_stack = list_stack->prev;
6400 free(tempitem);
6401 }
6402
6403 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006404}
6405
6406/*
6407 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006408 * "list_stack" is used to add lists to be marked. Can be NULL.
6409 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6410 *
6411 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006412 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006413 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006414set_ref_in_item(
6415 typval_T *tv,
6416 int copyID,
6417 ht_stack_T **ht_stack,
6418 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006419{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006420 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006421
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006422 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006423 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006424 dict_T *dd = tv->vval.v_dict;
6425
Bram Moolenaara03f2332016-02-06 18:09:59 +01006426 if (dd != NULL && dd->dv_copyID != copyID)
6427 {
6428 /* Didn't see this dict yet. */
6429 dd->dv_copyID = copyID;
6430 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006431 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01006432 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
6433 }
6434 else
6435 {
6436 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
6437 if (newitem == NULL)
6438 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006439 else
6440 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01006441 newitem->ht = &dd->dv_hashtab;
6442 newitem->prev = *ht_stack;
6443 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006444 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006445 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01006446 }
6447 }
6448 else if (tv->v_type == VAR_LIST)
6449 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006450 list_T *ll = tv->vval.v_list;
6451
Bram Moolenaara03f2332016-02-06 18:09:59 +01006452 if (ll != NULL && ll->lv_copyID != copyID)
6453 {
6454 /* Didn't see this list yet. */
6455 ll->lv_copyID = copyID;
6456 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006457 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01006458 abort = set_ref_in_list(ll, copyID, ht_stack);
6459 }
6460 else
6461 {
6462 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006463 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01006464 if (newitem == NULL)
6465 abort = TRUE;
6466 else
6467 {
6468 newitem->list = ll;
6469 newitem->prev = *list_stack;
6470 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006471 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006472 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01006473 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006474 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006475 else if (tv->v_type == VAR_PARTIAL)
6476 {
6477 partial_T *pt = tv->vval.v_partial;
6478 int i;
6479
6480 /* A partial does not have a copyID, because it cannot contain itself.
6481 */
6482 if (pt != NULL)
6483 {
6484 if (pt->pt_dict != NULL)
6485 {
6486 typval_T dtv;
6487
6488 dtv.v_type = VAR_DICT;
6489 dtv.vval.v_dict = pt->pt_dict;
6490 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6491 }
6492
6493 for (i = 0; i < pt->pt_argc; ++i)
6494 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
6495 ht_stack, list_stack);
6496 }
6497 }
6498#ifdef FEAT_JOB_CHANNEL
6499 else if (tv->v_type == VAR_JOB)
6500 {
6501 job_T *job = tv->vval.v_job;
6502 typval_T dtv;
6503
6504 if (job != NULL && job->jv_copyID != copyID)
6505 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006506 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006507 if (job->jv_channel != NULL)
6508 {
6509 dtv.v_type = VAR_CHANNEL;
6510 dtv.vval.v_channel = job->jv_channel;
6511 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6512 }
6513 if (job->jv_exit_partial != NULL)
6514 {
6515 dtv.v_type = VAR_PARTIAL;
6516 dtv.vval.v_partial = job->jv_exit_partial;
6517 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6518 }
6519 }
6520 }
6521 else if (tv->v_type == VAR_CHANNEL)
6522 {
6523 channel_T *ch =tv->vval.v_channel;
6524 int part;
6525 typval_T dtv;
6526 jsonq_T *jq;
6527 cbq_T *cq;
6528
6529 if (ch != NULL && ch->ch_copyID != copyID)
6530 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006531 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006532 for (part = PART_SOCK; part <= PART_IN; ++part)
6533 {
6534 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
6535 jq = jq->jq_next)
6536 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
6537 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
6538 cq = cq->cq_next)
6539 if (cq->cq_partial != NULL)
6540 {
6541 dtv.v_type = VAR_PARTIAL;
6542 dtv.vval.v_partial = cq->cq_partial;
6543 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6544 }
6545 if (ch->ch_part[part].ch_partial != NULL)
6546 {
6547 dtv.v_type = VAR_PARTIAL;
6548 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
6549 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6550 }
6551 }
6552 if (ch->ch_partial != NULL)
6553 {
6554 dtv.v_type = VAR_PARTIAL;
6555 dtv.vval.v_partial = ch->ch_partial;
6556 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6557 }
6558 if (ch->ch_close_partial != NULL)
6559 {
6560 dtv.v_type = VAR_PARTIAL;
6561 dtv.vval.v_partial = ch->ch_close_partial;
6562 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6563 }
6564 }
6565 }
6566#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006567 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006568}
6569
Bram Moolenaar069c1e72016-07-15 21:25:08 +02006570/* Get function arguments. */
6571 static int
6572get_function_args(
6573 char_u **argp,
6574 char_u endchar,
6575 garray_T *newargs,
6576 int *varargs,
6577 int skip)
6578{
6579 int mustend = FALSE;
6580 char_u *arg = *argp;
6581 char_u *p = arg;
6582 int c;
6583 int i;
6584
6585 if (newargs != NULL)
6586 ga_init2(newargs, (int)sizeof(char_u *), 3);
6587
6588 if (varargs != NULL)
6589 *varargs = FALSE;
6590
6591 /*
6592 * Isolate the arguments: "arg1, arg2, ...)"
6593 */
6594 while (*p != endchar)
6595 {
6596 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
6597 {
6598 if (varargs != NULL)
6599 *varargs = TRUE;
6600 p += 3;
6601 mustend = TRUE;
6602 }
6603 else
6604 {
6605 arg = p;
6606 while (ASCII_ISALNUM(*p) || *p == '_')
6607 ++p;
6608 if (arg == p || isdigit(*arg)
6609 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
6610 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
6611 {
6612 if (!skip)
6613 EMSG2(_("E125: Illegal argument: %s"), arg);
6614 break;
6615 }
6616 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
6617 return FAIL;
6618 if (newargs != NULL)
6619 {
6620 c = *p;
6621 *p = NUL;
6622 arg = vim_strsave(arg);
6623 if (arg == NULL)
6624 goto err_ret;
6625
6626 /* Check for duplicate argument name. */
6627 for (i = 0; i < newargs->ga_len; ++i)
6628 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg) == 0)
6629 {
6630 EMSG2(_("E853: Duplicate argument name: %s"), arg);
6631 vim_free(arg);
6632 goto err_ret;
6633 }
6634 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg;
6635 newargs->ga_len++;
6636
6637 *p = c;
6638 }
6639 if (*p == ',')
6640 ++p;
6641 else
6642 mustend = TRUE;
6643 }
6644 p = skipwhite(p);
6645 if (mustend && *p != endchar)
6646 {
6647 if (!skip)
6648 EMSG2(_(e_invarg2), *argp);
6649 break;
6650 }
6651 }
6652 ++p; /* skip the ')' */
6653
6654 *argp = p;
6655 return OK;
6656
6657err_ret:
6658 if (newargs != NULL)
6659 ga_clear_strings(newargs);
6660 return FAIL;
6661}
6662
6663/*
6664 * Parse a lambda expression and get a Funcref from "*arg".
6665 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
6666 */
6667 static int
6668get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate)
6669{
6670 garray_T newargs;
6671 garray_T newlines;
6672 ufunc_T *fp = NULL;
6673 int varargs;
6674 int ret;
6675 char_u name[20];
6676 char_u *start = skipwhite(*arg + 1);
6677 char_u *s, *e;
6678 static int lambda_no = 0;
6679
6680 ga_init(&newargs);
6681 ga_init(&newlines);
6682
Bram Moolenaarcd524592016-07-17 14:57:05 +02006683 /* First, check if this is a lambda expression. "->" must exist. */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02006684 ret = get_function_args(&start, '-', NULL, NULL, TRUE);
6685 if (ret == FAIL || *start != '>')
6686 return NOTDONE;
6687
6688 /* Parse the arguments again. */
6689 *arg = skipwhite(*arg + 1);
6690 ret = get_function_args(arg, '-', &newargs, &varargs, FALSE);
6691 if (ret == FAIL || **arg != '>')
6692 goto errret;
6693
6694 /* Get the start and the end of the expression. */
6695 *arg = skipwhite(*arg + 1);
6696 s = *arg;
6697 ret = skip_expr(arg);
6698 if (ret == FAIL)
6699 goto errret;
6700 e = *arg;
6701 *arg = skipwhite(*arg);
6702 if (**arg != '}')
6703 goto errret;
6704 ++*arg;
6705
6706 if (evaluate)
6707 {
6708 int len;
6709 char_u *p;
6710
6711 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + 20));
6712 if (fp == NULL)
6713 goto errret;
6714
6715 sprintf((char*)name, "<lambda>%d", ++lambda_no);
6716
6717 ga_init2(&newlines, (int)sizeof(char_u *), 1);
6718 if (ga_grow(&newlines, 1) == FAIL)
6719 goto errret;
6720
6721 /* Add "return " before the expression.
6722 * TODO: Support multiple expressions. */
6723 len = 7 + e - s + 1;
6724 p = (char_u *)alloc(len);
6725 if (p == NULL)
6726 goto errret;
6727 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
6728 STRCPY(p, "return ");
6729 STRNCPY(p + 7, s, e - s);
6730 p[7 + e - s] = NUL;
6731
6732 fp->uf_refcount = 1;
6733 STRCPY(fp->uf_name, name);
6734 hash_add(&func_hashtab, UF2HIKEY(fp));
6735 fp->uf_args = newargs;
6736 fp->uf_lines = newlines;
6737
6738#ifdef FEAT_PROFILE
6739 fp->uf_tml_count = NULL;
6740 fp->uf_tml_total = NULL;
6741 fp->uf_tml_self = NULL;
6742 fp->uf_profiling = FALSE;
6743 if (prof_def_func())
6744 func_do_profile(fp);
6745#endif
6746 fp->uf_varargs = TRUE;
6747 fp->uf_flags = 0;
6748 fp->uf_calls = 0;
6749 fp->uf_script_ID = current_SID;
6750
6751 rettv->vval.v_string = vim_strsave(name);
6752 rettv->v_type = VAR_FUNC;
6753 }
6754 else
6755 ga_clear_strings(&newargs);
6756
6757 return OK;
6758
6759errret:
6760 ga_clear_strings(&newargs);
6761 ga_clear_strings(&newlines);
6762 vim_free(fp);
6763 return FAIL;
6764}
6765
Bram Moolenaar17a13432016-01-24 14:22:10 +01006766 static char *
6767get_var_special_name(int nr)
6768{
6769 switch (nr)
6770 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01006771 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01006772 case VVAL_TRUE: return "v:true";
6773 case VVAL_NONE: return "v:none";
6774 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01006775 }
6776 EMSG2(_(e_intern2), "get_var_special_name()");
6777 return "42";
6778}
6779
Bram Moolenaar8c711452005-01-14 21:53:12 +00006780/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006781 * Return a string with the string representation of a variable.
6782 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006783 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006784 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006785 * When both "echo_style" and "dict_val" are FALSE, put quotes around stings as
6786 * "string()", otherwise does not put quotes around strings, as ":echo"
6787 * displays values.
6788 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6789 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006790 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006791 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006792 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006793echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006794 typval_T *tv,
6795 char_u **tofree,
6796 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006797 int copyID,
6798 int echo_style,
6799 int restore_copyID,
6800 int dict_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006801{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006802 static int recurse = 0;
6803 char_u *r = NULL;
6804
Bram Moolenaar33570922005-01-25 22:26:29 +00006805 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006806 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006807 if (!did_echo_string_emsg)
6808 {
6809 /* Only give this message once for a recursive call to avoid
6810 * flooding the user with errors. And stop iterating over lists
6811 * and dicts. */
6812 did_echo_string_emsg = TRUE;
6813 EMSG(_("E724: variable nested too deep for displaying"));
6814 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006815 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006816 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006817 }
6818 ++recurse;
6819
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006820 switch (tv->v_type)
6821 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006822 case VAR_STRING:
6823 if (echo_style && !dict_val)
6824 {
6825 *tofree = NULL;
6826 r = get_tv_string_buf(tv, numbuf);
6827 }
6828 else
6829 {
6830 *tofree = string_quote(tv->vval.v_string, FALSE);
6831 r = *tofree;
6832 }
6833 break;
6834
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006835 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006836 if (echo_style)
6837 {
6838 *tofree = NULL;
6839 r = tv->vval.v_string;
6840 }
6841 else
6842 {
6843 *tofree = string_quote(tv->vval.v_string, TRUE);
6844 r = *tofree;
6845 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006846 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006847
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006848 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006849 {
6850 partial_T *pt = tv->vval.v_partial;
6851 char_u *fname = string_quote(pt == NULL ? NULL
6852 : pt->pt_name, FALSE);
6853 garray_T ga;
6854 int i;
6855 char_u *tf;
6856
6857 ga_init2(&ga, 1, 100);
6858 ga_concat(&ga, (char_u *)"function(");
6859 if (fname != NULL)
6860 {
6861 ga_concat(&ga, fname);
6862 vim_free(fname);
6863 }
6864 if (pt != NULL && pt->pt_argc > 0)
6865 {
6866 ga_concat(&ga, (char_u *)", [");
6867 for (i = 0; i < pt->pt_argc; ++i)
6868 {
6869 if (i > 0)
6870 ga_concat(&ga, (char_u *)", ");
6871 ga_concat(&ga,
6872 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6873 vim_free(tf);
6874 }
6875 ga_concat(&ga, (char_u *)"]");
6876 }
6877 if (pt != NULL && pt->pt_dict != NULL)
6878 {
6879 typval_T dtv;
6880
6881 ga_concat(&ga, (char_u *)", ");
6882 dtv.v_type = VAR_DICT;
6883 dtv.vval.v_dict = pt->pt_dict;
6884 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6885 vim_free(tf);
6886 }
6887 ga_concat(&ga, (char_u *)")");
6888
6889 *tofree = ga.ga_data;
6890 r = *tofree;
6891 break;
6892 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006893
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006894 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006895 if (tv->vval.v_list == NULL)
6896 {
6897 *tofree = NULL;
6898 r = NULL;
6899 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006900 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6901 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006902 {
6903 *tofree = NULL;
6904 r = (char_u *)"[...]";
6905 }
6906 else
6907 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006908 int old_copyID = tv->vval.v_list->lv_copyID;
6909
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006910 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006911 *tofree = list2string(tv, copyID, restore_copyID);
6912 if (restore_copyID)
6913 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006914 r = *tofree;
6915 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006916 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006917
Bram Moolenaar8c711452005-01-14 21:53:12 +00006918 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006919 if (tv->vval.v_dict == NULL)
6920 {
6921 *tofree = NULL;
6922 r = NULL;
6923 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006924 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6925 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006926 {
6927 *tofree = NULL;
6928 r = (char_u *)"{...}";
6929 }
6930 else
6931 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006932 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006933 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006934 *tofree = dict2string(tv, copyID, restore_copyID);
6935 if (restore_copyID)
6936 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006937 r = *tofree;
6938 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006939 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006940
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006941 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006942 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006943 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006944 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006945 *tofree = NULL;
6946 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006947 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006948
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006949 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006950#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006951 *tofree = NULL;
6952 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6953 r = numbuf;
6954 break;
6955#endif
6956
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006957 case VAR_SPECIAL:
6958 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006959 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006960 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006961 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006962
Bram Moolenaar8502c702014-06-17 12:51:16 +02006963 if (--recurse == 0)
6964 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006965 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006966}
6967
6968/*
6969 * Return a string with the string representation of a variable.
6970 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6971 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006972 * Does not put quotes around strings, as ":echo" displays values.
6973 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6974 * May return NULL.
6975 */
6976 static char_u *
6977echo_string(
6978 typval_T *tv,
6979 char_u **tofree,
6980 char_u *numbuf,
6981 int copyID)
6982{
6983 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6984}
6985
6986/*
6987 * Return a string with the string representation of a variable.
6988 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6989 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006990 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006991 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006992 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006993 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006994tv2string(
6995 typval_T *tv,
6996 char_u **tofree,
6997 char_u *numbuf,
6998 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006999{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007000 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007001}
7002
7003/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007004 * Return string "str" in ' quotes, doubling ' characters.
7005 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007006 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007007 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007008 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007009string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007010{
Bram Moolenaar33570922005-01-25 22:26:29 +00007011 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007012 char_u *p, *r, *s;
7013
Bram Moolenaar33570922005-01-25 22:26:29 +00007014 len = (function ? 13 : 3);
7015 if (str != NULL)
7016 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007017 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007018 for (p = str; *p != NUL; mb_ptr_adv(p))
7019 if (*p == '\'')
7020 ++len;
7021 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007022 s = r = alloc(len);
7023 if (r != NULL)
7024 {
7025 if (function)
7026 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007027 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007028 r += 10;
7029 }
7030 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007031 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007032 if (str != NULL)
7033 for (p = str; *p != NUL; )
7034 {
7035 if (*p == '\'')
7036 *r++ = '\'';
7037 MB_COPY_CHAR(p, r);
7038 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007039 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007040 if (function)
7041 *r++ = ')';
7042 *r++ = NUL;
7043 }
7044 return s;
7045}
7046
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007047#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007048/*
7049 * Convert the string "text" to a floating point number.
7050 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7051 * this always uses a decimal point.
7052 * Returns the length of the text that was consumed.
7053 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007054 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007055string2float(
7056 char_u *text,
7057 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007058{
7059 char *s = (char *)text;
7060 float_T f;
7061
7062 f = strtod(s, &s);
7063 *value = f;
7064 return (int)((char_u *)s - text);
7065}
7066#endif
7067
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007068/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 * Get the value of an environment variable.
7070 * "arg" is pointing to the '$'. It is advanced to after the name.
7071 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007072 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 */
7074 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007075get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076{
7077 char_u *string = NULL;
7078 int len;
7079 int cc;
7080 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007081 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082
7083 ++*arg;
7084 name = *arg;
7085 len = get_env_len(arg);
7086 if (evaluate)
7087 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007088 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01007089 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007090
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007091 cc = name[len];
7092 name[len] = NUL;
7093 /* first try vim_getenv(), fast for normal environment vars */
7094 string = vim_getenv(name, &mustfree);
7095 if (string != NULL && *string != NUL)
7096 {
7097 if (!mustfree)
7098 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007100 else
7101 {
7102 if (mustfree)
7103 vim_free(string);
7104
7105 /* next try expanding things like $VIM and ${HOME} */
7106 string = expand_env_save(name - 1);
7107 if (string != NULL && *string == '$')
7108 {
7109 vim_free(string);
7110 string = NULL;
7111 }
7112 }
7113 name[len] = cc;
7114
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007115 rettv->v_type = VAR_STRING;
7116 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 }
7118
7119 return OK;
7120}
7121
7122/*
7123 * Array with names and number of arguments of all internal functions
7124 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7125 */
7126static struct fst
7127{
7128 char *f_name; /* function name */
7129 char f_min_argc; /* minimal number of arguments */
7130 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01007131 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007132 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133} functions[] =
7134{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007135#ifdef FEAT_FLOAT
7136 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007137 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007138#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007139 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007140 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 {"append", 2, 2, f_append},
7142 {"argc", 0, 0, f_argc},
7143 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007144 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007145 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01007146#ifdef FEAT_FLOAT
7147 {"asin", 1, 1, f_asin}, /* WJMc */
7148#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01007149 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01007150 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01007151 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01007152 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02007153 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02007154 {"assert_notequal", 2, 3, f_assert_notequal},
7155 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01007156 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007157#ifdef FEAT_FLOAT
7158 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007159 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007162 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 {"bufexists", 1, 1, f_bufexists},
7164 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7165 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7166 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7167 {"buflisted", 1, 1, f_buflisted},
7168 {"bufloaded", 1, 1, f_bufloaded},
7169 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007170 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaarb3619a92016-06-04 17:58:52 +02007171 {"bufwinid", 1, 1, f_bufwinid},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 {"bufwinnr", 1, 1, f_bufwinnr},
7173 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007174 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007175 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007176 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007177#ifdef FEAT_FLOAT
7178 {"ceil", 1, 1, f_ceil},
7179#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007180#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01007181 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01007182 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
7183 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01007184 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01007185 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01007186 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01007187 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01007188 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01007189 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01007190 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01007191 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01007192 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
7193 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01007194 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01007195 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01007196#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007197 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007198 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007200 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007202#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007203 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007204 {"complete_add", 1, 1, f_complete_add},
7205 {"complete_check", 0, 0, f_complete_check},
7206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007208 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007209#ifdef FEAT_FLOAT
7210 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007211 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007212#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007213 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007215 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007216 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01007217 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007219 {"diff_filler", 1, 1, f_diff_filler},
7220 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007221 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007223 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 {"eventhandler", 0, 0, f_eventhandler},
7225 {"executable", 1, 1, f_executable},
Bram Moolenaar79815f12016-07-09 17:07:29 +02007226 {"execute", 1, 2, f_execute},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007227 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007229#ifdef FEAT_FLOAT
7230 {"exp", 1, 1, f_exp},
7231#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007232 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007233 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007234 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7236 {"filereadable", 1, 1, f_filereadable},
7237 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007238 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007239 {"finddir", 1, 3, f_finddir},
7240 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007241#ifdef FEAT_FLOAT
7242 {"float2nr", 1, 1, f_float2nr},
7243 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007244 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007245#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007246 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 {"fnamemodify", 2, 2, f_fnamemodify},
7248 {"foldclosed", 1, 1, f_foldclosed},
7249 {"foldclosedend", 1, 1, f_foldclosedend},
7250 {"foldlevel", 1, 1, f_foldlevel},
7251 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007252 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007254 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007255 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007256 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007257 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007258 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 {"getchar", 0, 1, f_getchar},
7260 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02007261 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 {"getcmdline", 0, 0, f_getcmdline},
7263 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007264 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02007265 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007266#if defined(FEAT_CMDL_COMPL)
7267 {"getcompletion", 2, 2, f_getcompletion},
7268#endif
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02007269 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01007270 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007271 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007272 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 {"getfsize", 1, 1, f_getfsize},
7274 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007275 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007276 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007277 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007278 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007279 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007280 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007281 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02007282 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007284 {"gettabvar", 2, 3, f_gettabvar},
7285 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 {"getwinposx", 0, 0, f_getwinposx},
7287 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007288 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01007289 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01007290 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01007291 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007293 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01007294 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007295 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7297 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7298 {"histadd", 2, 2, f_histadd},
7299 {"histdel", 1, 2, f_histdel},
7300 {"histget", 1, 2, f_histget},
7301 {"histnr", 1, 1, f_histnr},
7302 {"hlID", 1, 1, f_hlID},
7303 {"hlexists", 1, 1, f_hlexists},
7304 {"hostname", 0, 0, f_hostname},
7305 {"iconv", 3, 3, f_iconv},
7306 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007307 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007308 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007310 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 {"inputrestore", 0, 0, f_inputrestore},
7312 {"inputsave", 0, 0, f_inputsave},
7313 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007314 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007315 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007317 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01007318#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7319 {"isnan", 1, 1, f_isnan},
7320#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00007321 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007322#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01007323 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01007324 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01007325 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01007326 {"job_start", 1, 2, f_job_start},
7327 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01007328 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01007329#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007330 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01007331 {"js_decode", 1, 1, f_js_decode},
7332 {"js_encode", 1, 1, f_js_encode},
7333 {"json_decode", 1, 1, f_json_decode},
7334 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007335 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007337 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 {"libcall", 3, 3, f_libcall},
7339 {"libcallnr", 3, 3, f_libcallnr},
7340 {"line", 1, 1, f_line},
7341 {"line2byte", 1, 1, f_line2byte},
7342 {"lispindent", 1, 1, f_lispindent},
7343 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007344#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007345 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007346 {"log10", 1, 1, f_log10},
7347#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007348#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01007349 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02007350#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007351 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007352 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007353 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007354 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02007355 {"matchadd", 2, 5, f_matchadd},
7356 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007357 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007358 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007359 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007360 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007361 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02007362 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007363 {"max", 1, 1, f_max},
7364 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007365#ifdef vim_mkdir
7366 {"mkdir", 1, 3, f_mkdir},
7367#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007368 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007369#ifdef FEAT_MZSCHEME
7370 {"mzeval", 1, 1, f_mzeval},
7371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007373 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007374 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007375 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01007376#ifdef FEAT_PERL
7377 {"perleval", 1, 1, f_perleval},
7378#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007379#ifdef FEAT_FLOAT
7380 {"pow", 2, 2, f_pow},
7381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007383 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007384 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02007385#ifdef FEAT_PYTHON3
7386 {"py3eval", 1, 1, f_py3eval},
7387#endif
7388#ifdef FEAT_PYTHON
7389 {"pyeval", 1, 1, f_pyeval},
7390#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00007391 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007392 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007393 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01007394#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01007395 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01007396#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007397 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 {"remote_expr", 2, 3, f_remote_expr},
7399 {"remote_foreground", 1, 1, f_remote_foreground},
7400 {"remote_peek", 1, 2, f_remote_peek},
7401 {"remote_read", 1, 1, f_remote_read},
7402 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007403 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007405 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007407 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007408#ifdef FEAT_FLOAT
7409 {"round", 1, 1, f_round},
7410#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02007411 {"screenattr", 2, 2, f_screenattr},
7412 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01007413 {"screencol", 0, 0, f_screencol},
7414 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00007415 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007416 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007417 {"searchpair", 3, 7, f_searchpair},
7418 {"searchpairpos", 3, 7, f_searchpairpos},
7419 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 {"server2client", 2, 2, f_server2client},
7421 {"serverlist", 0, 0, f_serverlist},
7422 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02007423 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01007425 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007427 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007428 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007429 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007430 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007432 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007433 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01007435#ifdef FEAT_CRYPT
7436 {"sha256", 1, 1, f_sha256},
7437#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007438 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02007439 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007441#ifdef FEAT_FLOAT
7442 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007443 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007444#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02007445 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007446 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007447 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007448 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007449 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007450#ifdef FEAT_FLOAT
7451 {"sqrt", 1, 1, f_sqrt},
7452 {"str2float", 1, 1, f_str2float},
7453#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007454 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02007455 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02007456 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02007457 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458#ifdef HAVE_STRFTIME
7459 {"strftime", 1, 2, f_strftime},
7460#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02007461 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00007462 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007463 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 {"strlen", 1, 1, f_strlen},
7465 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007466 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007468 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02007469 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 {"substitute", 4, 4, f_substitute},
7471 {"synID", 3, 3, f_synID},
7472 {"synIDattr", 2, 3, f_synIDattr},
7473 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02007474 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007475 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007476 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02007477 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007478 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007479 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007480 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007481 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007482 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007483#ifdef FEAT_FLOAT
7484 {"tan", 1, 1, f_tan},
7485 {"tanh", 1, 1, f_tanh},
7486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 {"tempname", 0, 0, f_tempname},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02007488 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
Bram Moolenaar5c719942016-07-09 23:40:45 +02007489 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02007490 {"test_disable_char_avail", 1, 1, f_test_disable_char_avail},
Bram Moolenaar574860b2016-05-24 17:33:34 +02007491 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
7492#ifdef FEAT_JOB_CHANNEL
7493 {"test_null_channel", 0, 0, f_test_null_channel},
7494#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007495 {"test_null_dict", 0, 0, f_test_null_dict},
Bram Moolenaar574860b2016-05-24 17:33:34 +02007496#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007497 {"test_null_job", 0, 0, f_test_null_job},
Bram Moolenaar574860b2016-05-24 17:33:34 +02007498#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007499 {"test_null_list", 0, 0, f_test_null_list},
Bram Moolenaar574860b2016-05-24 17:33:34 +02007500 {"test_null_partial", 0, 0, f_test_null_partial},
7501 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007502 {"test_settime", 1, 1, f_test_settime},
Bram Moolenaar975b5272016-03-15 23:10:59 +01007503#ifdef FEAT_TIMERS
7504 {"timer_start", 2, 3, f_timer_start},
7505 {"timer_stop", 1, 1, f_timer_stop},
7506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 {"tolower", 1, 1, f_tolower},
7508 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007509 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007510#ifdef FEAT_FLOAT
7511 {"trunc", 1, 1, f_trunc},
7512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007514 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007515 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01007516 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007517 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 {"virtcol", 1, 1, f_virtcol},
7519 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01007520 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01007521 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01007522 {"win_getid", 0, 2, f_win_getid},
7523 {"win_gotoid", 1, 1, f_win_gotoid},
7524 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
7525 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 {"winbufnr", 1, 1, f_winbufnr},
7527 {"wincol", 0, 0, f_wincol},
7528 {"winheight", 1, 1, f_winheight},
7529 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007530 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007532 {"winrestview", 1, 1, f_winrestview},
7533 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01007535 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007536 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007537 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538};
7539
7540#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7541
7542/*
7543 * Function given to ExpandGeneric() to obtain the list of internal
7544 * or user defined function names.
7545 */
7546 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007547get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548{
7549 static int intidx = -1;
7550 char_u *name;
7551
7552 if (idx == 0)
7553 intidx = -1;
7554 if (intidx < 0)
7555 {
7556 name = get_user_func_name(xp, idx);
7557 if (name != NULL)
7558 return name;
7559 }
7560 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7561 {
7562 STRCPY(IObuff, functions[intidx].f_name);
7563 STRCAT(IObuff, "(");
7564 if (functions[intidx].f_max_argc == 0)
7565 STRCAT(IObuff, ")");
7566 return IObuff;
7567 }
7568
7569 return NULL;
7570}
7571
7572/*
7573 * Function given to ExpandGeneric() to obtain the list of internal or
7574 * user defined variable or function names.
7575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007577get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578{
7579 static int intidx = -1;
7580 char_u *name;
7581
7582 if (idx == 0)
7583 intidx = -1;
7584 if (intidx < 0)
7585 {
7586 name = get_function_name(xp, idx);
7587 if (name != NULL)
7588 return name;
7589 }
7590 return get_user_var_name(xp, ++intidx);
7591}
7592
7593#endif /* FEAT_CMDL_COMPL */
7594
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007595#if defined(EBCDIC) || defined(PROTO)
7596/*
7597 * Compare struct fst by function name.
7598 */
7599 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007600compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007601{
7602 struct fst *p1 = (struct fst *)s1;
7603 struct fst *p2 = (struct fst *)s2;
7604
7605 return STRCMP(p1->f_name, p2->f_name);
7606}
7607
7608/*
7609 * Sort the function table by function name.
7610 * The sorting of the table above is ASCII dependant.
7611 * On machines using EBCDIC we have to sort it.
7612 */
7613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007614sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007615{
7616 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7617
7618 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
7619}
7620#endif
7621
7622
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623/*
7624 * Find internal function in table above.
7625 * Return index, or -1 if not found
7626 */
7627 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007628find_internal_func(
7629 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630{
7631 int first = 0;
7632 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7633 int cmp;
7634 int x;
7635
7636 /*
7637 * Find the function name in the table. Binary search.
7638 */
7639 while (first <= last)
7640 {
7641 x = first + ((unsigned)(last - first) >> 1);
7642 cmp = STRCMP(name, functions[x].f_name);
7643 if (cmp < 0)
7644 last = x - 1;
7645 else if (cmp > 0)
7646 first = x + 1;
7647 else
7648 return x;
7649 }
7650 return -1;
7651}
7652
7653/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007654 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7655 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01007656 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
7657 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007658 */
7659 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01007660deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007661{
Bram Moolenaar33570922005-01-25 22:26:29 +00007662 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007663 int cc;
7664
Bram Moolenaar65639032016-03-16 21:40:30 +01007665 if (partialp != NULL)
7666 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007667
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007668 cc = name[*lenp];
7669 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01007670 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007671 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007672 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007673 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007674 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007675 {
7676 *lenp = 0;
7677 return (char_u *)""; /* just in case */
7678 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007679 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007680 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007681 }
7682
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007683 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
7684 {
Bram Moolenaar65639032016-03-16 21:40:30 +01007685 partial_T *pt = v->di_tv.vval.v_partial;
7686
7687 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007688 {
7689 *lenp = 0;
7690 return (char_u *)""; /* just in case */
7691 }
Bram Moolenaar65639032016-03-16 21:40:30 +01007692 if (partialp != NULL)
7693 *partialp = pt;
7694 *lenp = (int)STRLEN(pt->pt_name);
7695 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007696 }
7697
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007698 return name;
7699}
7700
7701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 * Allocate a variable for the result of a function.
7703 * Return OK or FAIL.
7704 */
7705 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007706get_func_tv(
7707 char_u *name, /* name of the function */
7708 int len, /* length of "name" */
7709 typval_T *rettv,
7710 char_u **arg, /* argument, pointing to the '(' */
7711 linenr_T firstline, /* first line of range */
7712 linenr_T lastline, /* last line of range */
7713 int *doesrange, /* return: function handled range */
7714 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007715 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01007716 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717{
7718 char_u *argp;
7719 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007720 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 int argcount = 0; /* number of arguments found */
7722
7723 /*
7724 * Get the arguments.
7725 */
7726 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007727 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 {
7729 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7730 if (*argp == ')' || *argp == ',' || *argp == NUL)
7731 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7733 {
7734 ret = FAIL;
7735 break;
7736 }
7737 ++argcount;
7738 if (*argp != ',')
7739 break;
7740 }
7741 if (*argp == ')')
7742 ++argp;
7743 else
7744 ret = FAIL;
7745
7746 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007747 {
7748 int i = 0;
7749
7750 if (get_vim_var_nr(VV_TESTING))
7751 {
Bram Moolenaar8e8df252016-05-25 21:23:21 +02007752 /* Prepare for calling test_garbagecollect_now(), need to know
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007753 * what variables are used on the call stack. */
7754 if (funcargs.ga_itemsize == 0)
7755 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
7756 for (i = 0; i < argcount; ++i)
7757 if (ga_grow(&funcargs, 1) == OK)
7758 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
7759 &argvars[i];
7760 }
7761
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007762 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007763 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007764
7765 funcargs.ga_len -= i;
7766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007768 {
7769 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00007770 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007771 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00007772 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774
7775 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007776 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777
7778 *arg = skipwhite(argp);
7779 return ret;
7780}
7781
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01007782#define ERROR_UNKNOWN 0
7783#define ERROR_TOOMANY 1
7784#define ERROR_TOOFEW 2
7785#define ERROR_SCRIPT 3
7786#define ERROR_DICT 4
7787#define ERROR_NONE 5
7788#define ERROR_OTHER 6
7789#define FLEN_FIXED 40
7790
7791/*
7792 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7793 * Change <SNR>123_name() to K_SNR 123_name().
7794 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
7795 * (slow).
7796 */
7797 static char_u *
7798fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
7799{
7800 int llen;
7801 char_u *fname;
7802 int i;
7803
7804 llen = eval_fname_script(name);
7805 if (llen > 0)
7806 {
7807 fname_buf[0] = K_SPECIAL;
7808 fname_buf[1] = KS_EXTRA;
7809 fname_buf[2] = (int)KE_SNR;
7810 i = 3;
7811 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7812 {
7813 if (current_SID <= 0)
7814 *error = ERROR_SCRIPT;
7815 else
7816 {
7817 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7818 i = (int)STRLEN(fname_buf);
7819 }
7820 }
7821 if (i + STRLEN(name + llen) < FLEN_FIXED)
7822 {
7823 STRCPY(fname_buf + i, name + llen);
7824 fname = fname_buf;
7825 }
7826 else
7827 {
7828 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7829 if (fname == NULL)
7830 *error = ERROR_OTHER;
7831 else
7832 {
7833 *tofree = fname;
7834 mch_memmove(fname, fname_buf, (size_t)i);
7835 STRCPY(fname + i, name + llen);
7836 }
7837 }
7838 }
7839 else
7840 fname = name;
7841 return fname;
7842}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843
7844/*
7845 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007846 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00007847 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01007849 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007850call_func(
7851 char_u *funcname, /* name of the function */
7852 int len, /* length of "name" */
7853 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007854 int argcount_in, /* number of "argvars" */
7855 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007856 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01007857 linenr_T firstline, /* first line of range */
7858 linenr_T lastline, /* last line of range */
7859 int *doesrange, /* return: function handled range */
7860 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007861 partial_T *partial, /* optional, can be NULL */
7862 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863{
7864 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 int error = ERROR_NONE;
7866 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01007869 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02007871 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007872 int argcount = argcount_in;
7873 typval_T *argvars = argvars_in;
7874 dict_T *selfdict = selfdict_in;
7875 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
7876 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02007877
7878 /* Make a copy of the name, if it comes from a funcref variable it could
7879 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02007880 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02007881 if (name == NULL)
7882 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01007884 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885
7886 *doesrange = FALSE;
7887
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007888 if (partial != NULL)
7889 {
Bram Moolenaar1d429612016-05-24 15:44:17 +02007890 /* When the function has a partial with a dict and there is a dict
7891 * argument, use the dict argument. That is backwards compatible.
7892 * When the dict was bound explicitly use the one from the partial. */
7893 if (partial->pt_dict != NULL
7894 && (selfdict_in == NULL || !partial->pt_auto))
7895 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007896 if (error == ERROR_NONE && partial->pt_argc > 0)
7897 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007898 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
7899 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
7900 for (i = 0; i < argcount_in; ++i)
7901 argv[i + argv_clear] = argvars_in[i];
7902 argvars = argv;
7903 argcount = partial->pt_argc + argcount_in;
7904 }
7905 }
7906
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907
7908 /* execute the function if no errors detected and executing */
7909 if (evaluate && error == ERROR_NONE)
7910 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02007911 char_u *rfname = fname;
7912
7913 /* Ignore "g:" before a function name. */
7914 if (fname[0] == 'g' && fname[1] == ':')
7915 rfname = fname + 2;
7916
Bram Moolenaar798b30b2009-04-22 10:56:16 +00007917 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
7918 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 error = ERROR_UNKNOWN;
7920
Bram Moolenaara4f317d2014-04-24 17:12:33 +02007921 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 {
7923 /*
7924 * User defined function.
7925 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02007926 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007927
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007929 /* Trigger FuncUndefined event, may load the function. */
7930 if (fp == NULL
7931 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02007932 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007933 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007935 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02007936 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 }
7938#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007939 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02007940 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007941 {
7942 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02007943 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007944 }
7945
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 if (fp != NULL)
7947 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007948 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007950 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007952 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007954 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007955 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 else
7957 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01007958 int did_save_redo = FALSE;
7959
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 /*
7961 * Call the user function.
7962 * Save and restore search patterns, script variables and
7963 * redo buffer.
7964 */
7965 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01007966#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01007967 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01007968#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01007969 {
7970 saveRedobuff();
7971 did_save_redo = TRUE;
7972 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007973 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007974 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007975 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007976 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02007977 if (--fp->uf_calls <= 0 && (isdigit(*fp->uf_name)
7978 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007979 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007980 /* Function was unreferenced while being used, free it
7981 * now. */
7982 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01007983 if (did_save_redo)
7984 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 restore_search_patterns();
7986 error = ERROR_NONE;
7987 }
7988 }
7989 }
7990 else
7991 {
7992 /*
7993 * Find the function name in the table, call its implementation.
7994 */
7995 i = find_internal_func(fname);
7996 if (i >= 0)
7997 {
7998 if (argcount < functions[i].f_min_argc)
7999 error = ERROR_TOOFEW;
8000 else if (argcount > functions[i].f_max_argc)
8001 error = ERROR_TOOMANY;
8002 else
8003 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008004 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008005 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 error = ERROR_NONE;
8007 }
8008 }
8009 }
8010 /*
8011 * The function call (or "FuncUndefined" autocommand sequence) might
8012 * have been aborted by an error, an interrupt, or an explicitly thrown
8013 * exception that has not been caught so far. This situation can be
8014 * tested for by calling aborting(). For an error in an internal
8015 * function or for the "E132" error in call_user_func(), however, the
8016 * throw point at which the "force_abort" flag (temporarily reset by
8017 * emsg()) is normally updated has not been reached yet. We need to
8018 * update that flag first to make aborting() reliable.
8019 */
8020 update_force_abort();
8021 }
8022 if (error == ERROR_NONE)
8023 ret = OK;
8024
8025 /*
8026 * Report an error unless the argument evaluation or function call has been
8027 * cancelled due to an aborting error, an interrupt, or an exception.
8028 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008029 if (!aborting())
8030 {
8031 switch (error)
8032 {
8033 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008034 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008035 break;
8036 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008037 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008038 break;
8039 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008040 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008041 name);
8042 break;
8043 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008044 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008045 name);
8046 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008047 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008048 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008049 name);
8050 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008051 }
8052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008054 while (argv_clear > 0)
8055 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008056 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008057 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058
8059 return ret;
8060}
8061
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008062/*
8063 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008064 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008065 */
8066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008067emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008068{
8069 char_u *p;
8070
8071 if (*name == K_SPECIAL)
8072 p = concat_str((char_u *)"<SNR>", name + 3);
8073 else
8074 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008075 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008076 if (p != name)
8077 vim_free(p);
8078}
8079
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008080/*
8081 * Return TRUE for a non-zero Number and a non-empty String.
8082 */
8083 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008084non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008085{
8086 return ((argvars[0].v_type == VAR_NUMBER
8087 && argvars[0].vval.v_number != 0)
Bram Moolenaare381d3d2016-07-07 14:50:41 +02008088 || (argvars[0].v_type == VAR_SPECIAL
8089 && argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008090 || (argvars[0].v_type == VAR_STRING
8091 && argvars[0].vval.v_string != NULL
8092 && *argvars[0].vval.v_string != NUL));
8093}
8094
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095/*********************************************
8096 * Implementation of the built-in functions
8097 */
8098
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008099#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008100static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008101
8102/*
8103 * Get the float value of "argvars[0]" into "f".
8104 * Returns FAIL when the argument is not a Number or Float.
8105 */
8106 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008107get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008108{
8109 if (argvars[0].v_type == VAR_FLOAT)
8110 {
8111 *f = argvars[0].vval.v_float;
8112 return OK;
8113 }
8114 if (argvars[0].v_type == VAR_NUMBER)
8115 {
8116 *f = (float_T)argvars[0].vval.v_number;
8117 return OK;
8118 }
8119 EMSG(_("E808: Number or Float required"));
8120 return FAIL;
8121}
8122
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008123/*
8124 * "abs(expr)" function
8125 */
8126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008127f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008128{
8129 if (argvars[0].v_type == VAR_FLOAT)
8130 {
8131 rettv->v_type = VAR_FLOAT;
8132 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8133 }
8134 else
8135 {
8136 varnumber_T n;
8137 int error = FALSE;
8138
8139 n = get_tv_number_chk(&argvars[0], &error);
8140 if (error)
8141 rettv->vval.v_number = -1;
8142 else if (n > 0)
8143 rettv->vval.v_number = n;
8144 else
8145 rettv->vval.v_number = -n;
8146 }
8147}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008148
8149/*
8150 * "acos()" function
8151 */
8152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008153f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008154{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01008155 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008156
8157 rettv->v_type = VAR_FLOAT;
8158 if (get_float_arg(argvars, &f) == OK)
8159 rettv->vval.v_float = acos(f);
8160 else
8161 rettv->vval.v_float = 0.0;
8162}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008163#endif
8164
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008166 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 */
8168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008169f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170{
Bram Moolenaar33570922005-01-25 22:26:29 +00008171 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008173 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008174 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008176 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008177 && !tv_check_lock(l->lv_lock,
8178 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008179 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008180 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008181 }
8182 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008183 EMSG(_(e_listreq));
8184}
8185
8186/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008187 * "and(expr, expr)" function
8188 */
8189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008190f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008191{
8192 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8193 & get_tv_number_chk(&argvars[1], NULL);
8194}
8195
8196/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008197 * "append(lnum, string/list)" function
8198 */
8199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008200f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008201{
8202 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008203 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008204 list_T *l = NULL;
8205 listitem_T *li = NULL;
8206 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008207 long added = 0;
8208
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008209 /* When coming here from Insert mode, sync undo, so that this can be
8210 * undone separately from what was previously inserted. */
8211 if (u_sync_once == 2)
8212 {
8213 u_sync_once = 1; /* notify that u_sync() was called */
8214 u_sync(TRUE);
8215 }
8216
Bram Moolenaar0d660222005-01-07 21:51:51 +00008217 lnum = get_tv_lnum(argvars);
8218 if (lnum >= 0
8219 && lnum <= curbuf->b_ml.ml_line_count
8220 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008221 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008222 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008223 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008224 l = argvars[1].vval.v_list;
8225 if (l == NULL)
8226 return;
8227 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008228 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008229 for (;;)
8230 {
8231 if (l == NULL)
8232 tv = &argvars[1]; /* append a string */
8233 else if (li == NULL)
8234 break; /* end of list */
8235 else
8236 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008237 line = get_tv_string_chk(tv);
8238 if (line == NULL) /* type error */
8239 {
8240 rettv->vval.v_number = 1; /* Failed */
8241 break;
8242 }
8243 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008244 ++added;
8245 if (l == NULL)
8246 break;
8247 li = li->li_next;
8248 }
8249
8250 appended_lines_mark(lnum, added);
8251 if (curwin->w_cursor.lnum > lnum)
8252 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008254 else
8255 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256}
8257
8258/*
8259 * "argc()" function
8260 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008262f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008264 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265}
8266
8267/*
8268 * "argidx()" function
8269 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008271f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008273 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274}
8275
8276/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008277 * "arglistid()" function
8278 */
8279 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008280f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008281{
8282 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008283
8284 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01008285 wp = find_tabwin(&argvars[0], &argvars[1]);
8286 if (wp != NULL)
8287 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008288}
8289
8290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 * "argv(nr)" function
8292 */
8293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008294f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295{
8296 int idx;
8297
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008298 if (argvars[0].v_type != VAR_UNKNOWN)
8299 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02008300 idx = (int)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008301 if (idx >= 0 && idx < ARGCOUNT)
8302 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8303 else
8304 rettv->vval.v_string = NULL;
8305 rettv->v_type = VAR_STRING;
8306 }
8307 else if (rettv_list_alloc(rettv) == OK)
8308 for (idx = 0; idx < ARGCOUNT; ++idx)
8309 list_append_string(rettv->vval.v_list,
8310 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311}
8312
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008313typedef enum
8314{
8315 ASSERT_EQUAL,
8316 ASSERT_NOTEQUAL,
8317 ASSERT_MATCH,
8318 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02008319 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008320} assert_type_T;
8321
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008322static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008323static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv, assert_type_T is_match);
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008324static void assert_error(garray_T *gap);
8325static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008326
8327/*
8328 * Prepare "gap" for an assert error and add the sourcing position.
8329 */
8330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008331prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008332{
8333 char buf[NUMBUFLEN];
8334
8335 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01008336 if (sourcing_name != NULL)
8337 {
8338 ga_concat(gap, sourcing_name);
8339 if (sourcing_lnum > 0)
8340 ga_concat(gap, (char_u *)" ");
8341 }
8342 if (sourcing_lnum > 0)
8343 {
8344 sprintf(buf, "line %ld", (long)sourcing_lnum);
8345 ga_concat(gap, (char_u *)buf);
8346 }
8347 if (sourcing_name != NULL || sourcing_lnum > 0)
8348 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008349}
8350
8351/*
Bram Moolenaar23689172016-02-15 22:37:37 +01008352 * Append "str" to "gap", escaping unprintable characters.
8353 * Changes NL to \n, CR to \r, etc.
8354 */
8355 static void
8356ga_concat_esc(garray_T *gap, char_u *str)
8357{
8358 char_u *p;
8359 char_u buf[NUMBUFLEN];
8360
Bram Moolenaarf1551962016-03-15 12:55:58 +01008361 if (str == NULL)
8362 {
8363 ga_concat(gap, (char_u *)"NULL");
8364 return;
8365 }
8366
Bram Moolenaar23689172016-02-15 22:37:37 +01008367 for (p = str; *p != NUL; ++p)
8368 switch (*p)
8369 {
8370 case BS: ga_concat(gap, (char_u *)"\\b"); break;
8371 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
8372 case FF: ga_concat(gap, (char_u *)"\\f"); break;
8373 case NL: ga_concat(gap, (char_u *)"\\n"); break;
8374 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
8375 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
8376 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
8377 default:
8378 if (*p < ' ')
8379 {
8380 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
8381 ga_concat(gap, buf);
8382 }
8383 else
8384 ga_append(gap, *p);
8385 break;
8386 }
8387}
8388
8389/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008390 * Fill "gap" with information about an assert error.
8391 */
8392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008393fill_assert_error(
8394 garray_T *gap,
8395 typval_T *opt_msg_tv,
8396 char_u *exp_str,
8397 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008398 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008399 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008400{
8401 char_u numbuf[NUMBUFLEN];
8402 char_u *tofree;
8403
8404 if (opt_msg_tv->v_type != VAR_UNKNOWN)
8405 {
8406 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
8407 vim_free(tofree);
8408 }
8409 else
8410 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008411 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008412 ga_concat(gap, (char_u *)"Pattern ");
8413 else
8414 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008415 if (exp_str == NULL)
8416 {
Bram Moolenaar23689172016-02-15 22:37:37 +01008417 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008418 vim_free(tofree);
8419 }
8420 else
Bram Moolenaar23689172016-02-15 22:37:37 +01008421 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008422 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008423 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008424 else if (atype == ASSERT_NOTMATCH)
8425 ga_concat(gap, (char_u *)" does match ");
8426 else if (atype == ASSERT_NOTEQUAL)
8427 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008428 else
8429 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01008430 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008431 vim_free(tofree);
8432 }
8433}
Bram Moolenaar43345542015-11-29 17:35:35 +01008434
8435/*
8436 * Add an assert error to v:errors.
8437 */
8438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008439assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01008440{
8441 struct vimvar *vp = &vimvars[VV_ERRORS];
8442
8443 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
8444 /* Make sure v:errors is a list. */
8445 set_vim_var_list(VV_ERRORS, list_alloc());
8446 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
8447}
8448
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008449 static void
8450assert_equal_common(typval_T *argvars, assert_type_T atype)
8451{
8452 garray_T ga;
8453
8454 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
8455 != (atype == ASSERT_EQUAL))
8456 {
8457 prepare_assert_error(&ga);
8458 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
8459 atype);
8460 assert_error(&ga);
8461 ga_clear(&ga);
8462 }
8463}
8464
Bram Moolenaar43345542015-11-29 17:35:35 +01008465/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008466 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01008467 */
8468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008469f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01008470{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008471 assert_equal_common(argvars, ASSERT_EQUAL);
8472}
Bram Moolenaar43345542015-11-29 17:35:35 +01008473
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008474/*
8475 * "assert_notequal(expected, actual[, msg])" function
8476 */
8477 static void
8478f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
8479{
8480 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01008481}
8482
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008483/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008484 * "assert_exception(string[, msg])" function
8485 */
8486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008487f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008488{
8489 garray_T ga;
8490 char *error;
8491
8492 error = (char *)get_tv_string_chk(&argvars[0]);
8493 if (vimvars[VV_EXCEPTION].vv_str == NULL)
8494 {
8495 prepare_assert_error(&ga);
8496 ga_concat(&ga, (char_u *)"v:exception is not set");
8497 assert_error(&ga);
8498 ga_clear(&ga);
8499 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01008500 else if (error != NULL
8501 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008502 {
8503 prepare_assert_error(&ga);
8504 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008505 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008506 assert_error(&ga);
8507 ga_clear(&ga);
8508 }
8509}
8510
8511/*
Bram Moolenaara260b872016-01-15 20:48:22 +01008512 * "assert_fails(cmd [, error])" function
8513 */
8514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008515f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01008516{
8517 char_u *cmd = get_tv_string_chk(&argvars[0]);
8518 garray_T ga;
8519
8520 called_emsg = FALSE;
8521 suppress_errthrow = TRUE;
8522 emsg_silent = TRUE;
8523 do_cmdline_cmd(cmd);
8524 if (!called_emsg)
8525 {
8526 prepare_assert_error(&ga);
8527 ga_concat(&ga, (char_u *)"command did not fail: ");
8528 ga_concat(&ga, cmd);
8529 assert_error(&ga);
8530 ga_clear(&ga);
8531 }
8532 else if (argvars[1].v_type != VAR_UNKNOWN)
8533 {
8534 char_u buf[NUMBUFLEN];
8535 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
8536
Bram Moolenaar1abb5022016-03-15 13:33:55 +01008537 if (error == NULL
8538 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01008539 {
8540 prepare_assert_error(&ga);
8541 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008542 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01008543 assert_error(&ga);
8544 ga_clear(&ga);
8545 }
8546 }
8547
8548 called_emsg = FALSE;
8549 suppress_errthrow = FALSE;
8550 emsg_silent = FALSE;
8551 emsg_on_display = FALSE;
8552 set_vim_var_string(VV_ERRMSG, NULL, 0);
8553}
8554
8555/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008556 * Common for assert_true() and assert_false().
8557 */
Bram Moolenaar43345542015-11-29 17:35:35 +01008558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008559assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01008560{
8561 int error = FALSE;
8562 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01008563
Bram Moolenaar37127922016-02-06 20:29:28 +01008564 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01008565 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01008566 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01008567 if (argvars[0].v_type != VAR_NUMBER
8568 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
8569 || error)
8570 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008571 prepare_assert_error(&ga);
8572 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01008573 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008574 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008575 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01008576 ga_clear(&ga);
8577 }
8578}
8579
8580/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008581 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01008582 */
8583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008584f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01008585{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008586 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01008587}
8588
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008589 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008590assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008591{
8592 garray_T ga;
8593 char_u buf1[NUMBUFLEN];
8594 char_u buf2[NUMBUFLEN];
8595 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
8596 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
8597
Bram Moolenaar72188e92016-03-28 22:48:29 +02008598 if (pat == NULL || text == NULL)
8599 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008600 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008601 {
8602 prepare_assert_error(&ga);
8603 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008604 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008605 assert_error(&ga);
8606 ga_clear(&ga);
8607 }
8608}
8609
8610/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008611 * "assert_match(pattern, actual[, msg])" function
8612 */
8613 static void
8614f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
8615{
8616 assert_match_common(argvars, ASSERT_MATCH);
8617}
8618
8619/*
8620 * "assert_notmatch(pattern, actual[, msg])" function
8621 */
8622 static void
8623f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
8624{
8625 assert_match_common(argvars, ASSERT_NOTMATCH);
8626}
8627
8628/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008629 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01008630 */
8631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008632f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01008633{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008634 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01008635}
8636
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008637#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008638/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008639 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008640 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008642f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008643{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01008644 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008645
8646 rettv->v_type = VAR_FLOAT;
8647 if (get_float_arg(argvars, &f) == OK)
8648 rettv->vval.v_float = asin(f);
8649 else
8650 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008651}
8652
8653/*
8654 * "atan()" function
8655 */
8656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008657f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008658{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01008659 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008660
8661 rettv->v_type = VAR_FLOAT;
8662 if (get_float_arg(argvars, &f) == OK)
8663 rettv->vval.v_float = atan(f);
8664 else
8665 rettv->vval.v_float = 0.0;
8666}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008667
8668/*
8669 * "atan2()" function
8670 */
8671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008672f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008673{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01008674 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008675
8676 rettv->v_type = VAR_FLOAT;
8677 if (get_float_arg(argvars, &fx) == OK
8678 && get_float_arg(&argvars[1], &fy) == OK)
8679 rettv->vval.v_float = atan2(fx, fy);
8680 else
8681 rettv->vval.v_float = 0.0;
8682}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008683#endif
8684
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685/*
8686 * "browse(save, title, initdir, default)" function
8687 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008689f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690{
8691#ifdef FEAT_BROWSE
8692 int save;
8693 char_u *title;
8694 char_u *initdir;
8695 char_u *defname;
8696 char_u buf[NUMBUFLEN];
8697 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008698 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02008700 save = (int)get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008701 title = get_tv_string_chk(&argvars[1]);
8702 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8703 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008705 if (error || title == NULL || initdir == NULL || defname == NULL)
8706 rettv->vval.v_string = NULL;
8707 else
8708 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008709 do_browse(save ? BROWSE_SAVE : 0,
8710 title, defname, NULL, initdir, NULL, curbuf);
8711#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008712 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008713#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008714 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008715}
8716
8717/*
8718 * "browsedir(title, initdir)" function
8719 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008721f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008722{
8723#ifdef FEAT_BROWSE
8724 char_u *title;
8725 char_u *initdir;
8726 char_u buf[NUMBUFLEN];
8727
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008728 title = get_tv_string_chk(&argvars[0]);
8729 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008730
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008731 if (title == NULL || initdir == NULL)
8732 rettv->vval.v_string = NULL;
8733 else
8734 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008735 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008737 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008739 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740}
8741
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008742static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008743
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744/*
8745 * Find a buffer by number or exact name.
8746 */
8747 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008748find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749{
8750 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008752 if (avar->v_type == VAR_NUMBER)
8753 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008754 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008756 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008757 if (buf == NULL)
8758 {
8759 /* No full path name match, try a match with a URL or a "nofile"
8760 * buffer, these don't use the full path. */
8761 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8762 if (buf->b_fname != NULL
8763 && (path_with_url(buf->b_fname)
8764#ifdef FEAT_QUICKFIX
8765 || bt_nofile(buf)
8766#endif
8767 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008768 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008769 break;
8770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 }
8772 return buf;
8773}
8774
8775/*
8776 * "bufexists(expr)" function
8777 */
8778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008779f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008781 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782}
8783
8784/*
8785 * "buflisted(expr)" function
8786 */
8787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008788f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789{
8790 buf_T *buf;
8791
8792 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008793 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794}
8795
8796/*
8797 * "bufloaded(expr)" function
8798 */
8799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008800f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801{
8802 buf_T *buf;
8803
8804 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008805 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806}
8807
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01008808 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01008809buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 int save_magic;
8812 char_u *save_cpo;
8813 buf_T *buf;
8814
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8816 save_magic = p_magic;
8817 p_magic = TRUE;
8818 save_cpo = p_cpo;
8819 p_cpo = (char_u *)"";
8820
8821 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01008822 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823
8824 p_magic = save_magic;
8825 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01008826 return buf;
8827}
8828
8829/*
8830 * Get buffer by number or pattern.
8831 */
8832 static buf_T *
8833get_buf_tv(typval_T *tv, int curtab_only)
8834{
8835 char_u *name = tv->vval.v_string;
8836 buf_T *buf;
8837
8838 if (tv->v_type == VAR_NUMBER)
8839 return buflist_findnr((int)tv->vval.v_number);
8840 if (tv->v_type != VAR_STRING)
8841 return NULL;
8842 if (name == NULL || *name == NUL)
8843 return curbuf;
8844 if (name[0] == '$' && name[1] == NUL)
8845 return lastbuf;
8846
8847 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848
8849 /* If not found, try expanding the name, like done for bufexists(). */
8850 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008851 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852
8853 return buf;
8854}
8855
8856/*
8857 * "bufname(expr)" function
8858 */
8859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008860f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861{
8862 buf_T *buf;
8863
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008864 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01008866 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008867 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008869 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008871 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 --emsg_off;
8873}
8874
8875/*
8876 * "bufnr(expr)" function
8877 */
8878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008879f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880{
8881 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008882 int error = FALSE;
8883 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008885 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01008887 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008888 --emsg_off;
8889
8890 /* If the buffer isn't found and the second argument is not zero create a
8891 * new buffer. */
8892 if (buf == NULL
8893 && argvars[1].v_type != VAR_UNKNOWN
8894 && get_tv_number_chk(&argvars[1], &error) != 0
8895 && !error
8896 && (name = get_tv_string_chk(&argvars[0])) != NULL
8897 && !error)
8898 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8899
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008901 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008903 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904}
8905
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 static void
Bram Moolenaarb3619a92016-06-04 17:58:52 +02008907buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908{
8909#ifdef FEAT_WINDOWS
8910 win_T *wp;
8911 int winnr = 0;
8912#endif
8913 buf_T *buf;
8914
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008915 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01008917 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918#ifdef FEAT_WINDOWS
8919 for (wp = firstwin; wp; wp = wp->w_next)
8920 {
8921 ++winnr;
8922 if (wp->w_buffer == buf)
8923 break;
8924 }
Bram Moolenaarb3619a92016-06-04 17:58:52 +02008925 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926#else
Bram Moolenaarb3619a92016-06-04 17:58:52 +02008927 rettv->vval.v_number = (curwin->w_buffer == buf
8928 ? (get_nr ? 1 : curwin->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929#endif
8930 --emsg_off;
8931}
8932
8933/*
Bram Moolenaarb3619a92016-06-04 17:58:52 +02008934 * "bufwinid(nr)" function
8935 */
8936 static void
8937f_bufwinid(typval_T *argvars, typval_T *rettv)
8938{
8939 buf_win_common(argvars, rettv, FALSE);
8940}
8941
8942/*
8943 * "bufwinnr(nr)" function
8944 */
8945 static void
8946f_bufwinnr(typval_T *argvars, typval_T *rettv)
8947{
8948 buf_win_common(argvars, rettv, TRUE);
8949}
8950
8951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 * "byte2line(byte)" function
8953 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008955f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956{
8957#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008958 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959#else
8960 long boff = 0;
8961
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008962 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008964 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008966 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 (linenr_T)0, &boff);
8968#endif
8969}
8970
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008972byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008973{
8974#ifdef FEAT_MBYTE
8975 char_u *t;
8976#endif
8977 char_u *str;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02008978 varnumber_T idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008979
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008980 str = get_tv_string_chk(&argvars[0]);
8981 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008982 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008983 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008984 return;
8985
8986#ifdef FEAT_MBYTE
8987 t = str;
8988 for ( ; idx > 0; idx--)
8989 {
8990 if (*t == NUL) /* EOL reached */
8991 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008992 if (enc_utf8 && comp)
8993 t += utf_ptr2len(t);
8994 else
8995 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008996 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008997 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008998#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008999 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009000 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009001#endif
9002}
9003
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009004/*
9005 * "byteidx()" function
9006 */
9007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009008f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009009{
9010 byteidx(argvars, rettv, FALSE);
9011}
9012
9013/*
9014 * "byteidxcomp()" function
9015 */
9016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009017f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009018{
9019 byteidx(argvars, rettv, TRUE);
9020}
9021
Bram Moolenaardb913952012-06-29 12:54:53 +02009022 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009023func_call(
9024 char_u *name,
9025 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009026 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009027 dict_T *selfdict,
9028 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009029{
9030 listitem_T *item;
9031 typval_T argv[MAX_FUNC_ARGS + 1];
9032 int argc = 0;
9033 int dummy;
9034 int r = 0;
9035
9036 for (item = args->vval.v_list->lv_first; item != NULL;
9037 item = item->li_next)
9038 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009039 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +02009040 {
9041 EMSG(_("E699: Too many arguments"));
9042 break;
9043 }
9044 /* Make a copy of each argument. This is needed to be able to set
9045 * v_lock to VAR_FIXED in the copy without changing the original list.
9046 */
9047 copy_tv(&item->li_tv, &argv[argc++]);
9048 }
9049
9050 if (item == NULL)
9051 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9052 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009053 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +02009054
9055 /* Free the arguments. */
9056 while (argc > 0)
9057 clear_tv(&argv[--argc]);
9058
9059 return r;
9060}
9061
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009062/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009063 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009064 */
9065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009066f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009067{
9068 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009069 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00009070 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009071
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009072 if (argvars[1].v_type != VAR_LIST)
9073 {
9074 EMSG(_(e_listreq));
9075 return;
9076 }
9077 if (argvars[1].vval.v_list == NULL)
9078 return;
9079
9080 if (argvars[0].v_type == VAR_FUNC)
9081 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009082 else if (argvars[0].v_type == VAR_PARTIAL)
9083 {
9084 partial = argvars[0].vval.v_partial;
9085 func = partial->pt_name;
9086 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009087 else
9088 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009089 if (*func == NUL)
9090 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009091
Bram Moolenaare9a41262005-01-15 22:18:47 +00009092 if (argvars[2].v_type != VAR_UNKNOWN)
9093 {
9094 if (argvars[2].v_type != VAR_DICT)
9095 {
9096 EMSG(_(e_dictreq));
9097 return;
9098 }
9099 selfdict = argvars[2].vval.v_dict;
9100 }
9101
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009102 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009103}
9104
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009105#ifdef FEAT_FLOAT
9106/*
9107 * "ceil({float})" function
9108 */
9109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009110f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009111{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009112 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009113
9114 rettv->v_type = VAR_FLOAT;
9115 if (get_float_arg(argvars, &f) == OK)
9116 rettv->vval.v_float = ceil(f);
9117 else
9118 rettv->vval.v_float = 0.0;
9119}
9120#endif
9121
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01009122#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009123/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009124 * "ch_close()" function
9125 */
9126 static void
9127f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9128{
Bram Moolenaar437905c2016-04-26 19:01:05 +02009129 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009130
9131 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +01009132 {
Bram Moolenaar8b374212016-02-24 20:43:06 +01009133 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +01009134 channel_clear(channel);
9135 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009136}
9137
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01009138/*
9139 * "ch_getbufnr()" function
9140 */
9141 static void
9142f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
9143{
Bram Moolenaar437905c2016-04-26 19:01:05 +02009144 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01009145
9146 rettv->vval.v_number = -1;
9147 if (channel != NULL)
9148 {
9149 char_u *what = get_tv_string(&argvars[1]);
9150 int part;
9151
9152 if (STRCMP(what, "err") == 0)
9153 part = PART_ERR;
9154 else if (STRCMP(what, "out") == 0)
9155 part = PART_OUT;
9156 else if (STRCMP(what, "in") == 0)
9157 part = PART_IN;
9158 else
9159 part = PART_SOCK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02009160 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
9161 rettv->vval.v_number =
9162 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01009163 }
9164}
9165
Bram Moolenaar02e83b42016-02-21 20:10:26 +01009166/*
9167 * "ch_getjob()" function
9168 */
9169 static void
9170f_ch_getjob(typval_T *argvars, typval_T *rettv)
9171{
Bram Moolenaar437905c2016-04-26 19:01:05 +02009172 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +01009173
9174 if (channel != NULL)
9175 {
9176 rettv->v_type = VAR_JOB;
9177 rettv->vval.v_job = channel->ch_job;
9178 if (channel->ch_job != NULL)
9179 ++channel->ch_job->jv_refcount;
9180 }
9181}
Bram Moolenaar02e83b42016-02-21 20:10:26 +01009182
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009183/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +01009184 * "ch_info()" function
9185 */
9186 static void
9187f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
9188{
Bram Moolenaar437905c2016-04-26 19:01:05 +02009189 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01009190
9191 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
9192 channel_info(channel, rettv->vval.v_dict);
9193}
9194
9195/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +01009196 * "ch_log()" function
9197 */
9198 static void
9199f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
9200{
9201 char_u *msg = get_tv_string(&argvars[0]);
9202 channel_T *channel = NULL;
9203
9204 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +02009205 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +01009206
9207 ch_log(channel, (char *)msg);
9208}
9209
9210/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009211 * "ch_logfile()" function
9212 */
9213 static void
9214f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
9215{
9216 char_u *fname;
9217 char_u *opt = (char_u *)"";
9218 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009219
9220 fname = get_tv_string(&argvars[0]);
9221 if (argvars[1].v_type == VAR_STRING)
9222 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009223 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009224}
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009225
9226/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009227 * "ch_open()" function
9228 */
9229 static void
9230f_ch_open(typval_T *argvars, typval_T *rettv)
9231{
Bram Moolenaar77073442016-02-13 23:23:53 +01009232 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +02009233 if (check_restricted() || check_secure())
9234 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009235 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +01009236}
9237
9238/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01009239 * "ch_read()" function
9240 */
9241 static void
9242f_ch_read(typval_T *argvars, typval_T *rettv)
9243{
9244 common_channel_read(argvars, rettv, FALSE);
9245}
9246
9247/*
9248 * "ch_readraw()" function
9249 */
9250 static void
9251f_ch_readraw(typval_T *argvars, typval_T *rettv)
9252{
9253 common_channel_read(argvars, rettv, TRUE);
9254}
9255
9256/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01009257 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009258 */
9259 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01009260f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
9261{
9262 ch_expr_common(argvars, rettv, TRUE);
9263}
9264
9265/*
9266 * "ch_sendexpr()" function
9267 */
9268 static void
9269f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
9270{
9271 ch_expr_common(argvars, rettv, FALSE);
9272}
9273
9274/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01009275 * "ch_evalraw()" function
9276 */
9277 static void
9278f_ch_evalraw(typval_T *argvars, typval_T *rettv)
9279{
9280 ch_raw_common(argvars, rettv, TRUE);
9281}
9282
9283/*
9284 * "ch_sendraw()" function
9285 */
9286 static void
9287f_ch_sendraw(typval_T *argvars, typval_T *rettv)
9288{
9289 ch_raw_common(argvars, rettv, FALSE);
9290}
9291
9292/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009293 * "ch_setoptions()" function
9294 */
9295 static void
9296f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
9297{
9298 channel_T *channel;
9299 jobopt_T opt;
9300
Bram Moolenaar437905c2016-04-26 19:01:05 +02009301 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009302 if (channel == NULL)
9303 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01009304 clear_job_options(&opt);
9305 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02009306 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
9307 channel_set_options(channel, &opt);
9308 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009309}
9310
9311/*
9312 * "ch_status()" function
9313 */
9314 static void
9315f_ch_status(typval_T *argvars, typval_T *rettv)
9316{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009317 channel_T *channel;
9318
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009319 /* return an empty string by default */
9320 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009321 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009322
Bram Moolenaar437905c2016-04-26 19:01:05 +02009323 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009324 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009325}
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009326#endif
9327
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009328/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009329 * "changenr()" function
9330 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009332f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009333{
9334 rettv->vval.v_number = curbuf->b_u_seq_cur;
9335}
9336
9337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 * "char2nr(string)" function
9339 */
9340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009341f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342{
9343#ifdef FEAT_MBYTE
9344 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009345 {
9346 int utf8 = 0;
9347
9348 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009349 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaard35d7842013-01-23 17:17:10 +01009350
9351 if (utf8)
9352 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9353 else
9354 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 else
9357#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009358 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359}
9360
9361/*
9362 * "cindent(lnum)" function
9363 */
9364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009365f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366{
9367#ifdef FEAT_CINDENT
9368 pos_T pos;
9369 linenr_T lnum;
9370
9371 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009372 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9374 {
9375 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009376 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 curwin->w_cursor = pos;
9378 }
9379 else
9380#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009381 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382}
9383
9384/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009385 * "clearmatches()" function
9386 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009388f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009389{
9390#ifdef FEAT_SEARCH_EXTRA
9391 clear_matches(curwin);
9392#endif
9393}
9394
9395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 * "col(string)" function
9397 */
9398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009399f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400{
9401 colnr_T col = 0;
9402 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009403 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009405 fp = var2fpos(&argvars[0], FALSE, &fnum);
9406 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 {
9408 if (fp->col == MAXCOL)
9409 {
9410 /* '> can be MAXCOL, get the length of the line then */
9411 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009412 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 else
9414 col = MAXCOL;
9415 }
9416 else
9417 {
9418 col = fp->col + 1;
9419#ifdef FEAT_VIRTUALEDIT
9420 /* col(".") when the cursor is on the NUL at the end of the line
9421 * because of "coladd" can be seen as an extra column. */
9422 if (virtual_active() && fp == &curwin->w_cursor)
9423 {
9424 char_u *p = ml_get_cursor();
9425
9426 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9427 curwin->w_virtcol - curwin->w_cursor.coladd))
9428 {
9429# ifdef FEAT_MBYTE
9430 int l;
9431
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009432 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 col += l;
9434# else
9435 if (*p != NUL && p[1] == NUL)
9436 ++col;
9437# endif
9438 }
9439 }
9440#endif
9441 }
9442 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009443 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444}
9445
Bram Moolenaar572cb562005-08-05 21:35:02 +00009446#if defined(FEAT_INS_EXPAND)
9447/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009448 * "complete()" function
9449 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009451f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +00009452{
9453 int startcol;
9454
9455 if ((State & INSERT) == 0)
9456 {
9457 EMSG(_("E785: complete() can only be used in Insert mode"));
9458 return;
9459 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009460
9461 /* Check for undo allowed here, because if something was already inserted
9462 * the line was already saved for undo and this check isn't done. */
9463 if (!undo_allowed())
9464 return;
9465
Bram Moolenaarade00832006-03-10 21:46:58 +00009466 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9467 {
9468 EMSG(_(e_invarg));
9469 return;
9470 }
9471
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009472 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaarade00832006-03-10 21:46:58 +00009473 if (startcol <= 0)
9474 return;
9475
9476 set_completion(startcol - 1, argvars[1].vval.v_list);
9477}
9478
9479/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009480 * "complete_add()" function
9481 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009483f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +00009484{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009485 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009486}
9487
9488/*
9489 * "complete_check()" function
9490 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009492f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +00009493{
9494 int saved = RedrawingDisabled;
9495
9496 RedrawingDisabled = 0;
9497 ins_compl_check_keys(0);
9498 rettv->vval.v_number = compl_interrupted;
9499 RedrawingDisabled = saved;
9500}
9501#endif
9502
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503/*
9504 * "confirm(message, buttons[, default [, type]])" function
9505 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009507f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508{
9509#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9510 char_u *message;
9511 char_u *buttons = NULL;
9512 char_u buf[NUMBUFLEN];
9513 char_u buf2[NUMBUFLEN];
9514 int def = 1;
9515 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009516 char_u *typestr;
9517 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009519 message = get_tv_string_chk(&argvars[0]);
9520 if (message == NULL)
9521 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009522 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009524 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9525 if (buttons == NULL)
9526 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009527 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009529 def = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009530 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009532 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9533 if (typestr == NULL)
9534 error = TRUE;
9535 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009537 switch (TOUPPER_ASC(*typestr))
9538 {
9539 case 'E': type = VIM_ERROR; break;
9540 case 'Q': type = VIM_QUESTION; break;
9541 case 'I': type = VIM_INFO; break;
9542 case 'W': type = VIM_WARNING; break;
9543 case 'G': type = VIM_GENERIC; break;
9544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 }
9546 }
9547 }
9548 }
9549
9550 if (buttons == NULL || *buttons == NUL)
9551 buttons = (char_u *)_("&Ok");
9552
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009553 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009554 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009555 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556#endif
9557}
9558
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009559/*
9560 * "copy()" function
9561 */
9562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009563f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009564{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009565 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009566}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009568#ifdef FEAT_FLOAT
9569/*
9570 * "cos()" function
9571 */
9572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009573f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009574{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009575 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009576
9577 rettv->v_type = VAR_FLOAT;
9578 if (get_float_arg(argvars, &f) == OK)
9579 rettv->vval.v_float = cos(f);
9580 else
9581 rettv->vval.v_float = 0.0;
9582}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009583
9584/*
9585 * "cosh()" function
9586 */
9587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009588f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009589{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009590 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009591
9592 rettv->v_type = VAR_FLOAT;
9593 if (get_float_arg(argvars, &f) == OK)
9594 rettv->vval.v_float = cosh(f);
9595 else
9596 rettv->vval.v_float = 0.0;
9597}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009598#endif
9599
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009601 * "count()" function
9602 */
9603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009604f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009605{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009606 long n = 0;
9607 int ic = FALSE;
9608
Bram Moolenaare9a41262005-01-15 22:18:47 +00009609 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009610 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009611 listitem_T *li;
9612 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009613 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009614
Bram Moolenaare9a41262005-01-15 22:18:47 +00009615 if ((l = argvars[0].vval.v_list) != NULL)
9616 {
9617 li = l->lv_first;
9618 if (argvars[2].v_type != VAR_UNKNOWN)
9619 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009620 int error = FALSE;
9621
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009622 ic = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009623 if (argvars[3].v_type != VAR_UNKNOWN)
9624 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009625 idx = (long)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009626 if (!error)
9627 {
9628 li = list_find(l, idx);
9629 if (li == NULL)
9630 EMSGN(_(e_listidx), idx);
9631 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009632 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009633 if (error)
9634 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009635 }
9636
9637 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009638 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009639 ++n;
9640 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009641 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009642 else if (argvars[0].v_type == VAR_DICT)
9643 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009644 int todo;
9645 dict_T *d;
9646 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009647
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009648 if ((d = argvars[0].vval.v_dict) != NULL)
9649 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009650 int error = FALSE;
9651
Bram Moolenaare9a41262005-01-15 22:18:47 +00009652 if (argvars[2].v_type != VAR_UNKNOWN)
9653 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009654 ic = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009655 if (argvars[3].v_type != VAR_UNKNOWN)
9656 EMSG(_(e_invarg));
9657 }
9658
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009659 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009660 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009661 {
9662 if (!HASHITEM_EMPTY(hi))
9663 {
9664 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009665 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009666 ++n;
9667 }
9668 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009669 }
9670 }
9671 else
9672 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009673 rettv->vval.v_number = n;
9674}
9675
9676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9678 *
9679 * Checks the existence of a cscope connection.
9680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009682f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683{
9684#ifdef FEAT_CSCOPE
9685 int num = 0;
9686 char_u *dbpath = NULL;
9687 char_u *prepend = NULL;
9688 char_u buf[NUMBUFLEN];
9689
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009690 if (argvars[0].v_type != VAR_UNKNOWN
9691 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009693 num = (int)get_tv_number(&argvars[0]);
9694 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009695 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 }
9698
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009699 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700#endif
9701}
9702
9703/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +01009704 * "cursor(lnum, col)" function, or
9705 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009707 * Moves the cursor to the specified line and column.
9708 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009711f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712{
9713 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009714#ifdef FEAT_VIRTUALEDIT
9715 long coladd = 0;
9716#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +01009717 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009719 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009720 if (argvars[1].v_type == VAR_UNKNOWN)
9721 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009722 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +02009723 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009724
Bram Moolenaar493c1782014-05-28 14:34:46 +02009725 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +01009726 {
9727 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +00009728 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +01009729 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009730 line = pos.lnum;
9731 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009732#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009733 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009734#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +02009735 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +01009736 {
Bram Moolenaar493c1782014-05-28 14:34:46 +02009737 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +01009738 set_curswant = FALSE;
9739 }
Bram Moolenaara5525202006-03-02 22:52:09 +00009740 }
9741 else
9742 {
9743 line = get_tv_lnum(argvars);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009744 col = (long)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaara5525202006-03-02 22:52:09 +00009745#ifdef FEAT_VIRTUALEDIT
9746 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009747 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaara5525202006-03-02 22:52:09 +00009748#endif
9749 }
9750 if (line < 0 || col < 0
9751#ifdef FEAT_VIRTUALEDIT
9752 || coladd < 0
9753#endif
9754 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009755 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 if (line > 0)
9757 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 if (col > 0)
9759 curwin->w_cursor.col = col - 1;
9760#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009761 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762#endif
9763
9764 /* Make sure the cursor is in a valid position. */
9765 check_cursor();
9766#ifdef FEAT_MBYTE
9767 /* Correct cursor for multi-byte character. */
9768 if (has_mbyte)
9769 mb_adjust_cursor();
9770#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009771
Bram Moolenaarc21d67e2015-12-31 22:27:55 +01009772 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009773 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774}
9775
9776/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009777 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 */
9779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009780f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009782 int noref = 0;
9783
9784 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009785 noref = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009786 if (noref < 0 || noref > 1)
9787 EMSG(_(e_invarg));
9788 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009789 {
9790 current_copyID += COPYID_INC;
9791 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793}
9794
9795/*
9796 * "delete()" function
9797 */
9798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009799f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800{
Bram Moolenaarda440d22016-01-16 21:27:23 +01009801 char_u nbuf[NUMBUFLEN];
9802 char_u *name;
9803 char_u *flags;
9804
9805 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +01009807 return;
9808
9809 name = get_tv_string(&argvars[0]);
9810 if (name == NULL || *name == NUL)
9811 {
9812 EMSG(_(e_invarg));
9813 return;
9814 }
9815
9816 if (argvars[1].v_type != VAR_UNKNOWN)
9817 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 else
Bram Moolenaarda440d22016-01-16 21:27:23 +01009819 flags = (char_u *)"";
9820
9821 if (*flags == NUL)
9822 /* delete a file */
9823 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
9824 else if (STRCMP(flags, "d") == 0)
9825 /* delete an empty directory */
9826 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
9827 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +01009828 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +01009829 rettv->vval.v_number = delete_recursive(name);
9830 else
9831 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832}
9833
9834/*
9835 * "did_filetype()" function
9836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009838f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839{
9840#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009841 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842#endif
9843}
9844
9845/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009846 * "diff_filler()" function
9847 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009849f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +00009850{
9851#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009852 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009853#endif
9854}
9855
9856/*
9857 * "diff_hlID()" function
9858 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009860f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +00009861{
9862#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009863 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009864 static linenr_T prev_lnum = 0;
9865 static int changedtick = 0;
9866 static int fnum = 0;
9867 static int change_start = 0;
9868 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009869 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009870 int filler_lines;
9871 int col;
9872
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009873 if (lnum < 0) /* ignore type error in {lnum} arg */
9874 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009875 if (lnum != prev_lnum
9876 || changedtick != curbuf->b_changedtick
9877 || fnum != curbuf->b_fnum)
9878 {
9879 /* New line, buffer, change: need to get the values. */
9880 filler_lines = diff_check(curwin, lnum);
9881 if (filler_lines < 0)
9882 {
9883 if (filler_lines == -1)
9884 {
9885 change_start = MAXCOL;
9886 change_end = -1;
9887 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9888 hlID = HLF_ADD; /* added line */
9889 else
9890 hlID = HLF_CHD; /* changed line */
9891 }
9892 else
9893 hlID = HLF_ADD; /* added line */
9894 }
9895 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009896 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009897 prev_lnum = lnum;
9898 changedtick = curbuf->b_changedtick;
9899 fnum = curbuf->b_fnum;
9900 }
9901
9902 if (hlID == HLF_CHD || hlID == HLF_TXD)
9903 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009904 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009905 if (col >= change_start && col <= change_end)
9906 hlID = HLF_TXD; /* changed text */
9907 else
9908 hlID = HLF_CHD; /* changed line */
9909 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009910 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009911#endif
9912}
9913
9914/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009915 * "empty({expr})" function
9916 */
9917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009918f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009919{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01009920 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009921
9922 switch (argvars[0].v_type)
9923 {
9924 case VAR_STRING:
9925 case VAR_FUNC:
9926 n = argvars[0].vval.v_string == NULL
9927 || *argvars[0].vval.v_string == NUL;
9928 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009929 case VAR_PARTIAL:
9930 n = FALSE;
9931 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009932 case VAR_NUMBER:
9933 n = argvars[0].vval.v_number == 0;
9934 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009935 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009936#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009937 n = argvars[0].vval.v_float == 0.0;
9938 break;
9939#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009940 case VAR_LIST:
9941 n = argvars[0].vval.v_list == NULL
9942 || argvars[0].vval.v_list->lv_first == NULL;
9943 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009944 case VAR_DICT:
9945 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009946 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009947 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +01009948 case VAR_SPECIAL:
9949 n = argvars[0].vval.v_number != VVAL_TRUE;
9950 break;
9951
Bram Moolenaar835dc632016-02-07 14:27:38 +01009952 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01009953#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01009954 n = argvars[0].vval.v_job == NULL
9955 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
9956 break;
9957#endif
9958 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01009959#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01009960 n = argvars[0].vval.v_channel == NULL
9961 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +01009962 break;
9963#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01009964 case VAR_UNKNOWN:
9965 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
9966 n = TRUE;
9967 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009968 }
9969
9970 rettv->vval.v_number = n;
9971}
9972
9973/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 * "escape({string}, {chars})" function
9975 */
9976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009977f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978{
9979 char_u buf[NUMBUFLEN];
9980
Bram Moolenaar758711c2005-02-02 23:11:38 +00009981 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9982 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009983 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984}
9985
9986/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009987 * "eval()" function
9988 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009990f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009991{
Bram Moolenaar615b9972015-01-14 17:15:05 +01009992 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009993
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009994 s = get_tv_string_chk(&argvars[0]);
9995 if (s != NULL)
9996 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009997
Bram Moolenaar615b9972015-01-14 17:15:05 +01009998 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009999 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10000 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010001 if (p != NULL && !aborting())
10002 EMSG2(_(e_invexpr2), p);
10003 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010004 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010005 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010006 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010007 else if (*s != NUL)
10008 EMSG(_(e_trailing));
10009}
10010
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020010011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 * "eventhandler()" function
10013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010015f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010017 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018}
10019
10020/*
10021 * "executable()" function
10022 */
10023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010024f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010026 char_u *name = get_tv_string(&argvars[0]);
10027
10028 /* Check in $PATH and also check directly if there is a directory name. */
10029 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10030 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010031}
10032
Bram Moolenaar79815f12016-07-09 17:07:29 +020010033static garray_T redir_execute_ga;
10034
10035/*
10036 * Append "value[value_len]" to the execute() output.
10037 */
10038 void
10039execute_redir_str(char_u *value, int value_len)
10040{
10041 int len;
10042
10043 if (value_len == -1)
10044 len = (int)STRLEN(value); /* Append the entire string */
10045 else
10046 len = value_len; /* Append only "value_len" characters */
10047 if (ga_grow(&redir_execute_ga, len) == OK)
10048 {
10049 mch_memmove((char *)redir_execute_ga.ga_data
10050 + redir_execute_ga.ga_len, value, len);
10051 redir_execute_ga.ga_len += len;
10052 }
10053}
10054
10055/*
10056 * Get next line from a list.
10057 * Called by do_cmdline() to get the next line.
10058 * Returns allocated string, or NULL for end of function.
10059 */
10060
10061 static char_u *
10062get_list_line(
10063 int c UNUSED,
10064 void *cookie,
10065 int indent UNUSED)
10066{
10067 listitem_T **p = (listitem_T **)cookie;
10068 listitem_T *item = *p;
10069 char_u buf[NUMBUFLEN];
10070 char_u *s;
10071
10072 if (item == NULL)
10073 return NULL;
10074 s = get_tv_string_buf_chk(&item->li_tv, buf);
10075 *p = item->li_next;
10076 return s == NULL ? NULL : vim_strsave(s);
10077}
10078
10079/*
10080 * "execute()" function
10081 */
10082 static void
10083f_execute(typval_T *argvars, typval_T *rettv)
10084{
10085 char_u *cmd = NULL;
10086 list_T *list = NULL;
10087 int save_msg_silent = msg_silent;
10088 int save_emsg_silent = emsg_silent;
10089 int save_emsg_noredir = emsg_noredir;
10090 int save_redir_execute = redir_execute;
10091 garray_T save_ga;
10092
10093 rettv->vval.v_string = NULL;
10094 rettv->v_type = VAR_STRING;
10095
10096 if (argvars[0].v_type == VAR_LIST)
10097 {
10098 list = argvars[0].vval.v_list;
10099 if (list == NULL || list->lv_first == NULL)
10100 /* empty list, no commands, empty output */
10101 return;
10102 ++list->lv_refcount;
10103 }
10104 else
10105 {
10106 cmd = get_tv_string_chk(&argvars[0]);
10107 if (cmd == NULL)
10108 return;
10109 }
10110
Bram Moolenaar79815f12016-07-09 17:07:29 +020010111 if (argvars[1].v_type != VAR_UNKNOWN)
10112 {
10113 char_u buf[NUMBUFLEN];
10114 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
10115
10116 if (s == NULL)
10117 return;
10118 if (STRNCMP(s, "silent", 6) == 0)
10119 ++msg_silent;
10120 if (STRCMP(s, "silent!") == 0)
10121 {
10122 emsg_silent = TRUE;
10123 emsg_noredir = TRUE;
10124 }
10125 }
10126 else
10127 ++msg_silent;
10128
Bram Moolenaared59aa62016-07-09 17:41:12 +020010129 if (redir_execute)
10130 save_ga = redir_execute_ga;
10131 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
10132 redir_execute = TRUE;
10133
Bram Moolenaar79815f12016-07-09 17:07:29 +020010134 if (cmd != NULL)
10135 do_cmdline_cmd(cmd);
10136 else
10137 {
10138 listitem_T *item = list->lv_first;
10139
10140 do_cmdline(NULL, get_list_line, (void *)&item,
10141 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
10142 --list->lv_refcount;
10143 }
10144
10145 rettv->vval.v_string = redir_execute_ga.ga_data;
10146 msg_silent = save_msg_silent;
10147 emsg_silent = save_emsg_silent;
10148 emsg_noredir = save_emsg_noredir;
10149
10150 redir_execute = save_redir_execute;
10151 if (redir_execute)
10152 redir_execute_ga = save_ga;
10153
10154 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
10155 * line. Put it back in the first column. */
10156 msg_col = 0;
10157}
10158
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010159/*
10160 * "exepath()" function
10161 */
10162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010163f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010164{
10165 char_u *p = NULL;
10166
Bram Moolenaarb5971142015-03-21 17:32:19 +010010167 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010168 rettv->v_type = VAR_STRING;
10169 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170}
10171
10172/*
10173 * "exists()" function
10174 */
10175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010176f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177{
10178 char_u *p;
10179 char_u *name;
10180 int n = FALSE;
10181 int len = 0;
10182
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010183 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184 if (*p == '$') /* environment variable */
10185 {
10186 /* first try "normal" environment variables (fast) */
10187 if (mch_getenv(p + 1) != NULL)
10188 n = TRUE;
10189 else
10190 {
10191 /* try expanding things like $VIM and ${HOME} */
10192 p = expand_env_save(p);
10193 if (p != NULL && *p != '$')
10194 n = TRUE;
10195 vim_free(p);
10196 }
10197 }
10198 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010199 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010200 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010201 if (*skipwhite(p) != NUL)
10202 n = FALSE; /* trailing garbage */
10203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 else if (*p == '*') /* internal or user defined function */
10205 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010206 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 }
10208 else if (*p == ':')
10209 {
10210 n = cmd_exists(p + 1);
10211 }
10212 else if (*p == '#')
10213 {
10214#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010215 if (p[1] == '#')
10216 n = autocmd_supported(p + 2);
10217 else
10218 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219#endif
10220 }
10221 else /* internal variable */
10222 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010223 char_u *tofree;
10224 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010226 /* get_name_len() takes care of expanding curly braces */
10227 name = p;
10228 len = get_name_len(&p, &tofree, TRUE, FALSE);
10229 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010231 if (tofree != NULL)
10232 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010233 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010234 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010236 /* handle d.key, l[idx], f(expr) */
10237 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10238 if (n)
10239 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 }
10241 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010242 if (*p != NUL)
10243 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010245 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 }
10247
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010248 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249}
10250
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010251#ifdef FEAT_FLOAT
10252/*
10253 * "exp()" function
10254 */
10255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010256f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010257{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010258 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010259
10260 rettv->v_type = VAR_FLOAT;
10261 if (get_float_arg(argvars, &f) == OK)
10262 rettv->vval.v_float = exp(f);
10263 else
10264 rettv->vval.v_float = 0.0;
10265}
10266#endif
10267
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268/*
10269 * "expand()" function
10270 */
10271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010272f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273{
10274 char_u *s;
10275 int len;
10276 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010277 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010279 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010280 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010282 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010283 if (argvars[1].v_type != VAR_UNKNOWN
10284 && argvars[2].v_type != VAR_UNKNOWN
10285 && get_tv_number_chk(&argvars[2], &error)
10286 && !error)
10287 {
10288 rettv->v_type = VAR_LIST;
10289 rettv->vval.v_list = NULL;
10290 }
10291
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010292 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 if (*s == '%' || *s == '#' || *s == '<')
10294 {
10295 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010296 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010298 if (rettv->v_type == VAR_LIST)
10299 {
10300 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10301 list_append_string(rettv->vval.v_list, result, -1);
10302 else
10303 vim_free(result);
10304 }
10305 else
10306 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 }
10308 else
10309 {
10310 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010311 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010312 if (argvars[1].v_type != VAR_UNKNOWN
10313 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010314 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010315 if (!error)
10316 {
10317 ExpandInit(&xpc);
10318 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010319 if (p_wic)
10320 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010321 if (rettv->v_type == VAR_STRING)
10322 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10323 options, WILD_ALL);
10324 else if (rettv_list_alloc(rettv) != FAIL)
10325 {
10326 int i;
10327
10328 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10329 for (i = 0; i < xpc.xp_numfiles; i++)
10330 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10331 ExpandCleanup(&xpc);
10332 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010333 }
10334 else
10335 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 }
10337}
10338
10339/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010340 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010341 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010342 */
10343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010344f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010345{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010346 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010347
Bram Moolenaare9a41262005-01-15 22:18:47 +000010348 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010349 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010350 list_T *l1, *l2;
10351 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010352 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010353 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010354
Bram Moolenaare9a41262005-01-15 22:18:47 +000010355 l1 = argvars[0].vval.v_list;
10356 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010357 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010358 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010359 {
10360 if (argvars[2].v_type != VAR_UNKNOWN)
10361 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010362 before = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010363 if (error)
10364 return; /* type error; errmsg already given */
10365
Bram Moolenaar758711c2005-02-02 23:11:38 +000010366 if (before == l1->lv_len)
10367 item = NULL;
10368 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010369 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010370 item = list_find(l1, before);
10371 if (item == NULL)
10372 {
10373 EMSGN(_(e_listidx), before);
10374 return;
10375 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010376 }
10377 }
10378 else
10379 item = NULL;
10380 list_extend(l1, l2, item);
10381
Bram Moolenaare9a41262005-01-15 22:18:47 +000010382 copy_tv(&argvars[0], rettv);
10383 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010384 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010385 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10386 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010387 dict_T *d1, *d2;
10388 char_u *action;
10389 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010390
10391 d1 = argvars[0].vval.v_dict;
10392 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010393 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010394 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010395 {
10396 /* Check the third argument. */
10397 if (argvars[2].v_type != VAR_UNKNOWN)
10398 {
10399 static char *(av[]) = {"keep", "force", "error"};
10400
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010401 action = get_tv_string_chk(&argvars[2]);
10402 if (action == NULL)
10403 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010404 for (i = 0; i < 3; ++i)
10405 if (STRCMP(action, av[i]) == 0)
10406 break;
10407 if (i == 3)
10408 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010409 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010410 return;
10411 }
10412 }
10413 else
10414 action = (char_u *)"force";
10415
Bram Moolenaara9922d62013-05-30 13:01:18 +020010416 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010417
Bram Moolenaare9a41262005-01-15 22:18:47 +000010418 copy_tv(&argvars[0], rettv);
10419 }
10420 }
10421 else
10422 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010423}
10424
10425/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010426 * "feedkeys()" function
10427 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010429f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010430{
10431 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010432 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010433 char_u *keys, *flags;
10434 char_u nbuf[NUMBUFLEN];
10435 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010436 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020010437 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010438 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010439
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010440 /* This is not allowed in the sandbox. If the commands would still be
10441 * executed in the sandbox it would be OK, but it probably happens later,
10442 * when "sandbox" is no longer set. */
10443 if (check_secure())
10444 return;
10445
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010446 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010010447
10448 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010449 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010010450 flags = get_tv_string_buf(&argvars[1], nbuf);
10451 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010452 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010010453 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010454 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010010455 case 'n': remap = FALSE; break;
10456 case 'm': remap = TRUE; break;
10457 case 't': typed = TRUE; break;
10458 case 'i': insert = TRUE; break;
10459 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020010460 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010461 }
10462 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010010463 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010464
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010010465 if (*keys != NUL || execute)
10466 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010467 /* Need to escape K_SPECIAL and CSI before putting the string in the
10468 * typeahead buffer. */
10469 keys_esc = vim_strsave_escape_csi(keys);
10470 if (keys_esc != NULL)
10471 {
10472 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010473 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010474 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010475 if (vgetc_busy)
10476 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010477 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010010478 {
10479 int save_msg_scroll = msg_scroll;
10480
10481 /* Avoid a 1 second delay when the keys start Insert mode. */
10482 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020010483
Bram Moolenaar245c4102016-04-20 17:37:41 +020010484 if (!dangerous)
10485 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010486 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020010487 if (!dangerous)
10488 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010010489 msg_scroll |= save_msg_scroll;
10490 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010491 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010492 }
10493}
10494
10495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496 * "filereadable()" function
10497 */
10498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010499f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010501 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502 char_u *p;
10503 int n;
10504
Bram Moolenaarc236c162008-07-13 17:41:49 +000010505#ifndef O_NONBLOCK
10506# define O_NONBLOCK 0
10507#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010508 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010509 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10510 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 {
10512 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010513 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514 }
10515 else
10516 n = FALSE;
10517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010518 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519}
10520
10521/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010522 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523 * rights to write into.
10524 */
10525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010526f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010528 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529}
10530
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010531 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010010532findfilendir(
10533 typval_T *argvars UNUSED,
10534 typval_T *rettv,
10535 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010536{
10537#ifdef FEAT_SEARCHPATH
10538 char_u *fname;
10539 char_u *fresult = NULL;
10540 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10541 char_u *p;
10542 char_u pathbuf[NUMBUFLEN];
10543 int count = 1;
10544 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010545 int error = FALSE;
10546#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010547
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010548 rettv->vval.v_string = NULL;
10549 rettv->v_type = VAR_STRING;
10550
10551#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010552 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010553
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010554 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010555 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010556 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10557 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010558 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010559 else
10560 {
10561 if (*p != NUL)
10562 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010563
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010564 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010565 count = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010566 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010567 }
10568
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010569 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10570 error = TRUE;
10571
10572 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010573 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010574 do
10575 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010576 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010577 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010578 fresult = find_file_in_path_option(first ? fname : NULL,
10579 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010580 0, first, path,
10581 find_what,
10582 curbuf->b_ffname,
10583 find_what == FINDFILE_DIR
10584 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010585 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010586
10587 if (fresult != NULL && rettv->v_type == VAR_LIST)
10588 list_append_string(rettv->vval.v_list, fresult, -1);
10589
10590 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010591 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010592
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010593 if (rettv->v_type == VAR_STRING)
10594 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010595#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010596}
10597
Bram Moolenaar48e697e2016-01-23 22:17:30 +010010598static void filter_map(typval_T *argvars, typval_T *rettv, int map);
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020010599static int filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010600
10601/*
10602 * Implementation of map() and filter().
10603 */
10604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010605filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010606{
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020010607 typval_T *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010608 listitem_T *li, *nli;
10609 list_T *l = NULL;
10610 dictitem_T *di;
10611 hashtab_T *ht;
10612 hashitem_T *hi;
10613 dict_T *d = NULL;
10614 typval_T save_val;
10615 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010616 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010617 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010618 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020010619 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010620 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010621 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010622 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010623
Bram Moolenaare9a41262005-01-15 22:18:47 +000010624 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010625 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010626 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010627 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010628 return;
10629 }
10630 else if (argvars[0].v_type == VAR_DICT)
10631 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010632 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010633 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010634 return;
10635 }
10636 else
10637 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010638 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010639 return;
10640 }
10641
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020010642 expr = &argvars[1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010643 /* On type errors, the preceding call has already displayed an error
10644 * message. Avoid a misleading error message for an empty string that
10645 * was not passed as argument. */
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020010646 if (expr->v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010647 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010648 prepare_vimvar(VV_VAL, &save_val);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010649
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010650 /* We reset "did_emsg" to be able to detect whether an error
10651 * occurred during evaluation of the expression. */
10652 save_did_emsg = did_emsg;
10653 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010654
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010655 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010656 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010657 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010658 vimvars[VV_KEY].vv_type = VAR_STRING;
10659
10660 ht = &d->dv_hashtab;
10661 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010662 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010663 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010664 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010665 if (!HASHITEM_EMPTY(hi))
10666 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010667 int r;
10668
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010669 --todo;
10670 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010671 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020010672 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10673 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010674 break;
10675 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010676 r = filter_map_one(&di->di_tv, expr, map, &rem);
10677 clear_tv(&vimvars[VV_KEY].vv_tv);
10678 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010679 break;
10680 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010681 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010682 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10683 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010684 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010685 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010686 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010687 }
10688 }
10689 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010690 }
10691 else
10692 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010693 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10694
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010695 for (li = l->lv_first; li != NULL; li = nli)
10696 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010697 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010698 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010699 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010700 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010701 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010702 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010703 break;
10704 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010705 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010706 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010707 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010708 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010709
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010710 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010711 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010712
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010713 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010714 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010715
10716 copy_tv(&argvars[0], rettv);
10717}
10718
10719 static int
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020010720filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010721{
Bram Moolenaar33570922005-01-25 22:26:29 +000010722 typval_T rettv;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020010723 typval_T argv[3];
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020010724 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010725 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010726 int retval = FAIL;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020010727 int dummy;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010728
Bram Moolenaar33570922005-01-25 22:26:29 +000010729 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020010730 argv[0] = vimvars[VV_KEY].vv_tv;
10731 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020010732 if (expr->v_type == VAR_FUNC)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010733 {
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020010734 s = expr->vval.v_string;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020010735 if (call_func(s, (int)STRLEN(s),
10736 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
10737 goto theend;
10738 }
10739 else if (expr->v_type == VAR_PARTIAL)
10740 {
10741 partial_T *partial = expr->vval.v_partial;
10742
10743 s = partial->pt_name;
10744 if (call_func(s, (int)STRLEN(s),
10745 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL)
10746 == FAIL)
10747 goto theend;
10748 }
10749 else
10750 {
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020010751 s = get_tv_string_buf_chk(expr, buf);
10752 if (s == NULL)
10753 goto theend;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020010754 s = skipwhite(s);
10755 if (eval1(&s, &rettv, TRUE) == FAIL)
10756 goto theend;
10757 if (*s != NUL) /* check for trailing chars after expr */
10758 {
10759 EMSG2(_(e_invexpr2), s);
10760 goto theend;
10761 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010762 }
10763 if (map)
10764 {
10765 /* map(): replace the list item value */
10766 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010767 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010768 *tv = rettv;
10769 }
10770 else
10771 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010772 int error = FALSE;
10773
Bram Moolenaare9a41262005-01-15 22:18:47 +000010774 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010775 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010776 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010777 /* On type error, nothing has been removed; return FAIL to stop the
10778 * loop. The error message was given by get_tv_number_chk(). */
10779 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010780 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010781 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010782 retval = OK;
10783theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010784 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010785 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010786}
10787
10788/*
10789 * "filter()" function
10790 */
10791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010792f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010793{
10794 filter_map(argvars, rettv, FALSE);
10795}
10796
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010797/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010798 * "finddir({fname}[, {path}[, {count}]])" function
10799 */
10800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010801f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010802{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010803 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010804}
10805
10806/*
10807 * "findfile({fname}[, {path}[, {count}]])" function
10808 */
10809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010810f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010811{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010812 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010813}
10814
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010815#ifdef FEAT_FLOAT
10816/*
10817 * "float2nr({float})" function
10818 */
10819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010820f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010821{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010822 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010823
10824 if (get_float_arg(argvars, &f) == OK)
10825 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010826# ifdef FEAT_NUM64
10827 if (f < -0x7fffffffffffffff)
10828 rettv->vval.v_number = -0x7fffffffffffffff;
10829 else if (f > 0x7fffffffffffffff)
10830 rettv->vval.v_number = 0x7fffffffffffffff;
10831 else
10832 rettv->vval.v_number = (varnumber_T)f;
10833# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010834 if (f < -0x7fffffff)
10835 rettv->vval.v_number = -0x7fffffff;
10836 else if (f > 0x7fffffff)
10837 rettv->vval.v_number = 0x7fffffff;
10838 else
10839 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010840# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010841 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010842}
10843
10844/*
10845 * "floor({float})" function
10846 */
10847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010848f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010849{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010850 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010851
10852 rettv->v_type = VAR_FLOAT;
10853 if (get_float_arg(argvars, &f) == OK)
10854 rettv->vval.v_float = floor(f);
10855 else
10856 rettv->vval.v_float = 0.0;
10857}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010858
10859/*
10860 * "fmod()" function
10861 */
10862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010863f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010864{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010865 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010866
10867 rettv->v_type = VAR_FLOAT;
10868 if (get_float_arg(argvars, &fx) == OK
10869 && get_float_arg(&argvars[1], &fy) == OK)
10870 rettv->vval.v_float = fmod(fx, fy);
10871 else
10872 rettv->vval.v_float = 0.0;
10873}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010874#endif
10875
Bram Moolenaar0d660222005-01-07 21:51:51 +000010876/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010877 * "fnameescape({string})" function
10878 */
10879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010880f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010881{
10882 rettv->vval.v_string = vim_strsave_fnameescape(
10883 get_tv_string(&argvars[0]), FALSE);
10884 rettv->v_type = VAR_STRING;
10885}
10886
10887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 * "fnamemodify({fname}, {mods})" function
10889 */
10890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010891f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892{
10893 char_u *fname;
10894 char_u *mods;
10895 int usedlen = 0;
10896 int len;
10897 char_u *fbuf = NULL;
10898 char_u buf[NUMBUFLEN];
10899
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010900 fname = get_tv_string_chk(&argvars[0]);
10901 mods = get_tv_string_buf_chk(&argvars[1], buf);
10902 if (fname == NULL || mods == NULL)
10903 fname = NULL;
10904 else
10905 {
10906 len = (int)STRLEN(fname);
10907 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010910 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010912 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010914 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915 vim_free(fbuf);
10916}
10917
Bram Moolenaar48e697e2016-01-23 22:17:30 +010010918static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919
10920/*
10921 * "foldclosed()" function
10922 */
10923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010924foldclosed_both(
10925 typval_T *argvars UNUSED,
10926 typval_T *rettv,
10927 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928{
10929#ifdef FEAT_FOLDING
10930 linenr_T lnum;
10931 linenr_T first, last;
10932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010933 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10935 {
10936 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10937 {
10938 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010939 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010941 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 return;
10943 }
10944 }
10945#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010946 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947}
10948
10949/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010950 * "foldclosed()" function
10951 */
10952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010953f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010954{
10955 foldclosed_both(argvars, rettv, FALSE);
10956}
10957
10958/*
10959 * "foldclosedend()" function
10960 */
10961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010962f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010963{
10964 foldclosed_both(argvars, rettv, TRUE);
10965}
10966
10967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968 * "foldlevel()" function
10969 */
10970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010971f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972{
10973#ifdef FEAT_FOLDING
10974 linenr_T lnum;
10975
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010976 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010978 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980}
10981
10982/*
10983 * "foldtext()" function
10984 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010986f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987{
10988#ifdef FEAT_FOLDING
10989 linenr_T lnum;
10990 char_u *s;
10991 char_u *r;
10992 int len;
10993 char *txt;
10994#endif
10995
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010996 rettv->v_type = VAR_STRING;
10997 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010999 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11000 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11001 <= curbuf->b_ml.ml_line_count
11002 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003 {
11004 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011005 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11006 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 {
11008 if (!linewhite(lnum))
11009 break;
11010 ++lnum;
11011 }
11012
11013 /* Find interesting text in this line. */
11014 s = skipwhite(ml_get(lnum));
11015 /* skip C comment-start */
11016 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011017 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011019 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011020 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011021 {
11022 s = skipwhite(ml_get(lnum + 1));
11023 if (*s == '*')
11024 s = skipwhite(s + 1);
11025 }
11026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027 txt = _("+-%s%3ld lines: ");
11028 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011029 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011030 + 20 /* for %3ld */
11031 + STRLEN(s))); /* concatenated */
11032 if (r != NULL)
11033 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011034 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11035 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11036 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 len = (int)STRLEN(r);
11038 STRCAT(r, s);
11039 /* remove 'foldmarker' and 'commentstring' */
11040 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011041 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042 }
11043 }
11044#endif
11045}
11046
11047/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011048 * "foldtextresult(lnum)" function
11049 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011051f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011052{
11053#ifdef FEAT_FOLDING
11054 linenr_T lnum;
11055 char_u *text;
11056 char_u buf[51];
11057 foldinfo_T foldinfo;
11058 int fold_count;
11059#endif
11060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011061 rettv->v_type = VAR_STRING;
11062 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011063#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011064 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011065 /* treat illegal types and illegal string values for {lnum} the same */
11066 if (lnum < 0)
11067 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011068 fold_count = foldedCount(curwin, lnum, &foldinfo);
11069 if (fold_count > 0)
11070 {
11071 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11072 &foldinfo, buf);
11073 if (text == buf)
11074 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011075 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011076 }
11077#endif
11078}
11079
11080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081 * "foreground()" function
11082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011084f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086#ifdef FEAT_GUI
11087 if (gui.in_use)
11088 gui_mch_set_foreground();
11089#else
11090# ifdef WIN32
11091 win32_set_foreground();
11092# endif
11093#endif
11094}
11095
11096/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011097 * "function()" function
11098 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011100f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011101{
11102 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011103 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011104 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011105 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011106
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011107 if (argvars[0].v_type == VAR_FUNC)
11108 {
11109 /* function(MyFunc, [arg], dict) */
11110 s = argvars[0].vval.v_string;
11111 }
11112 else if (argvars[0].v_type == VAR_PARTIAL
11113 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011114 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011115 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011116 arg_pt = argvars[0].vval.v_partial;
11117 s = arg_pt->pt_name;
11118 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011119 else
11120 {
11121 /* function('MyFunc', [arg], dict) */
11122 s = get_tv_string(&argvars[0]);
11123 use_string = TRUE;
11124 }
11125
11126 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011127 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011128 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011129 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
11130 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011131 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011132 else
11133 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011134 int dict_idx = 0;
11135 int arg_idx = 0;
11136 list_T *list = NULL;
11137
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011138 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011139 {
11140 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011141 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011142
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011143 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11144 * also be called from another script. Using trans_function_name()
11145 * would also work, but some plugins depend on the name being
11146 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011147 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011148 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
11149 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011150 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011151 STRCPY(name, sid_buf);
11152 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011153 }
11154 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011155 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011156 name = vim_strsave(s);
11157
11158 if (argvars[1].v_type != VAR_UNKNOWN)
11159 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011160 if (argvars[2].v_type != VAR_UNKNOWN)
11161 {
11162 /* function(name, [args], dict) */
11163 arg_idx = 1;
11164 dict_idx = 2;
11165 }
11166 else if (argvars[1].v_type == VAR_DICT)
11167 /* function(name, dict) */
11168 dict_idx = 1;
11169 else
11170 /* function(name, [args]) */
11171 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010011172 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011173 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011174 if (argvars[dict_idx].v_type != VAR_DICT)
11175 {
11176 EMSG(_("E922: expected a dict"));
11177 vim_free(name);
11178 return;
11179 }
11180 if (argvars[dict_idx].vval.v_dict == NULL)
11181 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011182 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011183 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011184 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011185 if (argvars[arg_idx].v_type != VAR_LIST)
11186 {
11187 EMSG(_("E923: Second argument of function() must be a list or a dict"));
11188 vim_free(name);
11189 return;
11190 }
11191 list = argvars[arg_idx].vval.v_list;
11192 if (list == NULL || list->lv_len == 0)
11193 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011194 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011195 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011196 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010011197 {
11198 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011199
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011200 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010011201 if (pt == NULL)
11202 vim_free(name);
11203 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011204 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011205 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011206 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011207 listitem_T *li;
11208 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011209 int arg_len = 0;
11210 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011211
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011212 if (arg_pt != NULL)
11213 arg_len = arg_pt->pt_argc;
11214 if (list != NULL)
11215 lv_len = list->lv_len;
11216 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011217 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011218 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011219 if (pt->pt_argv == NULL)
11220 {
11221 vim_free(pt);
11222 vim_free(name);
11223 return;
11224 }
11225 else
11226 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011227 for (i = 0; i < arg_len; i++)
11228 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
11229 if (lv_len > 0)
11230 for (li = list->lv_first; li != NULL;
11231 li = li->li_next)
11232 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011233 }
11234 }
11235
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010011236 /* For "function(dict.func, [], dict)" and "func" is a partial
11237 * use "dict". That is backwards compatible. */
11238 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011239 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020011240 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011241 pt->pt_dict = argvars[dict_idx].vval.v_dict;
11242 ++pt->pt_dict->dv_refcount;
11243 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011244 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010011245 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020011246 /* If the dict was bound automatically the result is also
11247 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011248 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020011249 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011250 if (pt->pt_dict != NULL)
11251 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010011252 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011253
11254 pt->pt_refcount = 1;
11255 pt->pt_name = name;
11256 func_ref(pt->pt_name);
11257 }
11258 rettv->v_type = VAR_PARTIAL;
11259 rettv->vval.v_partial = pt;
11260 }
11261 else
11262 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011263 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011264 rettv->v_type = VAR_FUNC;
11265 rettv->vval.v_string = name;
11266 func_ref(name);
11267 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011268 }
11269}
11270
11271/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011272 * "garbagecollect()" function
11273 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011275f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011276{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011277 /* This is postponed until we are back at the toplevel, because we may be
11278 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11279 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011280
11281 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11282 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011283}
11284
11285/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011286 * "get()" function
11287 */
11288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011289f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011290{
Bram Moolenaar33570922005-01-25 22:26:29 +000011291 listitem_T *li;
11292 list_T *l;
11293 dictitem_T *di;
11294 dict_T *d;
11295 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011296
Bram Moolenaare9a41262005-01-15 22:18:47 +000011297 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011298 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011299 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011300 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011301 int error = FALSE;
11302
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011303 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011304 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011305 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011306 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011307 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011308 else if (argvars[0].v_type == VAR_DICT)
11309 {
11310 if ((d = argvars[0].vval.v_dict) != NULL)
11311 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011312 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011313 if (di != NULL)
11314 tv = &di->di_tv;
11315 }
11316 }
Bram Moolenaar03e19a02016-05-24 22:29:49 +020011317 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020011318 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020011319 partial_T *pt;
11320 partial_T fref_pt;
11321
11322 if (argvars[0].v_type == VAR_PARTIAL)
11323 pt = argvars[0].vval.v_partial;
11324 else
11325 {
11326 vim_memset(&fref_pt, 0, sizeof(fref_pt));
11327 fref_pt.pt_name = argvars[0].vval.v_string;
11328 pt = &fref_pt;
11329 }
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020011330
11331 if (pt != NULL)
11332 {
11333 char_u *what = get_tv_string(&argvars[1]);
11334
Bram Moolenaar03e19a02016-05-24 22:29:49 +020011335 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020011336 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020011337 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020011338 if (pt->pt_name == NULL)
11339 rettv->vval.v_string = NULL;
11340 else
11341 rettv->vval.v_string = vim_strsave(pt->pt_name);
11342 }
11343 else if (STRCMP(what, "dict") == 0)
11344 {
11345 rettv->v_type = VAR_DICT;
11346 rettv->vval.v_dict = pt->pt_dict;
11347 if (pt->pt_dict != NULL)
11348 ++pt->pt_dict->dv_refcount;
11349 }
11350 else if (STRCMP(what, "args") == 0)
11351 {
11352 rettv->v_type = VAR_LIST;
11353 if (rettv_list_alloc(rettv) == OK)
11354 {
11355 int i;
11356
11357 for (i = 0; i < pt->pt_argc; ++i)
11358 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
11359 }
11360 }
11361 else
11362 EMSG2(_(e_invarg2), what);
11363 return;
11364 }
11365 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011366 else
11367 EMSG2(_(e_listdictarg), "get()");
11368
11369 if (tv == NULL)
11370 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011371 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011372 copy_tv(&argvars[2], rettv);
11373 }
11374 else
11375 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011376}
11377
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011378static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011379
11380/*
11381 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011382 * Return a range (from start to end) of lines in rettv from the specified
11383 * buffer.
11384 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011385 */
11386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011387get_buffer_lines(
11388 buf_T *buf,
11389 linenr_T start,
11390 linenr_T end,
11391 int retlist,
11392 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011393{
11394 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011395
Bram Moolenaar959a1432013-12-14 12:17:38 +010011396 rettv->v_type = VAR_STRING;
11397 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011398 if (retlist && rettv_list_alloc(rettv) == FAIL)
11399 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011400
11401 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11402 return;
11403
11404 if (!retlist)
11405 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011406 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11407 p = ml_get_buf(buf, start, FALSE);
11408 else
11409 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011410 rettv->vval.v_string = vim_strsave(p);
11411 }
11412 else
11413 {
11414 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011415 return;
11416
11417 if (start < 1)
11418 start = 1;
11419 if (end > buf->b_ml.ml_line_count)
11420 end = buf->b_ml.ml_line_count;
11421 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011422 if (list_append_string(rettv->vval.v_list,
11423 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011424 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011425 }
11426}
11427
11428/*
11429 * "getbufline()" function
11430 */
11431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011432f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011433{
11434 linenr_T lnum;
11435 linenr_T end;
11436 buf_T *buf;
11437
11438 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11439 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011440 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011441 --emsg_off;
11442
Bram Moolenaar661b1822005-07-28 22:36:45 +000011443 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011444 if (argvars[2].v_type == VAR_UNKNOWN)
11445 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011446 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011447 end = get_tv_lnum_buf(&argvars[2], buf);
11448
Bram Moolenaar342337a2005-07-21 21:11:17 +000011449 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011450}
11451
Bram Moolenaar0d660222005-01-07 21:51:51 +000011452/*
11453 * "getbufvar()" function
11454 */
11455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011456f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011457{
11458 buf_T *buf;
11459 buf_T *save_curbuf;
11460 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011461 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011462 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011463
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011464 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11465 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011466 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011467 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011468
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011469 rettv->v_type = VAR_STRING;
11470 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011471
11472 if (buf != NULL && varname != NULL)
11473 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011474 /* set curbuf to be our buf, temporarily */
11475 save_curbuf = curbuf;
11476 curbuf = buf;
11477
Bram Moolenaar0d660222005-01-07 21:51:51 +000011478 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011479 {
11480 if (get_option_tv(&varname, rettv, TRUE) == OK)
11481 done = TRUE;
11482 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011483 else if (STRCMP(varname, "changedtick") == 0)
11484 {
11485 rettv->v_type = VAR_NUMBER;
11486 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011487 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011488 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011489 else
11490 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011491 /* Look up the variable. */
11492 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11493 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11494 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011495 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011496 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011497 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011498 done = TRUE;
11499 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011500 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011501
11502 /* restore previous notion of curbuf */
11503 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011504 }
11505
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011506 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11507 /* use the default value */
11508 copy_tv(&argvars[2], rettv);
11509
Bram Moolenaar0d660222005-01-07 21:51:51 +000011510 --emsg_off;
11511}
11512
11513/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514 * "getchar()" function
11515 */
11516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011517f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518{
11519 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011520 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011522 /* Position the cursor. Needed after a message that ends in a space. */
11523 windgoto(msg_row, msg_col);
11524
Bram Moolenaar071d4272004-06-13 20:20:40 +000011525 ++no_mapping;
11526 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011527 for (;;)
11528 {
11529 if (argvars[0].v_type == VAR_UNKNOWN)
11530 /* getchar(): blocking wait. */
11531 n = safe_vgetc();
11532 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11533 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011534 n = vpeekc_any();
11535 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011536 /* illegal argument or getchar(0) and no char avail: return zero */
11537 n = 0;
11538 else
11539 /* getchar(0) and char avail: return char */
11540 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011541
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011542 if (n == K_IGNORE)
11543 continue;
11544 break;
11545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546 --no_mapping;
11547 --allow_keys;
11548
Bram Moolenaar219b8702006-11-01 14:32:36 +000011549 vimvars[VV_MOUSE_WIN].vv_nr = 0;
Bram Moolenaar511972d2016-06-04 18:09:59 +020011550 vimvars[VV_MOUSE_WINID].vv_nr = 0;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011551 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11552 vimvars[VV_MOUSE_COL].vv_nr = 0;
11553
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011554 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555 if (IS_SPECIAL(n) || mod_mask != 0)
11556 {
11557 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11558 int i = 0;
11559
11560 /* Turn a special key into three bytes, plus modifier. */
11561 if (mod_mask != 0)
11562 {
11563 temp[i++] = K_SPECIAL;
11564 temp[i++] = KS_MODIFIER;
11565 temp[i++] = mod_mask;
11566 }
11567 if (IS_SPECIAL(n))
11568 {
11569 temp[i++] = K_SPECIAL;
11570 temp[i++] = K_SECOND(n);
11571 temp[i++] = K_THIRD(n);
11572 }
11573#ifdef FEAT_MBYTE
11574 else if (has_mbyte)
11575 i += (*mb_char2bytes)(n, temp + i);
11576#endif
11577 else
11578 temp[i++] = n;
11579 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011580 rettv->v_type = VAR_STRING;
11581 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011582
11583#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011584 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011585 {
11586 int row = mouse_row;
11587 int col = mouse_col;
11588 win_T *win;
11589 linenr_T lnum;
11590# ifdef FEAT_WINDOWS
11591 win_T *wp;
11592# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011593 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011594
11595 if (row >= 0 && col >= 0)
11596 {
11597 /* Find the window at the mouse coordinates and compute the
11598 * text position. */
11599 win = mouse_find_win(&row, &col);
11600 (void)mouse_comp_pos(win, &row, &col, &lnum);
11601# ifdef FEAT_WINDOWS
11602 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011603 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011604# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011605 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar511972d2016-06-04 18:09:59 +020011606 vimvars[VV_MOUSE_WINID].vv_nr = win->w_id;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011607 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11608 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11609 }
11610 }
11611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612 }
11613}
11614
11615/*
11616 * "getcharmod()" function
11617 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011619f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011620{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011621 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011622}
11623
11624/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011625 * "getcharsearch()" function
11626 */
11627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011628f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011629{
11630 if (rettv_dict_alloc(rettv) != FAIL)
11631 {
11632 dict_T *dict = rettv->vval.v_dict;
11633
11634 dict_add_nr_str(dict, "char", 0L, last_csearch());
11635 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11636 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11637 }
11638}
11639
11640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641 * "getcmdline()" function
11642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011644f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011646 rettv->v_type = VAR_STRING;
11647 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648}
11649
11650/*
11651 * "getcmdpos()" function
11652 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011654f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011655{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011656 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657}
11658
11659/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011660 * "getcmdtype()" function
11661 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011663f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011664{
11665 rettv->v_type = VAR_STRING;
11666 rettv->vval.v_string = alloc(2);
11667 if (rettv->vval.v_string != NULL)
11668 {
11669 rettv->vval.v_string[0] = get_cmdline_type();
11670 rettv->vval.v_string[1] = NUL;
11671 }
11672}
11673
11674/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011675 * "getcmdwintype()" function
11676 */
11677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011678f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011679{
11680 rettv->v_type = VAR_STRING;
11681 rettv->vval.v_string = NULL;
11682#ifdef FEAT_CMDWIN
11683 rettv->vval.v_string = alloc(2);
11684 if (rettv->vval.v_string != NULL)
11685 {
11686 rettv->vval.v_string[0] = cmdwin_type;
11687 rettv->vval.v_string[1] = NUL;
11688 }
11689#endif
11690}
11691
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020011692#if defined(FEAT_CMDL_COMPL)
11693/*
11694 * "getcompletion()" function
11695 */
11696 static void
11697f_getcompletion(typval_T *argvars, typval_T *rettv)
11698{
11699 char_u *pat;
11700 expand_T xpc;
11701 int options = WILD_KEEP_ALL | WILD_SILENT | WILD_USE_NL
11702 | WILD_LIST_NOTFOUND | WILD_NO_BEEP;
11703
11704 if (p_wic)
11705 options |= WILD_ICASE;
11706
11707 ExpandInit(&xpc);
11708 xpc.xp_pattern = get_tv_string(&argvars[0]);
Bram Moolenaar25065ec2016-07-10 19:22:53 +020011709 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020011710 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
11711 if (xpc.xp_context == EXPAND_NOTHING)
11712 {
11713 if (argvars[1].v_type == VAR_STRING)
11714 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
11715 else
11716 EMSG(_(e_invarg));
11717 return;
11718 }
11719
Bram Moolenaar4c068152016-07-11 23:15:25 +020011720# if defined(FEAT_MENU)
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020011721 if (xpc.xp_context == EXPAND_MENUS)
11722 {
11723 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
Bram Moolenaar25065ec2016-07-10 19:22:53 +020011724 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020011725 }
Bram Moolenaar4c068152016-07-11 23:15:25 +020011726# endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020011727
11728 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
11729 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
11730 {
11731 int i;
11732
11733 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
11734
11735 for (i = 0; i < xpc.xp_numfiles; i++)
11736 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11737 }
11738 vim_free(pat);
11739 ExpandCleanup(&xpc);
11740}
11741#endif
11742
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011743/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744 * "getcwd()" function
11745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011747f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011748{
Bram Moolenaarc9703302016-01-17 21:49:33 +010011749 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011750 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011752 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011753 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010011754
11755 wp = find_tabwin(&argvars[0], &argvars[1]);
11756 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010011758 if (wp->w_localdir != NULL)
11759 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar5c719942016-07-09 23:40:45 +020011760 else if (globaldir != NULL)
Bram Moolenaarc9703302016-01-17 21:49:33 +010011761 rettv->vval.v_string = vim_strsave(globaldir);
11762 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020011763 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010011764 cwd = alloc(MAXPATHL);
11765 if (cwd != NULL)
11766 {
11767 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11768 rettv->vval.v_string = vim_strsave(cwd);
11769 vim_free(cwd);
11770 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020011771 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010011772#ifdef BACKSLASH_IN_FILENAME
11773 if (rettv->vval.v_string != NULL)
11774 slash_adjust(rettv->vval.v_string);
11775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011776 }
11777}
11778
11779/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011780 * "getfontname()" function
11781 */
11782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011783f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011784{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011785 rettv->v_type = VAR_STRING;
11786 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011787#ifdef FEAT_GUI
11788 if (gui.in_use)
11789 {
11790 GuiFont font;
11791 char_u *name = NULL;
11792
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011793 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011794 {
11795 /* Get the "Normal" font. Either the name saved by
11796 * hl_set_font_name() or from the font ID. */
11797 font = gui.norm_font;
11798 name = hl_get_font_name();
11799 }
11800 else
11801 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011802 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011803 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11804 return;
11805 font = gui_mch_get_font(name, FALSE);
11806 if (font == NOFONT)
11807 return; /* Invalid font name, return empty string. */
11808 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011809 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011810 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011811 gui_mch_free_font(font);
11812 }
11813#endif
11814}
11815
11816/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011817 * "getfperm({fname})" function
11818 */
11819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011820f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011821{
11822 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011823 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011824 char_u *perm = NULL;
11825 char_u flags[] = "rwx";
11826 int i;
11827
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011828 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011829
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011830 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011831 if (mch_stat((char *)fname, &st) >= 0)
11832 {
11833 perm = vim_strsave((char_u *)"---------");
11834 if (perm != NULL)
11835 {
11836 for (i = 0; i < 9; i++)
11837 {
11838 if (st.st_mode & (1 << (8 - i)))
11839 perm[i] = flags[i % 3];
11840 }
11841 }
11842 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011843 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011844}
11845
11846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847 * "getfsize({fname})" function
11848 */
11849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011850f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851{
11852 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011853 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011855 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011857 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858
11859 if (mch_stat((char *)fname, &st) >= 0)
11860 {
11861 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011862 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011864 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011865 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011866
11867 /* non-perfect check for overflow */
Bram Moolenaar8767f522016-07-01 17:17:39 +020011868 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
Bram Moolenaard827ada2007-06-19 15:19:55 +000011869 rettv->vval.v_number = -2;
11870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011871 }
11872 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011873 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874}
11875
11876/*
11877 * "getftime({fname})" function
11878 */
11879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011880f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881{
11882 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011883 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011885 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011886
11887 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011888 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011889 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011890 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011891}
11892
11893/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011894 * "getftype({fname})" function
11895 */
11896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011897f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011898{
11899 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011900 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011901 char_u *type = NULL;
11902 char *t;
11903
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011904 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011905
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011906 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011907 if (mch_lstat((char *)fname, &st) >= 0)
11908 {
11909#ifdef S_ISREG
11910 if (S_ISREG(st.st_mode))
11911 t = "file";
11912 else if (S_ISDIR(st.st_mode))
11913 t = "dir";
11914# ifdef S_ISLNK
11915 else if (S_ISLNK(st.st_mode))
11916 t = "link";
11917# endif
11918# ifdef S_ISBLK
11919 else if (S_ISBLK(st.st_mode))
11920 t = "bdev";
11921# endif
11922# ifdef S_ISCHR
11923 else if (S_ISCHR(st.st_mode))
11924 t = "cdev";
11925# endif
11926# ifdef S_ISFIFO
11927 else if (S_ISFIFO(st.st_mode))
11928 t = "fifo";
11929# endif
11930# ifdef S_ISSOCK
11931 else if (S_ISSOCK(st.st_mode))
11932 t = "fifo";
11933# endif
11934 else
11935 t = "other";
11936#else
11937# ifdef S_IFMT
11938 switch (st.st_mode & S_IFMT)
11939 {
11940 case S_IFREG: t = "file"; break;
11941 case S_IFDIR: t = "dir"; break;
11942# ifdef S_IFLNK
11943 case S_IFLNK: t = "link"; break;
11944# endif
11945# ifdef S_IFBLK
11946 case S_IFBLK: t = "bdev"; break;
11947# endif
11948# ifdef S_IFCHR
11949 case S_IFCHR: t = "cdev"; break;
11950# endif
11951# ifdef S_IFIFO
11952 case S_IFIFO: t = "fifo"; break;
11953# endif
11954# ifdef S_IFSOCK
11955 case S_IFSOCK: t = "socket"; break;
11956# endif
11957 default: t = "other";
11958 }
11959# else
11960 if (mch_isdir(fname))
11961 t = "dir";
11962 else
11963 t = "file";
11964# endif
11965#endif
11966 type = vim_strsave((char_u *)t);
11967 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011968 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011969}
11970
11971/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011972 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011973 */
11974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011975f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011976{
11977 linenr_T lnum;
11978 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011979 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011980
11981 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011982 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011983 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011984 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011985 retlist = FALSE;
11986 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011987 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011988 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011989 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011990 retlist = TRUE;
11991 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011992
Bram Moolenaar342337a2005-07-21 21:11:17 +000011993 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011994}
11995
11996/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011997 * "getmatches()" function
11998 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012000f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012001{
12002#ifdef FEAT_SEARCH_EXTRA
12003 dict_T *dict;
12004 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012005 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012006
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012007 if (rettv_list_alloc(rettv) == OK)
12008 {
12009 while (cur != NULL)
12010 {
12011 dict = dict_alloc();
12012 if (dict == NULL)
12013 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012014 if (cur->match.regprog == NULL)
12015 {
12016 /* match added with matchaddpos() */
12017 for (i = 0; i < MAXPOSMATCH; ++i)
12018 {
12019 llpos_T *llpos;
12020 char buf[6];
12021 list_T *l;
12022
12023 llpos = &cur->pos.pos[i];
12024 if (llpos->lnum == 0)
12025 break;
12026 l = list_alloc();
12027 if (l == NULL)
12028 break;
12029 list_append_number(l, (varnumber_T)llpos->lnum);
12030 if (llpos->col > 0)
12031 {
12032 list_append_number(l, (varnumber_T)llpos->col);
12033 list_append_number(l, (varnumber_T)llpos->len);
12034 }
12035 sprintf(buf, "pos%d", i + 1);
12036 dict_add_list(dict, buf, l);
12037 }
12038 }
12039 else
12040 {
12041 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12042 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012043 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012044 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12045 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020012046# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020012047 if (cur->conceal_char)
12048 {
12049 char_u buf[MB_MAXBYTES + 1];
12050
12051 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12052 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12053 }
12054# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012055 list_append_dict(rettv->vval.v_list, dict);
12056 cur = cur->next;
12057 }
12058 }
12059#endif
12060}
12061
12062/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012063 * "getpid()" function
12064 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012066f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012067{
12068 rettv->vval.v_number = mch_get_pid();
12069}
12070
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012072getpos_both(
12073 typval_T *argvars,
12074 typval_T *rettv,
12075 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012076{
Bram Moolenaara5525202006-03-02 22:52:09 +000012077 pos_T *fp;
12078 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012079 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012080
12081 if (rettv_list_alloc(rettv) == OK)
12082 {
12083 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012084 if (getcurpos)
12085 fp = &curwin->w_cursor;
12086 else
12087 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012088 if (fnum != -1)
12089 list_append_number(l, (varnumber_T)fnum);
12090 else
12091 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012092 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12093 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012094 list_append_number(l, (fp != NULL)
12095 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012096 : (varnumber_T)0);
12097 list_append_number(l,
12098#ifdef FEAT_VIRTUALEDIT
12099 (fp != NULL) ? (varnumber_T)fp->coladd :
12100#endif
12101 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012102 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012103 {
12104 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012105 list_append_number(l, curwin->w_curswant == MAXCOL ?
12106 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012107 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012108 }
12109 else
12110 rettv->vval.v_number = FALSE;
12111}
12112
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020012113
12114/*
12115 * "getcurpos()" function
12116 */
12117 static void
12118f_getcurpos(typval_T *argvars, typval_T *rettv)
12119{
12120 getpos_both(argvars, rettv, TRUE);
12121}
12122
12123/*
12124 * "getpos(string)" function
12125 */
12126 static void
12127f_getpos(typval_T *argvars, typval_T *rettv)
12128{
12129 getpos_both(argvars, rettv, FALSE);
12130}
12131
Bram Moolenaara5525202006-03-02 22:52:09 +000012132/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012133 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012134 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012136f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012137{
12138#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012139 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012140#endif
12141
Bram Moolenaar2641f772005-03-25 21:58:17 +000012142#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012143 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012144 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012145 wp = NULL;
12146 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12147 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012148 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012149 if (wp == NULL)
12150 return;
12151 }
12152
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012153 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012154 }
12155#endif
12156}
12157
12158/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159 * "getreg()" function
12160 */
12161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012162f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163{
12164 char_u *strregname;
12165 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012166 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012167 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012168 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012169
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012170 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012172 strregname = get_tv_string_chk(&argvars[0]);
12173 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012174 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012175 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012176 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012177 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012178 return_list = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012179 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012182 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012183
12184 if (error)
12185 return;
12186
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187 regname = (strregname == NULL ? '"' : *strregname);
12188 if (regname == 0)
12189 regname = '"';
12190
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012191 if (return_list)
12192 {
12193 rettv->v_type = VAR_LIST;
12194 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12195 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020012196 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020012197 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020012198 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012199 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012200 }
12201 else
12202 {
12203 rettv->v_type = VAR_STRING;
12204 rettv->vval.v_string = get_reg_contents(regname,
12205 arg2 ? GREG_EXPR_SRC : 0);
12206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207}
12208
12209/*
12210 * "getregtype()" function
12211 */
12212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012213f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214{
12215 char_u *strregname;
12216 int regname;
12217 char_u buf[NUMBUFLEN + 2];
12218 long reglen = 0;
12219
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012220 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012221 {
12222 strregname = get_tv_string_chk(&argvars[0]);
12223 if (strregname == NULL) /* type error; errmsg already given */
12224 {
12225 rettv->v_type = VAR_STRING;
12226 rettv->vval.v_string = NULL;
12227 return;
12228 }
12229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230 else
12231 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012232 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233
12234 regname = (strregname == NULL ? '"' : *strregname);
12235 if (regname == 0)
12236 regname = '"';
12237
12238 buf[0] = NUL;
12239 buf[1] = NUL;
12240 switch (get_reg_type(regname, &reglen))
12241 {
12242 case MLINE: buf[0] = 'V'; break;
12243 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244 case MBLOCK:
12245 buf[0] = Ctrl_V;
12246 sprintf((char *)buf + 1, "%ld", reglen + 1);
12247 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012248 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012249 rettv->v_type = VAR_STRING;
12250 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251}
12252
12253/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012254 * "gettabvar()" function
12255 */
12256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012257f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012258{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012259 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012260 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012261 dictitem_T *v;
12262 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012263 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012264
12265 rettv->v_type = VAR_STRING;
12266 rettv->vval.v_string = NULL;
12267
12268 varname = get_tv_string_chk(&argvars[1]);
12269 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12270 if (tp != NULL && varname != NULL)
12271 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012272 /* Set tp to be our tabpage, temporarily. Also set the window to the
12273 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012274 if (switch_win(&oldcurwin, &oldtabpage,
12275 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012276 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012277 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012278 /* look up the variable */
12279 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12280 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12281 if (v != NULL)
12282 {
12283 copy_tv(&v->di_tv, rettv);
12284 done = TRUE;
12285 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012286 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012287
12288 /* restore previous notion of curwin */
12289 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012290 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012291
12292 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012293 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012294}
12295
12296/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012297 * "gettabwinvar()" function
12298 */
12299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012300f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012301{
12302 getwinvar(argvars, rettv, 1);
12303}
12304
12305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306 * "getwinposx()" function
12307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012309f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012311 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312#ifdef FEAT_GUI
12313 if (gui.in_use)
12314 {
12315 int x, y;
12316
12317 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012318 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012319 }
12320#endif
12321}
12322
12323/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010012324 * "win_findbuf()" function
12325 */
12326 static void
12327f_win_findbuf(typval_T *argvars, typval_T *rettv)
12328{
12329 if (rettv_list_alloc(rettv) != FAIL)
12330 win_findbuf(argvars, rettv->vval.v_list);
12331}
12332
12333/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010012334 * "win_getid()" function
12335 */
12336 static void
12337f_win_getid(typval_T *argvars, typval_T *rettv)
12338{
12339 rettv->vval.v_number = win_getid(argvars);
12340}
12341
12342/*
12343 * "win_gotoid()" function
12344 */
12345 static void
12346f_win_gotoid(typval_T *argvars, typval_T *rettv)
12347{
12348 rettv->vval.v_number = win_gotoid(argvars);
12349}
12350
12351/*
12352 * "win_id2tabwin()" function
12353 */
12354 static void
12355f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
12356{
12357 if (rettv_list_alloc(rettv) != FAIL)
12358 win_id2tabwin(argvars, rettv->vval.v_list);
12359}
12360
12361/*
12362 * "win_id2win()" function
12363 */
12364 static void
12365f_win_id2win(typval_T *argvars, typval_T *rettv)
12366{
12367 rettv->vval.v_number = win_id2win(argvars);
12368}
12369
12370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012371 * "getwinposy()" function
12372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012374f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012376 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377#ifdef FEAT_GUI
12378 if (gui.in_use)
12379 {
12380 int x, y;
12381
12382 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012383 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384 }
12385#endif
12386}
12387
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012388/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012389 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012390 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012391 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012392find_win_by_nr(
12393 typval_T *vp,
12394 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012395{
12396#ifdef FEAT_WINDOWS
12397 win_T *wp;
12398#endif
12399 int nr;
12400
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012401 nr = (int)get_tv_number_chk(vp, NULL);
Bram Moolenaara40058a2005-07-11 22:42:07 +000012402
12403#ifdef FEAT_WINDOWS
12404 if (nr < 0)
12405 return NULL;
12406 if (nr == 0)
12407 return curwin;
12408
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012409 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12410 wp != NULL; wp = wp->w_next)
Bram Moolenaar888ccac2016-06-04 18:49:36 +020012411 if (nr >= LOWEST_WIN_ID)
12412 {
12413 if (wp->w_id == nr)
12414 return wp;
12415 }
12416 else if (--nr <= 0)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012417 break;
Bram Moolenaar888ccac2016-06-04 18:49:36 +020012418 if (nr >= LOWEST_WIN_ID)
12419 return NULL;
Bram Moolenaara40058a2005-07-11 22:42:07 +000012420 return wp;
12421#else
Bram Moolenaar888ccac2016-06-04 18:49:36 +020012422 if (nr == 0 || nr == 1 || nr == curwin->w_id)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012423 return curwin;
12424 return NULL;
12425#endif
12426}
12427
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012429 * Find window specified by "wvp" in tabpage "tvp".
12430 */
12431 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012432find_tabwin(
12433 typval_T *wvp, /* VAR_UNKNOWN for current window */
12434 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012435{
12436 win_T *wp = NULL;
12437 tabpage_T *tp = NULL;
12438 long n;
12439
12440 if (wvp->v_type != VAR_UNKNOWN)
12441 {
12442 if (tvp->v_type != VAR_UNKNOWN)
12443 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012444 n = (long)get_tv_number(tvp);
Bram Moolenaarc9703302016-01-17 21:49:33 +010012445 if (n >= 0)
12446 tp = find_tabpage(n);
12447 }
12448 else
12449 tp = curtab;
12450
12451 if (tp != NULL)
12452 wp = find_win_by_nr(wvp, tp);
12453 }
12454 else
12455 wp = curwin;
12456
12457 return wp;
12458}
12459
12460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461 * "getwinvar()" function
12462 */
12463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012464f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012466 getwinvar(argvars, rettv, 0);
12467}
12468
12469/*
12470 * getwinvar() and gettabwinvar()
12471 */
12472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012473getwinvar(
12474 typval_T *argvars,
12475 typval_T *rettv,
12476 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012477{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012478 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012479 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012480 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012481 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012482 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012483#ifdef FEAT_WINDOWS
12484 win_T *oldcurwin;
12485 tabpage_T *oldtabpage;
12486 int need_switch_win;
12487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012488
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012489#ifdef FEAT_WINDOWS
12490 if (off == 1)
12491 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12492 else
12493 tp = curtab;
12494#endif
12495 win = find_win_by_nr(&argvars[off], tp);
12496 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012497 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012499 rettv->v_type = VAR_STRING;
12500 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501
12502 if (win != NULL && varname != NULL)
12503 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012504#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012505 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012506 * otherwise the window is not valid. Only do this when needed,
12507 * autocommands get blocked. */
12508 need_switch_win = !(tp == curtab && win == curwin);
12509 if (!need_switch_win
12510 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12511#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012512 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012513 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012514 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012515 if (get_option_tv(&varname, rettv, 1) == OK)
12516 done = TRUE;
12517 }
12518 else
12519 {
12520 /* Look up the variable. */
12521 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12522 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12523 varname, FALSE);
12524 if (v != NULL)
12525 {
12526 copy_tv(&v->di_tv, rettv);
12527 done = TRUE;
12528 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012530 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012531
Bram Moolenaarba117c22015-09-29 16:53:22 +020012532#ifdef FEAT_WINDOWS
12533 if (need_switch_win)
12534 /* restore previous notion of curwin */
12535 restore_win(oldcurwin, oldtabpage, TRUE);
12536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537 }
12538
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012539 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12540 /* use the default return value */
12541 copy_tv(&argvars[off + 2], rettv);
12542
Bram Moolenaar071d4272004-06-13 20:20:40 +000012543 --emsg_off;
12544}
12545
12546/*
12547 * "glob()" function
12548 */
12549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012550f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012551{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012552 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012554 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012556 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012557 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012558 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012559 if (argvars[1].v_type != VAR_UNKNOWN)
12560 {
12561 if (get_tv_number_chk(&argvars[1], &error))
12562 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012563 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012564 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012565 if (get_tv_number_chk(&argvars[2], &error))
12566 {
12567 rettv->v_type = VAR_LIST;
12568 rettv->vval.v_list = NULL;
12569 }
12570 if (argvars[3].v_type != VAR_UNKNOWN
12571 && get_tv_number_chk(&argvars[3], &error))
12572 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012573 }
12574 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012575 if (!error)
12576 {
12577 ExpandInit(&xpc);
12578 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012579 if (p_wic)
12580 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012581 if (rettv->v_type == VAR_STRING)
12582 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012583 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012584 else if (rettv_list_alloc(rettv) != FAIL)
12585 {
12586 int i;
12587
12588 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12589 NULL, options, WILD_ALL_KEEP);
12590 for (i = 0; i < xpc.xp_numfiles; i++)
12591 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12592
12593 ExpandCleanup(&xpc);
12594 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012595 }
12596 else
12597 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012598}
12599
12600/*
12601 * "globpath()" function
12602 */
12603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012604f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012605{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012606 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012607 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012608 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012609 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012610 garray_T ga;
12611 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012613 /* When the optional second argument is non-zero, don't remove matches
12614 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012615 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012616 if (argvars[2].v_type != VAR_UNKNOWN)
12617 {
12618 if (get_tv_number_chk(&argvars[2], &error))
12619 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012620 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012621 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012622 if (get_tv_number_chk(&argvars[3], &error))
12623 {
12624 rettv->v_type = VAR_LIST;
12625 rettv->vval.v_list = NULL;
12626 }
12627 if (argvars[4].v_type != VAR_UNKNOWN
12628 && get_tv_number_chk(&argvars[4], &error))
12629 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012630 }
12631 }
12632 if (file != NULL && !error)
12633 {
12634 ga_init2(&ga, (int)sizeof(char_u *), 10);
12635 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12636 if (rettv->v_type == VAR_STRING)
12637 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12638 else if (rettv_list_alloc(rettv) != FAIL)
12639 for (i = 0; i < ga.ga_len; ++i)
12640 list_append_string(rettv->vval.v_list,
12641 ((char_u **)(ga.ga_data))[i], -1);
12642 ga_clear_strings(&ga);
12643 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012644 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012645 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646}
12647
12648/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012649 * "glob2regpat()" function
12650 */
12651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012652f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012653{
12654 char_u *pat = get_tv_string_chk(&argvars[0]);
12655
12656 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010012657 rettv->vval.v_string = (pat == NULL)
12658 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012659}
12660
12661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012662 * "has()" function
12663 */
12664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012665f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012666{
12667 int i;
12668 char_u *name;
12669 int n = FALSE;
12670 static char *(has_list[]) =
12671 {
12672#ifdef AMIGA
12673 "amiga",
12674# ifdef FEAT_ARP
12675 "arp",
12676# endif
12677#endif
12678#ifdef __BEOS__
12679 "beos",
12680#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012681#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682 "mac",
12683#endif
12684#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010012685 "macunix", /* built with 'darwin' enabled */
12686#endif
12687#if defined(__APPLE__) && __APPLE__ == 1
12688 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690#ifdef __QNX__
12691 "qnx",
12692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693#ifdef UNIX
12694 "unix",
12695#endif
12696#ifdef VMS
12697 "vms",
12698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699#ifdef WIN32
12700 "win32",
12701#endif
12702#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12703 "win32unix",
12704#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012705#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706 "win64",
12707#endif
12708#ifdef EBCDIC
12709 "ebcdic",
12710#endif
12711#ifndef CASE_INSENSITIVE_FILENAME
12712 "fname_case",
12713#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012714#ifdef HAVE_ACL
12715 "acl",
12716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012717#ifdef FEAT_ARABIC
12718 "arabic",
12719#endif
12720#ifdef FEAT_AUTOCMD
12721 "autocmd",
12722#endif
12723#ifdef FEAT_BEVAL
12724 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012725# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12726 "balloon_multiline",
12727# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728#endif
12729#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12730 "builtin_terms",
12731# ifdef ALL_BUILTIN_TCAPS
12732 "all_builtin_terms",
12733# endif
12734#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012735#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12736 || defined(FEAT_GUI_W32) \
12737 || defined(FEAT_GUI_MOTIF))
12738 "browsefilter",
12739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740#ifdef FEAT_BYTEOFF
12741 "byte_offset",
12742#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010012743#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010012744 "channel",
12745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746#ifdef FEAT_CINDENT
12747 "cindent",
12748#endif
12749#ifdef FEAT_CLIENTSERVER
12750 "clientserver",
12751#endif
12752#ifdef FEAT_CLIPBOARD
12753 "clipboard",
12754#endif
12755#ifdef FEAT_CMDL_COMPL
12756 "cmdline_compl",
12757#endif
12758#ifdef FEAT_CMDHIST
12759 "cmdline_hist",
12760#endif
12761#ifdef FEAT_COMMENTS
12762 "comments",
12763#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012764#ifdef FEAT_CONCEAL
12765 "conceal",
12766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012767#ifdef FEAT_CRYPT
12768 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010012769 "crypt-blowfish",
12770 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771#endif
12772#ifdef FEAT_CSCOPE
12773 "cscope",
12774#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012775#ifdef FEAT_CURSORBIND
12776 "cursorbind",
12777#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012778#ifdef CURSOR_SHAPE
12779 "cursorshape",
12780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781#ifdef DEBUG
12782 "debug",
12783#endif
12784#ifdef FEAT_CON_DIALOG
12785 "dialog_con",
12786#endif
12787#ifdef FEAT_GUI_DIALOG
12788 "dialog_gui",
12789#endif
12790#ifdef FEAT_DIFF
12791 "diff",
12792#endif
12793#ifdef FEAT_DIGRAPHS
12794 "digraphs",
12795#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012796#ifdef FEAT_DIRECTX
12797 "directx",
12798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799#ifdef FEAT_DND
12800 "dnd",
12801#endif
12802#ifdef FEAT_EMACS_TAGS
12803 "emacs_tags",
12804#endif
12805 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010012806 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807#ifdef FEAT_SEARCH_EXTRA
12808 "extra_search",
12809#endif
12810#ifdef FEAT_FKMAP
12811 "farsi",
12812#endif
12813#ifdef FEAT_SEARCHPATH
12814 "file_in_path",
12815#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012816#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012817 "filterpipe",
12818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819#ifdef FEAT_FIND_ID
12820 "find_in_path",
12821#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012822#ifdef FEAT_FLOAT
12823 "float",
12824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825#ifdef FEAT_FOLDING
12826 "folding",
12827#endif
12828#ifdef FEAT_FOOTER
12829 "footer",
12830#endif
12831#if !defined(USE_SYSTEM) && defined(UNIX)
12832 "fork",
12833#endif
12834#ifdef FEAT_GETTEXT
12835 "gettext",
12836#endif
12837#ifdef FEAT_GUI
12838 "gui",
12839#endif
12840#ifdef FEAT_GUI_ATHENA
12841# ifdef FEAT_GUI_NEXTAW
12842 "gui_neXtaw",
12843# else
12844 "gui_athena",
12845# endif
12846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012847#ifdef FEAT_GUI_GTK
12848 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010012849# ifdef USE_GTK3
12850 "gui_gtk3",
12851# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010012853# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012855#ifdef FEAT_GUI_GNOME
12856 "gui_gnome",
12857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858#ifdef FEAT_GUI_MAC
12859 "gui_mac",
12860#endif
12861#ifdef FEAT_GUI_MOTIF
12862 "gui_motif",
12863#endif
12864#ifdef FEAT_GUI_PHOTON
12865 "gui_photon",
12866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867#ifdef FEAT_GUI_W32
12868 "gui_win32",
12869#endif
12870#ifdef FEAT_HANGULIN
12871 "hangul_input",
12872#endif
12873#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12874 "iconv",
12875#endif
12876#ifdef FEAT_INS_EXPAND
12877 "insert_expand",
12878#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010012879#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010012880 "job",
12881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882#ifdef FEAT_JUMPLIST
12883 "jumplist",
12884#endif
12885#ifdef FEAT_KEYMAP
12886 "keymap",
12887#endif
12888#ifdef FEAT_LANGMAP
12889 "langmap",
12890#endif
12891#ifdef FEAT_LIBCALL
12892 "libcall",
12893#endif
12894#ifdef FEAT_LINEBREAK
12895 "linebreak",
12896#endif
12897#ifdef FEAT_LISP
12898 "lispindent",
12899#endif
12900#ifdef FEAT_LISTCMDS
12901 "listcmds",
12902#endif
12903#ifdef FEAT_LOCALMAP
12904 "localmap",
12905#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012906#ifdef FEAT_LUA
12907# ifndef DYNAMIC_LUA
12908 "lua",
12909# endif
12910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911#ifdef FEAT_MENU
12912 "menu",
12913#endif
12914#ifdef FEAT_SESSION
12915 "mksession",
12916#endif
12917#ifdef FEAT_MODIFY_FNAME
12918 "modify_fname",
12919#endif
12920#ifdef FEAT_MOUSE
12921 "mouse",
12922#endif
12923#ifdef FEAT_MOUSESHAPE
12924 "mouseshape",
12925#endif
12926#if defined(UNIX) || defined(VMS)
12927# ifdef FEAT_MOUSE_DEC
12928 "mouse_dec",
12929# endif
12930# ifdef FEAT_MOUSE_GPM
12931 "mouse_gpm",
12932# endif
12933# ifdef FEAT_MOUSE_JSB
12934 "mouse_jsbterm",
12935# endif
12936# ifdef FEAT_MOUSE_NET
12937 "mouse_netterm",
12938# endif
12939# ifdef FEAT_MOUSE_PTERM
12940 "mouse_pterm",
12941# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012942# ifdef FEAT_MOUSE_SGR
12943 "mouse_sgr",
12944# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012945# ifdef FEAT_SYSMOUSE
12946 "mouse_sysmouse",
12947# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012948# ifdef FEAT_MOUSE_URXVT
12949 "mouse_urxvt",
12950# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951# ifdef FEAT_MOUSE_XTERM
12952 "mouse_xterm",
12953# endif
12954#endif
12955#ifdef FEAT_MBYTE
12956 "multi_byte",
12957#endif
12958#ifdef FEAT_MBYTE_IME
12959 "multi_byte_ime",
12960#endif
12961#ifdef FEAT_MULTI_LANG
12962 "multi_lang",
12963#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012964#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012965#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012966 "mzscheme",
12967#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012968#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012969#ifdef FEAT_NUM64
12970 "num64",
12971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012972#ifdef FEAT_OLE
12973 "ole",
12974#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010012975 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012976#ifdef FEAT_PATH_EXTRA
12977 "path_extra",
12978#endif
12979#ifdef FEAT_PERL
12980#ifndef DYNAMIC_PERL
12981 "perl",
12982#endif
12983#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012984#ifdef FEAT_PERSISTENT_UNDO
12985 "persistent_undo",
12986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012987#ifdef FEAT_PYTHON
12988#ifndef DYNAMIC_PYTHON
12989 "python",
12990#endif
12991#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012992#ifdef FEAT_PYTHON3
12993#ifndef DYNAMIC_PYTHON3
12994 "python3",
12995#endif
12996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997#ifdef FEAT_POSTSCRIPT
12998 "postscript",
12999#endif
13000#ifdef FEAT_PRINTER
13001 "printer",
13002#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013003#ifdef FEAT_PROFILE
13004 "profile",
13005#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013006#ifdef FEAT_RELTIME
13007 "reltime",
13008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013009#ifdef FEAT_QUICKFIX
13010 "quickfix",
13011#endif
13012#ifdef FEAT_RIGHTLEFT
13013 "rightleft",
13014#endif
13015#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13016 "ruby",
13017#endif
13018#ifdef FEAT_SCROLLBIND
13019 "scrollbind",
13020#endif
13021#ifdef FEAT_CMDL_INFO
13022 "showcmd",
13023 "cmdline_info",
13024#endif
13025#ifdef FEAT_SIGNS
13026 "signs",
13027#endif
13028#ifdef FEAT_SMARTINDENT
13029 "smartindent",
13030#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013031#ifdef STARTUPTIME
13032 "startuptime",
13033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034#ifdef FEAT_STL_OPT
13035 "statusline",
13036#endif
13037#ifdef FEAT_SUN_WORKSHOP
13038 "sun_workshop",
13039#endif
13040#ifdef FEAT_NETBEANS_INTG
13041 "netbeans_intg",
13042#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013043#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013044 "spell",
13045#endif
13046#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013047 "syntax",
13048#endif
13049#if defined(USE_SYSTEM) || !defined(UNIX)
13050 "system",
13051#endif
13052#ifdef FEAT_TAG_BINS
13053 "tag_binary",
13054#endif
13055#ifdef FEAT_TAG_OLDSTATIC
13056 "tag_old_static",
13057#endif
13058#ifdef FEAT_TAG_ANYWHITE
13059 "tag_any_white",
13060#endif
13061#ifdef FEAT_TCL
13062# ifndef DYNAMIC_TCL
13063 "tcl",
13064# endif
13065#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020013066#ifdef FEAT_TERMGUICOLORS
13067 "termguicolors",
13068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013069#ifdef TERMINFO
13070 "terminfo",
13071#endif
13072#ifdef FEAT_TERMRESPONSE
13073 "termresponse",
13074#endif
13075#ifdef FEAT_TEXTOBJ
13076 "textobjects",
13077#endif
13078#ifdef HAVE_TGETENT
13079 "tgetent",
13080#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010013081#ifdef FEAT_TIMERS
13082 "timers",
13083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013084#ifdef FEAT_TITLE
13085 "title",
13086#endif
13087#ifdef FEAT_TOOLBAR
13088 "toolbar",
13089#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013090#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13091 "unnamedplus",
13092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013093#ifdef FEAT_USR_CMDS
13094 "user-commands", /* was accidentally included in 5.4 */
13095 "user_commands",
13096#endif
13097#ifdef FEAT_VIMINFO
13098 "viminfo",
13099#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010013100#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013101 "vertsplit",
13102#endif
13103#ifdef FEAT_VIRTUALEDIT
13104 "virtualedit",
13105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013106 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107#ifdef FEAT_VISUALEXTRA
13108 "visualextra",
13109#endif
13110#ifdef FEAT_VREPLACE
13111 "vreplace",
13112#endif
13113#ifdef FEAT_WILDIGN
13114 "wildignore",
13115#endif
13116#ifdef FEAT_WILDMENU
13117 "wildmenu",
13118#endif
13119#ifdef FEAT_WINDOWS
13120 "windows",
13121#endif
13122#ifdef FEAT_WAK
13123 "winaltkeys",
13124#endif
13125#ifdef FEAT_WRITEBACKUP
13126 "writebackup",
13127#endif
13128#ifdef FEAT_XIM
13129 "xim",
13130#endif
13131#ifdef FEAT_XFONTSET
13132 "xfontset",
13133#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013134#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013135 "xpm",
13136 "xpm_w32", /* for backward compatibility */
13137#else
13138# if defined(HAVE_XPM)
13139 "xpm",
13140# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142#ifdef USE_XSMP
13143 "xsmp",
13144#endif
13145#ifdef USE_XSMP_INTERACT
13146 "xsmp_interact",
13147#endif
13148#ifdef FEAT_XCLIPBOARD
13149 "xterm_clipboard",
13150#endif
13151#ifdef FEAT_XTERM_SAVE
13152 "xterm_save",
13153#endif
13154#if defined(UNIX) && defined(FEAT_X11)
13155 "X11",
13156#endif
13157 NULL
13158 };
13159
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013160 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161 for (i = 0; has_list[i] != NULL; ++i)
13162 if (STRICMP(name, has_list[i]) == 0)
13163 {
13164 n = TRUE;
13165 break;
13166 }
13167
13168 if (n == FALSE)
13169 {
13170 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013171 {
13172 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010013173 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013174 && vim_isdigit(name[6])
13175 && vim_isdigit(name[8])
13176 && vim_isdigit(name[10]))
13177 {
13178 int major = atoi((char *)name + 6);
13179 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013180
13181 /* Expect "patch-9.9.01234". */
13182 n = (major < VIM_VERSION_MAJOR
13183 || (major == VIM_VERSION_MAJOR
13184 && (minor < VIM_VERSION_MINOR
13185 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013186 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013187 }
13188 else
13189 n = has_patch(atoi((char *)name + 5));
13190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013191 else if (STRICMP(name, "vim_starting") == 0)
13192 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013193#ifdef FEAT_MBYTE
13194 else if (STRICMP(name, "multi_byte_encoding") == 0)
13195 n = has_mbyte;
13196#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013197#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13198 else if (STRICMP(name, "balloon_multiline") == 0)
13199 n = multiline_balloon_available();
13200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201#ifdef DYNAMIC_TCL
13202 else if (STRICMP(name, "tcl") == 0)
13203 n = tcl_enabled(FALSE);
13204#endif
13205#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13206 else if (STRICMP(name, "iconv") == 0)
13207 n = iconv_enabled(FALSE);
13208#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013209#ifdef DYNAMIC_LUA
13210 else if (STRICMP(name, "lua") == 0)
13211 n = lua_enabled(FALSE);
13212#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013213#ifdef DYNAMIC_MZSCHEME
13214 else if (STRICMP(name, "mzscheme") == 0)
13215 n = mzscheme_enabled(FALSE);
13216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217#ifdef DYNAMIC_RUBY
13218 else if (STRICMP(name, "ruby") == 0)
13219 n = ruby_enabled(FALSE);
13220#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013221#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222#ifdef DYNAMIC_PYTHON
13223 else if (STRICMP(name, "python") == 0)
13224 n = python_enabled(FALSE);
13225#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013226#endif
13227#ifdef FEAT_PYTHON3
13228#ifdef DYNAMIC_PYTHON3
13229 else if (STRICMP(name, "python3") == 0)
13230 n = python3_enabled(FALSE);
13231#endif
13232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233#ifdef DYNAMIC_PERL
13234 else if (STRICMP(name, "perl") == 0)
13235 n = perl_enabled(FALSE);
13236#endif
13237#ifdef FEAT_GUI
13238 else if (STRICMP(name, "gui_running") == 0)
13239 n = (gui.in_use || gui.starting);
13240# ifdef FEAT_GUI_W32
13241 else if (STRICMP(name, "gui_win32s") == 0)
13242 n = gui_is_win32s();
13243# endif
13244# ifdef FEAT_BROWSE
13245 else if (STRICMP(name, "browse") == 0)
13246 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13247# endif
13248#endif
13249#ifdef FEAT_SYN_HL
13250 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013251 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252#endif
13253#if defined(WIN3264)
13254 else if (STRICMP(name, "win95") == 0)
13255 n = mch_windows95();
13256#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013257#ifdef FEAT_NETBEANS_INTG
13258 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013259 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013261 }
13262
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013263 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013264}
13265
13266/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013267 * "has_key()" function
13268 */
13269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013270f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013271{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013272 if (argvars[0].v_type != VAR_DICT)
13273 {
13274 EMSG(_(e_dictreq));
13275 return;
13276 }
13277 if (argvars[0].vval.v_dict == NULL)
13278 return;
13279
13280 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013281 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013282}
13283
13284/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013285 * "haslocaldir()" function
13286 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013288f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013289{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013290 win_T *wp = NULL;
13291
13292 wp = find_tabwin(&argvars[0], &argvars[1]);
13293 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013294}
13295
13296/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297 * "hasmapto()" function
13298 */
13299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013300f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013301{
13302 char_u *name;
13303 char_u *mode;
13304 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013305 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013307 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013308 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309 mode = (char_u *)"nvo";
13310 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013311 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013312 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013313 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013314 abbr = (int)get_tv_number(&argvars[2]);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316
Bram Moolenaar2c932302006-03-18 21:42:09 +000013317 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013318 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013319 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013320 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013321}
13322
13323/*
13324 * "histadd()" function
13325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013327f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013328{
13329#ifdef FEAT_CMDHIST
13330 int histype;
13331 char_u *str;
13332 char_u buf[NUMBUFLEN];
13333#endif
13334
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013335 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336 if (check_restricted() || check_secure())
13337 return;
13338#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013339 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13340 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013341 if (histype >= 0)
13342 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013343 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344 if (*str != NUL)
13345 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013346 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013348 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349 return;
13350 }
13351 }
13352#endif
13353}
13354
13355/*
13356 * "histdel()" function
13357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013359f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360{
13361#ifdef FEAT_CMDHIST
13362 int n;
13363 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013364 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013365
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013366 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13367 if (str == NULL)
13368 n = 0;
13369 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013370 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013371 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013372 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013373 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013374 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013375 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013376 else
13377 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013378 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013379 get_tv_string_buf(&argvars[1], buf));
13380 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013381#endif
13382}
13383
13384/*
13385 * "histget()" function
13386 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013388f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013389{
13390#ifdef FEAT_CMDHIST
13391 int type;
13392 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013393 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013394
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013395 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13396 if (str == NULL)
13397 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013398 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013399 {
13400 type = get_histtype(str);
13401 if (argvars[1].v_type == VAR_UNKNOWN)
13402 idx = get_history_idx(type);
13403 else
13404 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13405 /* -1 on type error */
13406 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013408#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013409 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013411 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412}
13413
13414/*
13415 * "histnr()" function
13416 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013418f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419{
13420 int i;
13421
13422#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013423 char_u *history = get_tv_string_chk(&argvars[0]);
13424
13425 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426 if (i >= HIST_CMD && i < HIST_COUNT)
13427 i = get_history_idx(i);
13428 else
13429#endif
13430 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013431 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432}
13433
13434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435 * "highlightID(name)" function
13436 */
13437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013438f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013440 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441}
13442
13443/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013444 * "highlight_exists()" function
13445 */
13446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013447f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013448{
13449 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13450}
13451
13452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013453 * "hostname()" function
13454 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013456f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457{
13458 char_u hostname[256];
13459
13460 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013461 rettv->v_type = VAR_STRING;
13462 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013463}
13464
13465/*
13466 * iconv() function
13467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013469f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470{
13471#ifdef FEAT_MBYTE
13472 char_u buf1[NUMBUFLEN];
13473 char_u buf2[NUMBUFLEN];
13474 char_u *from, *to, *str;
13475 vimconv_T vimconv;
13476#endif
13477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013478 rettv->v_type = VAR_STRING;
13479 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480
13481#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013482 str = get_tv_string(&argvars[0]);
13483 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13484 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013485 vimconv.vc_type = CONV_NONE;
13486 convert_setup(&vimconv, from, to);
13487
13488 /* If the encodings are equal, no conversion needed. */
13489 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013490 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013492 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013493
13494 convert_setup(&vimconv, NULL, NULL);
13495 vim_free(from);
13496 vim_free(to);
13497#endif
13498}
13499
13500/*
13501 * "indent()" function
13502 */
13503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013504f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013505{
13506 linenr_T lnum;
13507
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013508 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013510 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013511 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013512 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513}
13514
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013515/*
13516 * "index()" function
13517 */
13518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013519f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013520{
Bram Moolenaar33570922005-01-25 22:26:29 +000013521 list_T *l;
13522 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013523 long idx = 0;
13524 int ic = FALSE;
13525
13526 rettv->vval.v_number = -1;
13527 if (argvars[0].v_type != VAR_LIST)
13528 {
13529 EMSG(_(e_listreq));
13530 return;
13531 }
13532 l = argvars[0].vval.v_list;
13533 if (l != NULL)
13534 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013535 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013536 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013537 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013538 int error = FALSE;
13539
Bram Moolenaar758711c2005-02-02 23:11:38 +000013540 /* Start at specified item. Use the cached index that list_find()
13541 * sets, so that a negative number also works. */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013542 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013543 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013544 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013545 ic = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013546 if (error)
13547 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013548 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013549
Bram Moolenaar758711c2005-02-02 23:11:38 +000013550 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013551 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013552 {
13553 rettv->vval.v_number = idx;
13554 break;
13555 }
13556 }
13557}
13558
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559static int inputsecret_flag = 0;
13560
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013561static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013562
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013564 * This function is used by f_input() and f_inputdialog() functions. The third
13565 * argument to f_input() specifies the type of completion to use at the
13566 * prompt. The third argument to f_inputdialog() specifies the value to return
13567 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568 */
13569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013570get_user_input(
13571 typval_T *argvars,
13572 typval_T *rettv,
13573 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013575 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576 char_u *p = NULL;
13577 int c;
13578 char_u buf[NUMBUFLEN];
13579 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013580 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013581 int xp_type = EXPAND_NOTHING;
13582 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013584 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013585 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586
13587#ifdef NO_CONSOLE_INPUT
13588 /* While starting up, there is no place to enter text. */
13589 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591#endif
13592
13593 cmd_silent = FALSE; /* Want to see the prompt. */
13594 if (prompt != NULL)
13595 {
13596 /* Only the part of the message after the last NL is considered as
13597 * prompt for the command line */
13598 p = vim_strrchr(prompt, '\n');
13599 if (p == NULL)
13600 p = prompt;
13601 else
13602 {
13603 ++p;
13604 c = *p;
13605 *p = NUL;
13606 msg_start();
13607 msg_clr_eos();
13608 msg_puts_attr(prompt, echo_attr);
13609 msg_didout = FALSE;
13610 msg_starthere();
13611 *p = c;
13612 }
13613 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013614
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013615 if (argvars[1].v_type != VAR_UNKNOWN)
13616 {
13617 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13618 if (defstr != NULL)
13619 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013621 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013622 {
13623 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013624 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013625 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013626
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013627 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013628 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013629
Bram Moolenaar4463f292005-09-25 22:20:24 +000013630 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13631 if (xp_name == NULL)
13632 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013633
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013634 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013635
Bram Moolenaar4463f292005-09-25 22:20:24 +000013636 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13637 &xp_arg) == FAIL)
13638 return;
13639 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013640 }
13641
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013642 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013643 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013644 int save_ex_normal_busy = ex_normal_busy;
13645 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013646 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013647 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13648 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013649 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013650 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013651 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013652 && argvars[1].v_type != VAR_UNKNOWN
13653 && argvars[2].v_type != VAR_UNKNOWN)
13654 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13655 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013656
13657 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013658
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013659 /* since the user typed this, no need to wait for return */
13660 need_wait_return = FALSE;
13661 msg_didout = FALSE;
13662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663 cmd_silent = cmd_silent_save;
13664}
13665
13666/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013667 * "input()" function
13668 * Also handles inputsecret() when inputsecret is set.
13669 */
13670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013671f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013672{
13673 get_user_input(argvars, rettv, FALSE);
13674}
13675
13676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677 * "inputdialog()" function
13678 */
13679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013680f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681{
13682#if defined(FEAT_GUI_TEXTDIALOG)
13683 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13684 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13685 {
13686 char_u *message;
13687 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013688 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013690 message = get_tv_string_chk(&argvars[0]);
13691 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013692 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013693 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694 else
13695 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013696 if (message != NULL && defstr != NULL
13697 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013698 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013699 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700 else
13701 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013702 if (message != NULL && defstr != NULL
13703 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013704 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013705 rettv->vval.v_string = vim_strsave(
13706 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013708 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013710 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711 }
13712 else
13713#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013714 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013715}
13716
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013717/*
13718 * "inputlist()" function
13719 */
13720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013721f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013722{
13723 listitem_T *li;
13724 int selected;
13725 int mouse_used;
13726
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013727#ifdef NO_CONSOLE_INPUT
13728 /* While starting up, there is no place to enter text. */
13729 if (no_console_input())
13730 return;
13731#endif
13732 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13733 {
13734 EMSG2(_(e_listarg), "inputlist()");
13735 return;
13736 }
13737
13738 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013739 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013740 lines_left = Rows; /* avoid more prompt */
13741 msg_scroll = TRUE;
13742 msg_clr_eos();
13743
13744 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13745 {
13746 msg_puts(get_tv_string(&li->li_tv));
13747 msg_putchar('\n');
13748 }
13749
13750 /* Ask for choice. */
13751 selected = prompt_for_number(&mouse_used);
13752 if (mouse_used)
13753 selected -= lines_left;
13754
13755 rettv->vval.v_number = selected;
13756}
13757
13758
Bram Moolenaar071d4272004-06-13 20:20:40 +000013759static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13760
13761/*
13762 * "inputrestore()" function
13763 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013765f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013766{
13767 if (ga_userinput.ga_len > 0)
13768 {
13769 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13771 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013772 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773 }
13774 else if (p_verbose > 1)
13775 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013776 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013777 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013778 }
13779}
13780
13781/*
13782 * "inputsave()" function
13783 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013785f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013787 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788 if (ga_grow(&ga_userinput, 1) == OK)
13789 {
13790 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13791 + ga_userinput.ga_len);
13792 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013793 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794 }
13795 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013796 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797}
13798
13799/*
13800 * "inputsecret()" function
13801 */
13802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013803f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804{
13805 ++cmdline_star;
13806 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013807 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808 --cmdline_star;
13809 --inputsecret_flag;
13810}
13811
13812/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013813 * "insert()" function
13814 */
13815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013816f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013817{
13818 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013819 listitem_T *item;
13820 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013821 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013822
13823 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013824 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013825 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013826 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013827 {
13828 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013829 before = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013830 if (error)
13831 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013832
Bram Moolenaar758711c2005-02-02 23:11:38 +000013833 if (before == l->lv_len)
13834 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013835 else
13836 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013837 item = list_find(l, before);
13838 if (item == NULL)
13839 {
13840 EMSGN(_(e_listidx), before);
13841 l = NULL;
13842 }
13843 }
13844 if (l != NULL)
13845 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013846 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013847 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013848 }
13849 }
13850}
13851
13852/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013853 * "invert(expr)" function
13854 */
13855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013856f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013857{
13858 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13859}
13860
13861/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862 * "isdirectory()" function
13863 */
13864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013865f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013867 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868}
13869
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013870/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013871 * "islocked()" function
13872 */
13873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013874f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013875{
13876 lval_T lv;
13877 char_u *end;
13878 dictitem_T *di;
13879
13880 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013881 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13882 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013883 if (end != NULL && lv.ll_name != NULL)
13884 {
13885 if (*end != NUL)
13886 EMSG(_(e_trailing));
13887 else
13888 {
13889 if (lv.ll_tv == NULL)
13890 {
13891 if (check_changedtick(lv.ll_name))
13892 rettv->vval.v_number = 1; /* always locked */
13893 else
13894 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013895 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013896 if (di != NULL)
13897 {
13898 /* Consider a variable locked when:
13899 * 1. the variable itself is locked
13900 * 2. the value of the variable is locked.
13901 * 3. the List or Dict value is locked.
13902 */
13903 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13904 || tv_islocked(&di->di_tv));
13905 }
13906 }
13907 }
13908 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013909 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013910 else if (lv.ll_newkey != NULL)
13911 EMSG2(_(e_dictkey), lv.ll_newkey);
13912 else if (lv.ll_list != NULL)
13913 /* List item. */
13914 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13915 else
13916 /* Dictionary item. */
13917 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13918 }
13919 }
13920
13921 clear_lval(&lv);
13922}
13923
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010013924#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
13925/*
13926 * "isnan()" function
13927 */
13928 static void
13929f_isnan(typval_T *argvars, typval_T *rettv)
13930{
13931 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
13932 && isnan(argvars[0].vval.v_float);
13933}
13934#endif
13935
Bram Moolenaar8c711452005-01-14 21:53:12 +000013936/*
13937 * "items(dict)" function
13938 */
13939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013940f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013941{
13942 dict_list(argvars, rettv, 2);
13943}
13944
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013945#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010013946/*
13947 * Get the job from the argument.
13948 * Returns NULL if the job is invalid.
13949 */
13950 static job_T *
13951get_job_arg(typval_T *tv)
13952{
13953 job_T *job;
13954
13955 if (tv->v_type != VAR_JOB)
13956 {
13957 EMSG2(_(e_invarg2), get_tv_string(tv));
13958 return NULL;
13959 }
13960 job = tv->vval.v_job;
13961
13962 if (job == NULL)
13963 EMSG(_("E916: not a valid job"));
13964 return job;
13965}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010013966
Bram Moolenaar835dc632016-02-07 14:27:38 +010013967/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010013968 * "job_getchannel()" function
13969 */
13970 static void
13971f_job_getchannel(typval_T *argvars, typval_T *rettv)
13972{
Bram Moolenaar65edff82016-02-21 16:40:11 +010013973 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010013974
Bram Moolenaar65edff82016-02-21 16:40:11 +010013975 if (job != NULL)
13976 {
Bram Moolenaar77073442016-02-13 23:23:53 +010013977 rettv->v_type = VAR_CHANNEL;
13978 rettv->vval.v_channel = job->jv_channel;
13979 if (job->jv_channel != NULL)
13980 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010013981 }
13982}
13983
13984/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010013985 * "job_info()" function
13986 */
13987 static void
13988f_job_info(typval_T *argvars, typval_T *rettv)
13989{
13990 job_T *job = get_job_arg(&argvars[0]);
13991
13992 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
13993 job_info(job, rettv->vval.v_dict);
13994}
13995
13996/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010013997 * "job_setoptions()" function
13998 */
13999 static void
14000f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14001{
14002 job_T *job = get_job_arg(&argvars[0]);
14003 jobopt_T opt;
14004
14005 if (job == NULL)
14006 return;
14007 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020014008 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
14009 job_set_options(job, &opt);
14010 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010014011}
14012
14013/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014014 * "job_start()" function
14015 */
14016 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010014017f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014018{
Bram Moolenaar835dc632016-02-07 14:27:38 +010014019 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020014020 if (check_restricted() || check_secure())
14021 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014022 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014023}
14024
14025/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014026 * "job_status()" function
14027 */
14028 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014029f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014030{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014031 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014032
Bram Moolenaar65edff82016-02-21 16:40:11 +010014033 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014034 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010014035 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010014036 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010014037 }
14038}
14039
14040/*
14041 * "job_stop()" function
14042 */
14043 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014044f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014045{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014046 job_T *job = get_job_arg(&argvars[0]);
14047
14048 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014049 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014050}
14051#endif
14052
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014054 * "join()" function
14055 */
14056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014057f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014058{
14059 garray_T ga;
14060 char_u *sep;
14061
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014062 if (argvars[0].v_type != VAR_LIST)
14063 {
14064 EMSG(_(e_listreq));
14065 return;
14066 }
14067 if (argvars[0].vval.v_list == NULL)
14068 return;
14069 if (argvars[1].v_type == VAR_UNKNOWN)
14070 sep = (char_u *)" ";
14071 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014072 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014073
14074 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014075
14076 if (sep != NULL)
14077 {
14078 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar18dfb442016-05-31 22:31:23 +020014079 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014080 ga_append(&ga, NUL);
14081 rettv->vval.v_string = (char_u *)ga.ga_data;
14082 }
14083 else
14084 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014085}
14086
14087/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014088 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014089 */
14090 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014091f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014092{
14093 js_read_T reader;
14094
14095 reader.js_buf = get_tv_string(&argvars[0]);
14096 reader.js_fill = NULL;
14097 reader.js_used = 0;
14098 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14099 EMSG(_(e_invarg));
14100}
14101
14102/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014103 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014104 */
14105 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014106f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014107{
14108 rettv->v_type = VAR_STRING;
14109 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14110}
14111
14112/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014113 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014114 */
14115 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014116f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014117{
14118 js_read_T reader;
14119
14120 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014121 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014122 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014123 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014124 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014125}
14126
14127/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014128 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014129 */
14130 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014131f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014132{
14133 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014134 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014135}
14136
14137/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014138 * "keys()" function
14139 */
14140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014141f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014142{
14143 dict_list(argvars, rettv, 0);
14144}
14145
14146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147 * "last_buffer_nr()" function.
14148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014150f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151{
14152 int n = 0;
14153 buf_T *buf;
14154
14155 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14156 if (n < buf->b_fnum)
14157 n = buf->b_fnum;
14158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014159 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014160}
14161
14162/*
14163 * "len()" function
14164 */
14165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014166f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014167{
14168 switch (argvars[0].v_type)
14169 {
14170 case VAR_STRING:
14171 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014172 rettv->vval.v_number = (varnumber_T)STRLEN(
14173 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014174 break;
14175 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014176 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014177 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014178 case VAR_DICT:
14179 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14180 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014181 case VAR_UNKNOWN:
14182 case VAR_SPECIAL:
14183 case VAR_FLOAT:
14184 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010014185 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014186 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014187 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014188 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014189 break;
14190 }
14191}
14192
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014193static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014194
14195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014196libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014197{
14198#ifdef FEAT_LIBCALL
14199 char_u *string_in;
14200 char_u **string_result;
14201 int nr_result;
14202#endif
14203
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014204 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014205 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014206 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014207
14208 if (check_restricted() || check_secure())
14209 return;
14210
14211#ifdef FEAT_LIBCALL
14212 /* The first two args must be strings, otherwise its meaningless */
14213 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14214 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014215 string_in = NULL;
14216 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014217 string_in = argvars[2].vval.v_string;
14218 if (type == VAR_NUMBER)
14219 string_result = NULL;
14220 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014221 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014222 if (mch_libcall(argvars[0].vval.v_string,
14223 argvars[1].vval.v_string,
14224 string_in,
14225 argvars[2].vval.v_number,
14226 string_result,
14227 &nr_result) == OK
14228 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014229 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014230 }
14231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232}
14233
14234/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014235 * "libcall()" function
14236 */
14237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014238f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014239{
14240 libcall_common(argvars, rettv, VAR_STRING);
14241}
14242
14243/*
14244 * "libcallnr()" function
14245 */
14246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014247f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014248{
14249 libcall_common(argvars, rettv, VAR_NUMBER);
14250}
14251
14252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014253 * "line(string)" function
14254 */
14255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014256f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014257{
14258 linenr_T lnum = 0;
14259 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014260 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014262 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263 if (fp != NULL)
14264 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014265 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014266}
14267
14268/*
14269 * "line2byte(lnum)" function
14270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014272f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273{
14274#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014275 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014276#else
14277 linenr_T lnum;
14278
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014279 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014281 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014283 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14284 if (rettv->vval.v_number >= 0)
14285 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286#endif
14287}
14288
14289/*
14290 * "lispindent(lnum)" function
14291 */
14292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014293f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294{
14295#ifdef FEAT_LISP
14296 pos_T pos;
14297 linenr_T lnum;
14298
14299 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014300 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14302 {
14303 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014304 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305 curwin->w_cursor = pos;
14306 }
14307 else
14308#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014309 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310}
14311
14312/*
14313 * "localtime()" function
14314 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014316f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014317{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014318 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319}
14320
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014321static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322
14323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014324get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014325{
14326 char_u *keys;
14327 char_u *which;
14328 char_u buf[NUMBUFLEN];
14329 char_u *keys_buf = NULL;
14330 char_u *rhs;
14331 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014332 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014333 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014334 mapblock_T *mp;
14335 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336
14337 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014338 rettv->v_type = VAR_STRING;
14339 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014341 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342 if (*keys == NUL)
14343 return;
14344
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014345 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014346 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014347 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014348 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014349 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014350 abbr = (int)get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014351 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014352 get_dict = (int)get_tv_number(&argvars[3]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014353 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355 else
14356 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014357 if (which == NULL)
14358 return;
14359
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360 mode = get_map_mode(&which, 0);
14361
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014362 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014363 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014365
14366 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014368 /* Return a string. */
14369 if (rhs != NULL)
14370 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371
Bram Moolenaarbd743252010-10-20 21:23:33 +020014372 }
14373 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14374 {
14375 /* Return a dictionary. */
14376 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14377 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14378 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014379
Bram Moolenaarbd743252010-10-20 21:23:33 +020014380 dict_add_nr_str(dict, "lhs", 0L, lhs);
14381 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14382 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14383 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14384 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14385 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14386 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014387 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014388 dict_add_nr_str(dict, "mode", 0L, mapmode);
14389
14390 vim_free(lhs);
14391 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014392 }
14393}
14394
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014395#ifdef FEAT_FLOAT
14396/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014397 * "log()" function
14398 */
14399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014400f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014401{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010014402 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014403
14404 rettv->v_type = VAR_FLOAT;
14405 if (get_float_arg(argvars, &f) == OK)
14406 rettv->vval.v_float = log(f);
14407 else
14408 rettv->vval.v_float = 0.0;
14409}
14410
14411/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014412 * "log10()" function
14413 */
14414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014415f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014416{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010014417 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014418
14419 rettv->v_type = VAR_FLOAT;
14420 if (get_float_arg(argvars, &f) == OK)
14421 rettv->vval.v_float = log10(f);
14422 else
14423 rettv->vval.v_float = 0.0;
14424}
14425#endif
14426
Bram Moolenaar1dced572012-04-05 16:54:08 +020014427#ifdef FEAT_LUA
14428/*
14429 * "luaeval()" function
14430 */
14431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014432f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020014433{
14434 char_u *str;
14435 char_u buf[NUMBUFLEN];
14436
14437 str = get_tv_string_buf(&argvars[0], buf);
14438 do_luaeval(str, argvars + 1, rettv);
14439}
14440#endif
14441
Bram Moolenaar071d4272004-06-13 20:20:40 +000014442/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014443 * "map()" function
14444 */
14445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014446f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014447{
14448 filter_map(argvars, rettv, TRUE);
14449}
14450
14451/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014452 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453 */
14454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014455f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014457 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458}
14459
14460/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014461 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462 */
14463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014464f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014466 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467}
14468
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014469static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014470
14471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014472find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014474 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014475 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014476 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477 char_u *pat;
14478 regmatch_T regmatch;
14479 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014480 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481 char_u *save_cpo;
14482 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014483 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014484 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014485 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014486 list_T *l = NULL;
14487 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014488 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014489 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490
14491 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14492 save_cpo = p_cpo;
14493 p_cpo = (char_u *)"";
14494
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014495 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020014496 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014497 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020014498 /* type 3: return empty list when there are no matches.
14499 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014500 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014501 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020014502 if (type == 4
14503 && (list_append_string(rettv->vval.v_list,
14504 (char_u *)"", 0) == FAIL
14505 || list_append_number(rettv->vval.v_list,
14506 (varnumber_T)-1) == FAIL
14507 || list_append_number(rettv->vval.v_list,
14508 (varnumber_T)-1) == FAIL
14509 || list_append_number(rettv->vval.v_list,
14510 (varnumber_T)-1) == FAIL))
14511 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020014512 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020014513 rettv->vval.v_list = NULL;
14514 goto theend;
14515 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014516 }
14517 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014519 rettv->v_type = VAR_STRING;
14520 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014523 if (argvars[0].v_type == VAR_LIST)
14524 {
14525 if ((l = argvars[0].vval.v_list) == NULL)
14526 goto theend;
14527 li = l->lv_first;
14528 }
14529 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014530 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014531 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014532 len = (long)STRLEN(str);
14533 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014534
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014535 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14536 if (pat == NULL)
14537 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014538
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014539 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014541 int error = FALSE;
14542
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014543 start = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014544 if (error)
14545 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014546 if (l != NULL)
14547 {
14548 li = list_find(l, start);
14549 if (li == NULL)
14550 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014551 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014552 }
14553 else
14554 {
14555 if (start < 0)
14556 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014557 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014558 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014559 /* When "count" argument is there ignore matches before "start",
14560 * otherwise skip part of the string. Differs when pattern is "^"
14561 * or "\<". */
14562 if (argvars[3].v_type != VAR_UNKNOWN)
14563 startcol = start;
14564 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014565 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014566 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014567 len -= start;
14568 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014569 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014570
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014571 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014572 nth = (long)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014573 if (error)
14574 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575 }
14576
14577 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14578 if (regmatch.regprog != NULL)
14579 {
14580 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014581
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014582 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014583 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014584 if (l != NULL)
14585 {
14586 if (li == NULL)
14587 {
14588 match = FALSE;
14589 break;
14590 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014591 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020014592 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014593 if (str == NULL)
14594 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014595 }
14596
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014597 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014598
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014599 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014600 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014601 if (l == NULL && !match)
14602 break;
14603
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014604 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014605 if (l != NULL)
14606 {
14607 li = li->li_next;
14608 ++idx;
14609 }
14610 else
14611 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014612#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014613 startcol = (colnr_T)(regmatch.startp[0]
14614 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014615#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014616 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014617#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014618 if (startcol > (colnr_T)len
14619 || str + startcol <= regmatch.startp[0])
14620 {
14621 match = FALSE;
14622 break;
14623 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014624 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014625 }
14626
14627 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014628 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020014629 if (type == 4)
14630 {
14631 listitem_T *li1 = rettv->vval.v_list->lv_first;
14632 listitem_T *li2 = li1->li_next;
14633 listitem_T *li3 = li2->li_next;
14634 listitem_T *li4 = li3->li_next;
14635
Bram Moolenaar3c809342016-06-01 22:34:48 +020014636 vim_free(li1->li_tv.vval.v_string);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020014637 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
14638 (int)(regmatch.endp[0] - regmatch.startp[0]));
14639 li3->li_tv.vval.v_number =
14640 (varnumber_T)(regmatch.startp[0] - expr);
14641 li4->li_tv.vval.v_number =
14642 (varnumber_T)(regmatch.endp[0] - expr);
14643 if (l != NULL)
14644 li2->li_tv.vval.v_number = (varnumber_T)idx;
14645 }
14646 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014647 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014648 int i;
14649
14650 /* return list with matched string and submatches */
14651 for (i = 0; i < NSUBEXP; ++i)
14652 {
14653 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014654 {
14655 if (list_append_string(rettv->vval.v_list,
14656 (char_u *)"", 0) == FAIL)
14657 break;
14658 }
14659 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014660 regmatch.startp[i],
14661 (int)(regmatch.endp[i] - regmatch.startp[i]))
14662 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014663 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014664 }
14665 }
14666 else if (type == 2)
14667 {
14668 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014669 if (l != NULL)
14670 copy_tv(&li->li_tv, rettv);
14671 else
14672 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014673 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014674 }
14675 else if (l != NULL)
14676 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677 else
14678 {
14679 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014680 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014681 (varnumber_T)(regmatch.startp[0] - str);
14682 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014683 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014685 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014686 }
14687 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014688 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014689 }
14690
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020014691 if (type == 4 && l == NULL)
14692 /* matchstrpos() without a list: drop the second item. */
14693 listitem_remove(rettv->vval.v_list,
14694 rettv->vval.v_list->lv_first->li_next);
14695
Bram Moolenaar071d4272004-06-13 20:20:40 +000014696theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014697 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014698 p_cpo = save_cpo;
14699}
14700
14701/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014702 * "match()" function
14703 */
14704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014705f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014706{
14707 find_some_match(argvars, rettv, 1);
14708}
14709
14710/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014711 * "matchadd()" function
14712 */
14713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014714f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014715{
14716#ifdef FEAT_SEARCH_EXTRA
14717 char_u buf[NUMBUFLEN];
14718 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14719 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14720 int prio = 10; /* default priority */
14721 int id = -1;
14722 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014723 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014724
14725 rettv->vval.v_number = -1;
14726
14727 if (grp == NULL || pat == NULL)
14728 return;
14729 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014730 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014731 prio = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014732 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014733 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014734 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014735 if (argvars[4].v_type != VAR_UNKNOWN)
14736 {
14737 if (argvars[4].v_type != VAR_DICT)
14738 {
14739 EMSG(_(e_dictreq));
14740 return;
14741 }
14742 if (dict_find(argvars[4].vval.v_dict,
14743 (char_u *)"conceal", -1) != NULL)
14744 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14745 (char_u *)"conceal", FALSE);
14746 }
14747 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014748 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014749 if (error == TRUE)
14750 return;
14751 if (id >= 1 && id <= 3)
14752 {
14753 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14754 return;
14755 }
14756
Bram Moolenaar6561d522015-07-21 15:48:27 +020014757 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14758 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014759#endif
14760}
14761
14762/*
14763 * "matchaddpos()" function
14764 */
14765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014766f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020014767{
14768#ifdef FEAT_SEARCH_EXTRA
14769 char_u buf[NUMBUFLEN];
14770 char_u *group;
14771 int prio = 10;
14772 int id = -1;
14773 int error = FALSE;
14774 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014775 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014776
14777 rettv->vval.v_number = -1;
14778
14779 group = get_tv_string_buf_chk(&argvars[0], buf);
14780 if (group == NULL)
14781 return;
14782
14783 if (argvars[1].v_type != VAR_LIST)
14784 {
14785 EMSG2(_(e_listarg), "matchaddpos()");
14786 return;
14787 }
14788 l = argvars[1].vval.v_list;
14789 if (l == NULL)
14790 return;
14791
14792 if (argvars[2].v_type != VAR_UNKNOWN)
14793 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014794 prio = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014795 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014796 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014797 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014798 if (argvars[4].v_type != VAR_UNKNOWN)
14799 {
14800 if (argvars[4].v_type != VAR_DICT)
14801 {
14802 EMSG(_(e_dictreq));
14803 return;
14804 }
14805 if (dict_find(argvars[4].vval.v_dict,
14806 (char_u *)"conceal", -1) != NULL)
14807 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14808 (char_u *)"conceal", FALSE);
14809 }
14810 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014811 }
14812 if (error == TRUE)
14813 return;
14814
14815 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14816 if (id == 1 || id == 2)
14817 {
14818 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14819 return;
14820 }
14821
Bram Moolenaar6561d522015-07-21 15:48:27 +020014822 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14823 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014824#endif
14825}
14826
14827/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014828 * "matcharg()" function
14829 */
14830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014831f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014832{
14833 if (rettv_list_alloc(rettv) == OK)
14834 {
14835#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014836 int id = (int)get_tv_number(&argvars[0]);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014837 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014838
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014839 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014840 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014841 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14842 {
14843 list_append_string(rettv->vval.v_list,
14844 syn_id2name(m->hlg_id), -1);
14845 list_append_string(rettv->vval.v_list, m->pattern, -1);
14846 }
14847 else
14848 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014849 list_append_string(rettv->vval.v_list, NULL, -1);
14850 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014851 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014852 }
14853#endif
14854 }
14855}
14856
14857/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014858 * "matchdelete()" function
14859 */
14860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014861f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014862{
14863#ifdef FEAT_SEARCH_EXTRA
14864 rettv->vval.v_number = match_delete(curwin,
14865 (int)get_tv_number(&argvars[0]), TRUE);
14866#endif
14867}
14868
14869/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014870 * "matchend()" function
14871 */
14872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014873f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014874{
14875 find_some_match(argvars, rettv, 0);
14876}
14877
14878/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014879 * "matchlist()" function
14880 */
14881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014882f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014883{
14884 find_some_match(argvars, rettv, 3);
14885}
14886
14887/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014888 * "matchstr()" function
14889 */
14890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014891f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014892{
14893 find_some_match(argvars, rettv, 2);
14894}
14895
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020014896/*
14897 * "matchstrpos()" function
14898 */
14899 static void
14900f_matchstrpos(typval_T *argvars, typval_T *rettv)
14901{
14902 find_some_match(argvars, rettv, 4);
14903}
14904
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014905static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014906
14907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014908max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014909{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014910 varnumber_T n = 0;
14911 varnumber_T i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014912 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014913
14914 if (argvars[0].v_type == VAR_LIST)
14915 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014916 list_T *l;
14917 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014918
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014919 l = argvars[0].vval.v_list;
14920 if (l != NULL)
14921 {
14922 li = l->lv_first;
14923 if (li != NULL)
14924 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014925 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014926 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014927 {
14928 li = li->li_next;
14929 if (li == NULL)
14930 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014931 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014932 if (domax ? i > n : i < n)
14933 n = i;
14934 }
14935 }
14936 }
14937 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014938 else if (argvars[0].v_type == VAR_DICT)
14939 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014940 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014941 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014942 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014943 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014944
14945 d = argvars[0].vval.v_dict;
14946 if (d != NULL)
14947 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014948 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014949 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014950 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014951 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014952 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014953 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014954 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014955 if (first)
14956 {
14957 n = i;
14958 first = FALSE;
14959 }
14960 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014961 n = i;
14962 }
14963 }
14964 }
14965 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014966 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014967 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014968 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014969}
14970
14971/*
14972 * "max()" function
14973 */
14974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014975f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014976{
14977 max_min(argvars, rettv, TRUE);
14978}
14979
14980/*
14981 * "min()" function
14982 */
14983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014984f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014985{
14986 max_min(argvars, rettv, FALSE);
14987}
14988
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014989static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014990
14991/*
14992 * Create the directory in which "dir" is located, and higher levels when
14993 * needed.
14994 */
14995 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010014996mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014997{
14998 char_u *p;
14999 char_u *updir;
15000 int r = FAIL;
15001
15002 /* Get end of directory name in "dir".
15003 * We're done when it's "/" or "c:/". */
15004 p = gettail_sep(dir);
15005 if (p <= get_past_head(dir))
15006 return OK;
15007
15008 /* If the directory exists we're done. Otherwise: create it.*/
15009 updir = vim_strnsave(dir, (int)(p - dir));
15010 if (updir == NULL)
15011 return FAIL;
15012 if (mch_isdir(updir))
15013 r = OK;
15014 else if (mkdir_recurse(updir, prot) == OK)
15015 r = vim_mkdir_emsg(updir, prot);
15016 vim_free(updir);
15017 return r;
15018}
15019
15020#ifdef vim_mkdir
15021/*
15022 * "mkdir()" function
15023 */
15024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015025f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015026{
15027 char_u *dir;
15028 char_u buf[NUMBUFLEN];
15029 int prot = 0755;
15030
15031 rettv->vval.v_number = FAIL;
15032 if (check_restricted() || check_secure())
15033 return;
15034
15035 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015036 if (*dir == NUL)
15037 rettv->vval.v_number = FAIL;
15038 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015039 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015040 if (*gettail(dir) == NUL)
15041 /* remove trailing slashes */
15042 *gettail_sep(dir) = NUL;
15043
15044 if (argvars[1].v_type != VAR_UNKNOWN)
15045 {
15046 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015047 prot = (int)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015048 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15049 mkdir_recurse(dir, prot);
15050 }
15051 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015052 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015053}
15054#endif
15055
Bram Moolenaar0d660222005-01-07 21:51:51 +000015056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015057 * "mode()" function
15058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015060f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015061{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015062 char_u buf[3];
15063
15064 buf[1] = NUL;
15065 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015066
Bram Moolenaare381d3d2016-07-07 14:50:41 +020015067 if (time_for_testing == 93784)
15068 {
15069 /* Testing the two-character code. */
15070 buf[0] = 'x';
15071 buf[1] = '!';
15072 }
15073 else if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015074 {
15075 if (VIsual_select)
15076 buf[0] = VIsual_mode + 's' - 'v';
15077 else
15078 buf[0] = VIsual_mode;
15079 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015080 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015081 || State == CONFIRM)
15082 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015083 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015084 if (State == ASKMORE)
15085 buf[1] = 'm';
15086 else if (State == CONFIRM)
15087 buf[1] = '?';
15088 }
15089 else if (State == EXTERNCMD)
15090 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015091 else if (State & INSERT)
15092 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015093#ifdef FEAT_VREPLACE
15094 if (State & VREPLACE_FLAG)
15095 {
15096 buf[0] = 'R';
15097 buf[1] = 'v';
15098 }
15099 else
15100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015101 if (State & REPLACE_FLAG)
15102 buf[0] = 'R';
15103 else
15104 buf[0] = 'i';
15105 }
15106 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015107 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015108 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015109 if (exmode_active)
15110 buf[1] = 'v';
15111 }
15112 else if (exmode_active)
15113 {
15114 buf[0] = 'c';
15115 buf[1] = 'e';
15116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015117 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015118 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015119 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015120 if (finish_op)
15121 buf[1] = 'o';
15122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015123
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015124 /* Clear out the minor mode when the argument is not a non-zero number or
15125 * non-empty string. */
15126 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015127 buf[1] = NUL;
15128
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015129 rettv->vval.v_string = vim_strsave(buf);
15130 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131}
15132
Bram Moolenaar429fa852013-04-15 12:27:36 +020015133#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015134/*
15135 * "mzeval()" function
15136 */
15137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015138f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015139{
15140 char_u *str;
15141 char_u buf[NUMBUFLEN];
15142
15143 str = get_tv_string_buf(&argvars[0], buf);
15144 do_mzeval(str, rettv);
15145}
Bram Moolenaar75676462013-01-30 14:55:42 +010015146
15147 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015148mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015149{
15150 typval_T argvars[3];
15151
15152 argvars[0].v_type = VAR_STRING;
15153 argvars[0].vval.v_string = name;
15154 copy_tv(args, &argvars[1]);
15155 argvars[2].v_type = VAR_UNKNOWN;
15156 f_call(argvars, rettv);
15157 clear_tv(&argvars[1]);
15158}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015159#endif
15160
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015162 * "nextnonblank()" function
15163 */
15164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015165f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015166{
15167 linenr_T lnum;
15168
15169 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15170 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015171 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015172 {
15173 lnum = 0;
15174 break;
15175 }
15176 if (*skipwhite(ml_get(lnum)) != NUL)
15177 break;
15178 }
15179 rettv->vval.v_number = lnum;
15180}
15181
15182/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015183 * "nr2char()" function
15184 */
15185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015186f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015187{
15188 char_u buf[NUMBUFLEN];
15189
15190#ifdef FEAT_MBYTE
15191 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015192 {
15193 int utf8 = 0;
15194
15195 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015196 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaard35d7842013-01-23 17:17:10 +010015197 if (utf8)
15198 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15199 else
15200 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015202 else
15203#endif
15204 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015205 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015206 buf[1] = NUL;
15207 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015208 rettv->v_type = VAR_STRING;
15209 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015210}
15211
15212/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015213 * "or(expr, expr)" function
15214 */
15215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015216f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015217{
15218 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15219 | get_tv_number_chk(&argvars[1], NULL);
15220}
15221
15222/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015223 * "pathshorten()" function
15224 */
15225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015226f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015227{
15228 char_u *p;
15229
15230 rettv->v_type = VAR_STRING;
15231 p = get_tv_string_chk(&argvars[0]);
15232 if (p == NULL)
15233 rettv->vval.v_string = NULL;
15234 else
15235 {
15236 p = vim_strsave(p);
15237 rettv->vval.v_string = p;
15238 if (p != NULL)
15239 shorten_dir(p);
15240 }
15241}
15242
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015243#ifdef FEAT_PERL
15244/*
15245 * "perleval()" function
15246 */
15247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015248f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015249{
15250 char_u *str;
15251 char_u buf[NUMBUFLEN];
15252
15253 str = get_tv_string_buf(&argvars[0], buf);
15254 do_perleval(str, rettv);
15255}
15256#endif
15257
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015258#ifdef FEAT_FLOAT
15259/*
15260 * "pow()" function
15261 */
15262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015263f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015264{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015265 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015266
15267 rettv->v_type = VAR_FLOAT;
15268 if (get_float_arg(argvars, &fx) == OK
15269 && get_float_arg(&argvars[1], &fy) == OK)
15270 rettv->vval.v_float = pow(fx, fy);
15271 else
15272 rettv->vval.v_float = 0.0;
15273}
15274#endif
15275
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015276/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015277 * "prevnonblank()" function
15278 */
15279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015280f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015281{
15282 linenr_T lnum;
15283
15284 lnum = get_tv_lnum(argvars);
15285 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15286 lnum = 0;
15287 else
15288 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15289 --lnum;
15290 rettv->vval.v_number = lnum;
15291}
15292
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015293/* This dummy va_list is here because:
15294 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15295 * - locally in the function results in a "used before set" warning
15296 * - using va_start() to initialize it gives "function with fixed args" error */
15297static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015298
Bram Moolenaar8c711452005-01-14 21:53:12 +000015299/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015300 * "printf()" function
15301 */
15302 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015303f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015304{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015305 char_u buf[NUMBUFLEN];
15306 int len;
15307 char_u *s;
15308 int saved_did_emsg = did_emsg;
15309 char *fmt;
15310
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015311 rettv->v_type = VAR_STRING;
15312 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015313
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015314 /* Get the required length, allocate the buffer and do it for real. */
15315 did_emsg = FALSE;
15316 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15317 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15318 if (!did_emsg)
15319 {
15320 s = alloc(len + 1);
15321 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015322 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015323 rettv->vval.v_string = s;
15324 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015325 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015326 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015327 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015328}
15329
15330/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015331 * "pumvisible()" function
15332 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015334f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015335{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015336#ifdef FEAT_INS_EXPAND
15337 if (pum_visible())
15338 rettv->vval.v_number = 1;
15339#endif
15340}
15341
Bram Moolenaardb913952012-06-29 12:54:53 +020015342#ifdef FEAT_PYTHON3
15343/*
15344 * "py3eval()" function
15345 */
15346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015347f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015348{
15349 char_u *str;
15350 char_u buf[NUMBUFLEN];
15351
15352 str = get_tv_string_buf(&argvars[0], buf);
15353 do_py3eval(str, rettv);
15354}
15355#endif
15356
15357#ifdef FEAT_PYTHON
15358/*
15359 * "pyeval()" function
15360 */
15361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015362f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015363{
15364 char_u *str;
15365 char_u buf[NUMBUFLEN];
15366
15367 str = get_tv_string_buf(&argvars[0], buf);
15368 do_pyeval(str, rettv);
15369}
15370#endif
15371
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015372/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015373 * "range()" function
15374 */
15375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015376f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015377{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015378 varnumber_T start;
15379 varnumber_T end;
15380 varnumber_T stride = 1;
15381 varnumber_T i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015382 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015383
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015384 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015385 if (argvars[1].v_type == VAR_UNKNOWN)
15386 {
15387 end = start - 1;
15388 start = 0;
15389 }
15390 else
15391 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015392 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015393 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015394 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015395 }
15396
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015397 if (error)
15398 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015399 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015400 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015401 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015402 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015403 else
15404 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015405 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015406 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015407 if (list_append_number(rettv->vval.v_list,
15408 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015409 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015410 }
15411}
15412
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015413/*
15414 * "readfile()" function
15415 */
15416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015417f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015418{
15419 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015420 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015421 char_u *fname;
15422 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015423 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15424 int io_size = sizeof(buf);
15425 int readlen; /* size of last fread() */
15426 char_u *prev = NULL; /* previously read bytes, if any */
15427 long prevlen = 0; /* length of data in prev */
15428 long prevsize = 0; /* size of prev buffer */
15429 long maxline = MAXLNUM;
15430 long cnt = 0;
15431 char_u *p; /* position in buf */
15432 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015433
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015434 if (argvars[1].v_type != VAR_UNKNOWN)
15435 {
15436 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15437 binary = TRUE;
15438 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015439 maxline = (long)get_tv_number(&argvars[2]);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015440 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015441
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015442 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015443 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015444
15445 /* Always open the file in binary mode, library functions have a mind of
15446 * their own about CR-LF conversion. */
15447 fname = get_tv_string(&argvars[0]);
15448 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15449 {
15450 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15451 return;
15452 }
15453
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015454 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015455 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015456 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015457
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015458 /* This for loop processes what was read, but is also entered at end
15459 * of file so that either:
15460 * - an incomplete line gets written
15461 * - a "binary" file gets an empty line at the end if it ends in a
15462 * newline. */
15463 for (p = buf, start = buf;
15464 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15465 ++p)
15466 {
15467 if (*p == '\n' || readlen <= 0)
15468 {
15469 listitem_T *li;
15470 char_u *s = NULL;
15471 long_u len = p - start;
15472
15473 /* Finished a line. Remove CRs before NL. */
15474 if (readlen > 0 && !binary)
15475 {
15476 while (len > 0 && start[len - 1] == '\r')
15477 --len;
15478 /* removal may cross back to the "prev" string */
15479 if (len == 0)
15480 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15481 --prevlen;
15482 }
15483 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015484 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015485 else
15486 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015487 /* Change "prev" buffer to be the right size. This way
15488 * the bytes are only copied once, and very long lines are
15489 * allocated only once. */
15490 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015491 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015492 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015493 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015494 prev = NULL; /* the list will own the string */
15495 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015496 }
15497 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015498 if (s == NULL)
15499 {
15500 do_outofmem_msg((long_u) prevlen + len + 1);
15501 failed = TRUE;
15502 break;
15503 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015504
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015505 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015506 {
15507 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015508 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015509 break;
15510 }
15511 li->li_tv.v_type = VAR_STRING;
15512 li->li_tv.v_lock = 0;
15513 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015514 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015515
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015516 start = p + 1; /* step over newline */
15517 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015518 break;
15519 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015520 else if (*p == NUL)
15521 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015522#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015523 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15524 * when finding the BF and check the previous two bytes. */
15525 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015526 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015527 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15528 * + 1, these may be in the "prev" string. */
15529 char_u back1 = p >= buf + 1 ? p[-1]
15530 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15531 char_u back2 = p >= buf + 2 ? p[-2]
15532 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15533 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015534
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015535 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015536 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015537 char_u *dest = p - 2;
15538
15539 /* Usually a BOM is at the beginning of a file, and so at
15540 * the beginning of a line; then we can just step over it.
15541 */
15542 if (start == dest)
15543 start = p + 1;
15544 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015545 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015546 /* have to shuffle buf to close gap */
15547 int adjust_prevlen = 0;
15548
15549 if (dest < buf)
15550 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015551 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015552 dest = buf;
15553 }
15554 if (readlen > p - buf + 1)
15555 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15556 readlen -= 3 - adjust_prevlen;
15557 prevlen -= adjust_prevlen;
15558 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015559 }
15560 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015561 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015562#endif
15563 } /* for */
15564
15565 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15566 break;
15567 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015568 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015569 /* There's part of a line in buf, store it in "prev". */
15570 if (p - start + prevlen >= prevsize)
15571 {
15572 /* need bigger "prev" buffer */
15573 char_u *newprev;
15574
15575 /* A common use case is ordinary text files and "prev" gets a
15576 * fragment of a line, so the first allocation is made
15577 * small, to avoid repeatedly 'allocing' large and
15578 * 'reallocing' small. */
15579 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015580 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015581 else
15582 {
15583 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015584 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015585 prevsize = grow50pc > growmin ? grow50pc : growmin;
15586 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015587 newprev = prev == NULL ? alloc(prevsize)
15588 : vim_realloc(prev, prevsize);
15589 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015590 {
15591 do_outofmem_msg((long_u)prevsize);
15592 failed = TRUE;
15593 break;
15594 }
15595 prev = newprev;
15596 }
15597 /* Add the line part to end of "prev". */
15598 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015599 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015600 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015601 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015602
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015603 /*
15604 * For a negative line count use only the lines at the end of the file,
15605 * free the rest.
15606 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015607 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015608 while (cnt > -maxline)
15609 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015610 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015611 --cnt;
15612 }
15613
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015614 if (failed)
15615 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015616 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015617 /* readfile doc says an empty list is returned on error */
15618 rettv->vval.v_list = list_alloc();
15619 }
15620
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015621 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015622 fclose(fd);
15623}
15624
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015625#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015626static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015627
15628/*
15629 * Convert a List to proftime_T.
15630 * Return FAIL when there is something wrong.
15631 */
15632 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015633list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015634{
15635 long n1, n2;
15636 int error = FALSE;
15637
15638 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15639 || arg->vval.v_list->lv_len != 2)
15640 return FAIL;
15641 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15642 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15643# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015644 tm->HighPart = n1;
15645 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015646# else
15647 tm->tv_sec = n1;
15648 tm->tv_usec = n2;
15649# endif
15650 return error ? FAIL : OK;
15651}
15652#endif /* FEAT_RELTIME */
15653
15654/*
15655 * "reltime()" function
15656 */
15657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015658f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015659{
15660#ifdef FEAT_RELTIME
15661 proftime_T res;
15662 proftime_T start;
15663
15664 if (argvars[0].v_type == VAR_UNKNOWN)
15665 {
15666 /* No arguments: get current time. */
15667 profile_start(&res);
15668 }
15669 else if (argvars[1].v_type == VAR_UNKNOWN)
15670 {
15671 if (list2proftime(&argvars[0], &res) == FAIL)
15672 return;
15673 profile_end(&res);
15674 }
15675 else
15676 {
15677 /* Two arguments: compute the difference. */
15678 if (list2proftime(&argvars[0], &start) == FAIL
15679 || list2proftime(&argvars[1], &res) == FAIL)
15680 return;
15681 profile_sub(&res, &start);
15682 }
15683
15684 if (rettv_list_alloc(rettv) == OK)
15685 {
15686 long n1, n2;
15687
15688# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015689 n1 = res.HighPart;
15690 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015691# else
15692 n1 = res.tv_sec;
15693 n2 = res.tv_usec;
15694# endif
15695 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15696 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15697 }
15698#endif
15699}
15700
Bram Moolenaar79c2c882016-02-07 21:19:28 +010015701#ifdef FEAT_FLOAT
15702/*
15703 * "reltimefloat()" function
15704 */
15705 static void
15706f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
15707{
15708# ifdef FEAT_RELTIME
15709 proftime_T tm;
15710# endif
15711
15712 rettv->v_type = VAR_FLOAT;
15713 rettv->vval.v_float = 0;
15714# ifdef FEAT_RELTIME
15715 if (list2proftime(&argvars[0], &tm) == OK)
15716 rettv->vval.v_float = profile_float(&tm);
15717# endif
15718}
15719#endif
15720
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015721/*
15722 * "reltimestr()" function
15723 */
15724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015725f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015726{
15727#ifdef FEAT_RELTIME
15728 proftime_T tm;
15729#endif
15730
15731 rettv->v_type = VAR_STRING;
15732 rettv->vval.v_string = NULL;
15733#ifdef FEAT_RELTIME
15734 if (list2proftime(&argvars[0], &tm) == OK)
15735 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15736#endif
15737}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015738
Bram Moolenaar0d660222005-01-07 21:51:51 +000015739#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015740static void make_connection(void);
15741static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015742
15743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015744make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015745{
15746 if (X_DISPLAY == NULL
15747# ifdef FEAT_GUI
15748 && !gui.in_use
15749# endif
15750 )
15751 {
15752 x_force_connect = TRUE;
15753 setup_term_clip();
15754 x_force_connect = FALSE;
15755 }
15756}
15757
15758 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015759check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015760{
15761 make_connection();
15762 if (X_DISPLAY == NULL)
15763 {
15764 EMSG(_("E240: No connection to Vim server"));
15765 return FAIL;
15766 }
15767 return OK;
15768}
15769#endif
15770
15771#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000015772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015773remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015774{
15775 char_u *server_name;
15776 char_u *keys;
15777 char_u *r = NULL;
15778 char_u buf[NUMBUFLEN];
15779# ifdef WIN32
15780 HWND w;
15781# else
15782 Window w;
15783# endif
15784
15785 if (check_restricted() || check_secure())
15786 return;
15787
15788# ifdef FEAT_X11
15789 if (check_connection() == FAIL)
15790 return;
15791# endif
15792
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015793 server_name = get_tv_string_chk(&argvars[0]);
15794 if (server_name == NULL)
15795 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015796 keys = get_tv_string_buf(&argvars[1], buf);
15797# ifdef WIN32
15798 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15799# else
15800 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15801 < 0)
15802# endif
15803 {
15804 if (r != NULL)
15805 EMSG(r); /* sending worked but evaluation failed */
15806 else
15807 EMSG2(_("E241: Unable to send to %s"), server_name);
15808 return;
15809 }
15810
15811 rettv->vval.v_string = r;
15812
15813 if (argvars[2].v_type != VAR_UNKNOWN)
15814 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015815 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015816 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015817 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015818
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015819 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015820 v.di_tv.v_type = VAR_STRING;
15821 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015822 idvar = get_tv_string_chk(&argvars[2]);
15823 if (idvar != NULL)
15824 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015825 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015826 }
15827}
15828#endif
15829
15830/*
15831 * "remote_expr()" function
15832 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015834f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015835{
15836 rettv->v_type = VAR_STRING;
15837 rettv->vval.v_string = NULL;
15838#ifdef FEAT_CLIENTSERVER
15839 remote_common(argvars, rettv, TRUE);
15840#endif
15841}
15842
15843/*
15844 * "remote_foreground()" function
15845 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015847f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015848{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015849#ifdef FEAT_CLIENTSERVER
15850# ifdef WIN32
15851 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015852 {
15853 char_u *server_name = get_tv_string_chk(&argvars[0]);
15854
15855 if (server_name != NULL)
15856 serverForeground(server_name);
15857 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015858# else
15859 /* Send a foreground() expression to the server. */
15860 argvars[1].v_type = VAR_STRING;
15861 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15862 argvars[2].v_type = VAR_UNKNOWN;
15863 remote_common(argvars, rettv, TRUE);
15864 vim_free(argvars[1].vval.v_string);
15865# endif
15866#endif
15867}
15868
Bram Moolenaar0d660222005-01-07 21:51:51 +000015869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015870f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015871{
15872#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015873 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015874 char_u *s = NULL;
15875# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015876 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015877# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015878 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015879
15880 if (check_restricted() || check_secure())
15881 {
15882 rettv->vval.v_number = -1;
15883 return;
15884 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015885 serverid = get_tv_string_chk(&argvars[0]);
15886 if (serverid == NULL)
15887 {
15888 rettv->vval.v_number = -1;
15889 return; /* type error; errmsg already given */
15890 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015891# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010015892 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015893 if (n == 0)
15894 rettv->vval.v_number = -1;
15895 else
15896 {
15897 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15898 rettv->vval.v_number = (s != NULL);
15899 }
15900# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015901 if (check_connection() == FAIL)
15902 return;
15903
15904 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015905 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015906# endif
15907
15908 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15909 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015910 char_u *retvar;
15911
Bram Moolenaar33570922005-01-25 22:26:29 +000015912 v.di_tv.v_type = VAR_STRING;
15913 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015914 retvar = get_tv_string_chk(&argvars[1]);
15915 if (retvar != NULL)
15916 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015917 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015918 }
15919#else
15920 rettv->vval.v_number = -1;
15921#endif
15922}
15923
Bram Moolenaar0d660222005-01-07 21:51:51 +000015924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015925f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015926{
15927 char_u *r = NULL;
15928
15929#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015930 char_u *serverid = get_tv_string_chk(&argvars[0]);
15931
15932 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015933 {
15934# ifdef WIN32
15935 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015936 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015937
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010015938 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015939 if (n != 0)
15940 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15941 if (r == NULL)
15942# else
15943 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015944 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015945# endif
15946 EMSG(_("E277: Unable to read a server reply"));
15947 }
15948#endif
15949 rettv->v_type = VAR_STRING;
15950 rettv->vval.v_string = r;
15951}
15952
15953/*
15954 * "remote_send()" function
15955 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015957f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015958{
15959 rettv->v_type = VAR_STRING;
15960 rettv->vval.v_string = NULL;
15961#ifdef FEAT_CLIENTSERVER
15962 remote_common(argvars, rettv, FALSE);
15963#endif
15964}
15965
15966/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015967 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015968 */
15969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015970f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015971{
Bram Moolenaar33570922005-01-25 22:26:29 +000015972 list_T *l;
15973 listitem_T *item, *item2;
15974 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015975 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015976 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015977 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015978 dict_T *d;
15979 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020015980 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015981
Bram Moolenaar8c711452005-01-14 21:53:12 +000015982 if (argvars[0].v_type == VAR_DICT)
15983 {
15984 if (argvars[2].v_type != VAR_UNKNOWN)
15985 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015986 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015987 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015988 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015989 key = get_tv_string_chk(&argvars[1]);
15990 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015991 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015992 di = dict_find(d, key, -1);
15993 if (di == NULL)
15994 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020015995 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
15996 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015997 {
15998 *rettv = di->di_tv;
15999 init_tv(&di->di_tv);
16000 dictitem_remove(d, di);
16001 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016002 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016003 }
16004 }
16005 else if (argvars[0].v_type != VAR_LIST)
16006 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016007 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016008 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016009 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016010 int error = FALSE;
16011
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016012 idx = (long)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016013 if (error)
16014 ; /* type error: do nothing, errmsg already given */
16015 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016016 EMSGN(_(e_listidx), idx);
16017 else
16018 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016019 if (argvars[2].v_type == VAR_UNKNOWN)
16020 {
16021 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016022 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016023 *rettv = item->li_tv;
16024 vim_free(item);
16025 }
16026 else
16027 {
16028 /* Remove range of items, return list with values. */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016029 end = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016030 if (error)
16031 ; /* type error: do nothing */
16032 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016033 EMSGN(_(e_listidx), end);
16034 else
16035 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016036 int cnt = 0;
16037
16038 for (li = item; li != NULL; li = li->li_next)
16039 {
16040 ++cnt;
16041 if (li == item2)
16042 break;
16043 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016044 if (li == NULL) /* didn't find "item2" after "item" */
16045 EMSG(_(e_invrange));
16046 else
16047 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016048 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016049 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016050 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016051 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016052 l->lv_first = item;
16053 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016054 item->li_prev = NULL;
16055 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016056 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016057 }
16058 }
16059 }
16060 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016061 }
16062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016063}
16064
16065/*
16066 * "rename({from}, {to})" function
16067 */
16068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016069f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016070{
16071 char_u buf[NUMBUFLEN];
16072
16073 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016074 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016076 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16077 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016078}
16079
16080/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016081 * "repeat()" function
16082 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016084f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016085{
16086 char_u *p;
16087 int n;
16088 int slen;
16089 int len;
16090 char_u *r;
16091 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016092
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016093 n = (int)get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016094 if (argvars[0].v_type == VAR_LIST)
16095 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016096 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016097 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016098 if (list_extend(rettv->vval.v_list,
16099 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016100 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016101 }
16102 else
16103 {
16104 p = get_tv_string(&argvars[0]);
16105 rettv->v_type = VAR_STRING;
16106 rettv->vval.v_string = NULL;
16107
16108 slen = (int)STRLEN(p);
16109 len = slen * n;
16110 if (len <= 0)
16111 return;
16112
16113 r = alloc(len + 1);
16114 if (r != NULL)
16115 {
16116 for (i = 0; i < n; i++)
16117 mch_memmove(r + i * slen, p, (size_t)slen);
16118 r[len] = NUL;
16119 }
16120
16121 rettv->vval.v_string = r;
16122 }
16123}
16124
16125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016126 * "resolve()" function
16127 */
16128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016129f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016130{
16131 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016132#ifdef HAVE_READLINK
16133 char_u *buf = NULL;
16134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016135
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016136 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016137#ifdef FEAT_SHORTCUT
16138 {
16139 char_u *v = NULL;
16140
16141 v = mch_resolve_shortcut(p);
16142 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016143 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016144 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016145 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016146 }
16147#else
16148# ifdef HAVE_READLINK
16149 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016150 char_u *cpy;
16151 int len;
16152 char_u *remain = NULL;
16153 char_u *q;
16154 int is_relative_to_current = FALSE;
16155 int has_trailing_pathsep = FALSE;
16156 int limit = 100;
16157
16158 p = vim_strsave(p);
16159
16160 if (p[0] == '.' && (vim_ispathsep(p[1])
16161 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16162 is_relative_to_current = TRUE;
16163
16164 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016165 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016167 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016168 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170
16171 q = getnextcomp(p);
16172 if (*q != NUL)
16173 {
16174 /* Separate the first path component in "p", and keep the
16175 * remainder (beginning with the path separator). */
16176 remain = vim_strsave(q - 1);
16177 q[-1] = NUL;
16178 }
16179
Bram Moolenaard9462e32011-04-11 21:35:11 +020016180 buf = alloc(MAXPATHL + 1);
16181 if (buf == NULL)
16182 goto fail;
16183
Bram Moolenaar071d4272004-06-13 20:20:40 +000016184 for (;;)
16185 {
16186 for (;;)
16187 {
16188 len = readlink((char *)p, (char *)buf, MAXPATHL);
16189 if (len <= 0)
16190 break;
16191 buf[len] = NUL;
16192
16193 if (limit-- == 0)
16194 {
16195 vim_free(p);
16196 vim_free(remain);
16197 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016198 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199 goto fail;
16200 }
16201
16202 /* Ensure that the result will have a trailing path separator
16203 * if the argument has one. */
16204 if (remain == NULL && has_trailing_pathsep)
16205 add_pathsep(buf);
16206
16207 /* Separate the first path component in the link value and
16208 * concatenate the remainders. */
16209 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16210 if (*q != NUL)
16211 {
16212 if (remain == NULL)
16213 remain = vim_strsave(q - 1);
16214 else
16215 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016216 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016217 if (cpy != NULL)
16218 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016219 vim_free(remain);
16220 remain = cpy;
16221 }
16222 }
16223 q[-1] = NUL;
16224 }
16225
16226 q = gettail(p);
16227 if (q > p && *q == NUL)
16228 {
16229 /* Ignore trailing path separator. */
16230 q[-1] = NUL;
16231 q = gettail(p);
16232 }
16233 if (q > p && !mch_isFullName(buf))
16234 {
16235 /* symlink is relative to directory of argument */
16236 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16237 if (cpy != NULL)
16238 {
16239 STRCPY(cpy, p);
16240 STRCPY(gettail(cpy), buf);
16241 vim_free(p);
16242 p = cpy;
16243 }
16244 }
16245 else
16246 {
16247 vim_free(p);
16248 p = vim_strsave(buf);
16249 }
16250 }
16251
16252 if (remain == NULL)
16253 break;
16254
16255 /* Append the first path component of "remain" to "p". */
16256 q = getnextcomp(remain + 1);
16257 len = q - remain - (*q != NUL);
16258 cpy = vim_strnsave(p, STRLEN(p) + len);
16259 if (cpy != NULL)
16260 {
16261 STRNCAT(cpy, remain, len);
16262 vim_free(p);
16263 p = cpy;
16264 }
16265 /* Shorten "remain". */
16266 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016267 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268 else
16269 {
16270 vim_free(remain);
16271 remain = NULL;
16272 }
16273 }
16274
16275 /* If the result is a relative path name, make it explicitly relative to
16276 * the current directory if and only if the argument had this form. */
16277 if (!vim_ispathsep(*p))
16278 {
16279 if (is_relative_to_current
16280 && *p != NUL
16281 && !(p[0] == '.'
16282 && (p[1] == NUL
16283 || vim_ispathsep(p[1])
16284 || (p[1] == '.'
16285 && (p[2] == NUL
16286 || vim_ispathsep(p[2]))))))
16287 {
16288 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016289 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016290 if (cpy != NULL)
16291 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016292 vim_free(p);
16293 p = cpy;
16294 }
16295 }
16296 else if (!is_relative_to_current)
16297 {
16298 /* Strip leading "./". */
16299 q = p;
16300 while (q[0] == '.' && vim_ispathsep(q[1]))
16301 q += 2;
16302 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016303 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016304 }
16305 }
16306
16307 /* Ensure that the result will have no trailing path separator
16308 * if the argument had none. But keep "/" or "//". */
16309 if (!has_trailing_pathsep)
16310 {
16311 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016312 if (after_pathsep(p, q))
16313 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314 }
16315
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016316 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016317 }
16318# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016319 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016320# endif
16321#endif
16322
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016323 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016324
16325#ifdef HAVE_READLINK
16326fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016327 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016328#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016329 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016330}
16331
16332/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016333 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334 */
16335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016336f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337{
Bram Moolenaar33570922005-01-25 22:26:29 +000016338 list_T *l;
16339 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016340
Bram Moolenaar0d660222005-01-07 21:51:51 +000016341 if (argvars[0].v_type != VAR_LIST)
16342 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016343 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016344 && !tv_check_lock(l->lv_lock,
16345 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016346 {
16347 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016348 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016349 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016350 while (li != NULL)
16351 {
16352 ni = li->li_prev;
16353 list_append(l, li);
16354 li = ni;
16355 }
16356 rettv->vval.v_list = l;
16357 rettv->v_type = VAR_LIST;
16358 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016359 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016361}
16362
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016363#define SP_NOMOVE 0x01 /* don't move cursor */
16364#define SP_REPEAT 0x02 /* repeat to find outer pair */
16365#define SP_RETCOUNT 0x04 /* return matchcount */
16366#define SP_SETPCMARK 0x08 /* set previous context mark */
16367#define SP_START 0x10 /* accept match at start position */
16368#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16369#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016370#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016371
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016372static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016373
16374/*
16375 * Get flags for a search function.
16376 * Possibly sets "p_ws".
16377 * Returns BACKWARD, FORWARD or zero (for an error).
16378 */
16379 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016380get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016381{
16382 int dir = FORWARD;
16383 char_u *flags;
16384 char_u nbuf[NUMBUFLEN];
16385 int mask;
16386
16387 if (varp->v_type != VAR_UNKNOWN)
16388 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016389 flags = get_tv_string_buf_chk(varp, nbuf);
16390 if (flags == NULL)
16391 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016392 while (*flags != NUL)
16393 {
16394 switch (*flags)
16395 {
16396 case 'b': dir = BACKWARD; break;
16397 case 'w': p_ws = TRUE; break;
16398 case 'W': p_ws = FALSE; break;
16399 default: mask = 0;
16400 if (flagsp != NULL)
16401 switch (*flags)
16402 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016403 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016404 case 'e': mask = SP_END; break;
16405 case 'm': mask = SP_RETCOUNT; break;
16406 case 'n': mask = SP_NOMOVE; break;
16407 case 'p': mask = SP_SUBPAT; break;
16408 case 'r': mask = SP_REPEAT; break;
16409 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016410 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016411 }
16412 if (mask == 0)
16413 {
16414 EMSG2(_(e_invarg2), flags);
16415 dir = 0;
16416 }
16417 else
16418 *flagsp |= mask;
16419 }
16420 if (dir == 0)
16421 break;
16422 ++flags;
16423 }
16424 }
16425 return dir;
16426}
16427
Bram Moolenaar071d4272004-06-13 20:20:40 +000016428/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016429 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016430 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016431 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016432search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016433{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016434 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016435 char_u *pat;
16436 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016437 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016438 int save_p_ws = p_ws;
16439 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016440 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016441 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016442 proftime_T tm;
16443#ifdef FEAT_RELTIME
16444 long time_limit = 0;
16445#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016446 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016447 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016449 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016450 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016451 if (dir == 0)
16452 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016453 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016454 if (flags & SP_START)
16455 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016456 if (flags & SP_END)
16457 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016458 if (flags & SP_COLUMN)
16459 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016460
Bram Moolenaar76929292008-01-06 19:07:36 +000016461 /* Optional arguments: line number to stop searching and timeout. */
16462 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016463 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016464 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016465 if (lnum_stop < 0)
16466 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016467#ifdef FEAT_RELTIME
16468 if (argvars[3].v_type != VAR_UNKNOWN)
16469 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016470 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
Bram Moolenaar76929292008-01-06 19:07:36 +000016471 if (time_limit < 0)
16472 goto theend;
16473 }
16474#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016475 }
16476
Bram Moolenaar76929292008-01-06 19:07:36 +000016477#ifdef FEAT_RELTIME
16478 /* Set the time limit, if there is one. */
16479 profile_setlimit(time_limit, &tm);
16480#endif
16481
Bram Moolenaar231334e2005-07-25 20:46:57 +000016482 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016483 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016484 * Check to make sure only those flags are set.
16485 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16486 * flags cannot be set. Check for that condition also.
16487 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016488 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016489 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016490 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016491 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016492 goto theend;
16493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016495 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016496 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016497 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016498 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016499 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016500 if (flags & SP_SUBPAT)
16501 retval = subpatnum;
16502 else
16503 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016504 if (flags & SP_SETPCMARK)
16505 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016506 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016507 if (match_pos != NULL)
16508 {
16509 /* Store the match cursor position */
16510 match_pos->lnum = pos.lnum;
16511 match_pos->col = pos.col + 1;
16512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016513 /* "/$" will put the cursor after the end of the line, may need to
16514 * correct that here */
16515 check_cursor();
16516 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016517
16518 /* If 'n' flag is used: restore cursor position. */
16519 if (flags & SP_NOMOVE)
16520 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016521 else
16522 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016523theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016524 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016525
16526 return retval;
16527}
16528
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016529#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016530
16531/*
16532 * round() is not in C90, use ceil() or floor() instead.
16533 */
16534 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010016535vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016536{
16537 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16538}
16539
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016540/*
16541 * "round({float})" function
16542 */
16543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016544f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016545{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016546 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016547
16548 rettv->v_type = VAR_FLOAT;
16549 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016550 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016551 else
16552 rettv->vval.v_float = 0.0;
16553}
16554#endif
16555
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016556/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016557 * "screenattr()" function
16558 */
16559 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010016560f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016561{
16562 int row;
16563 int col;
16564 int c;
16565
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016566 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
16567 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar9a773482013-06-11 18:40:13 +020016568 if (row < 0 || row >= screen_Rows
16569 || col < 0 || col >= screen_Columns)
16570 c = -1;
16571 else
16572 c = ScreenAttrs[LineOffset[row] + col];
16573 rettv->vval.v_number = c;
16574}
16575
16576/*
16577 * "screenchar()" function
16578 */
16579 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010016580f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016581{
16582 int row;
16583 int col;
16584 int off;
16585 int c;
16586
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016587 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
16588 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar9a773482013-06-11 18:40:13 +020016589 if (row < 0 || row >= screen_Rows
16590 || col < 0 || col >= screen_Columns)
16591 c = -1;
16592 else
16593 {
16594 off = LineOffset[row] + col;
16595#ifdef FEAT_MBYTE
16596 if (enc_utf8 && ScreenLinesUC[off] != 0)
16597 c = ScreenLinesUC[off];
16598 else
16599#endif
16600 c = ScreenLines[off];
16601 }
16602 rettv->vval.v_number = c;
16603}
16604
16605/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016606 * "screencol()" function
16607 *
16608 * First column is 1 to be consistent with virtcol().
16609 */
16610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016611f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016612{
16613 rettv->vval.v_number = screen_screencol() + 1;
16614}
16615
16616/*
16617 * "screenrow()" function
16618 */
16619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016620f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016621{
16622 rettv->vval.v_number = screen_screenrow() + 1;
16623}
16624
16625/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016626 * "search()" function
16627 */
16628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016629f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016630{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016631 int flags = 0;
16632
16633 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016634}
16635
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016637 * "searchdecl()" function
16638 */
16639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016640f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016641{
16642 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016643 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016644 int error = FALSE;
16645 char_u *name;
16646
16647 rettv->vval.v_number = 1; /* default: FAIL */
16648
16649 name = get_tv_string_chk(&argvars[0]);
16650 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016651 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016652 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016653 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016654 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016655 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016656 if (!error && name != NULL)
16657 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016658 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016659}
16660
16661/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016662 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016663 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016664 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016665searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016666{
16667 char_u *spat, *mpat, *epat;
16668 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016669 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670 int dir;
16671 int flags = 0;
16672 char_u nbuf1[NUMBUFLEN];
16673 char_u nbuf2[NUMBUFLEN];
16674 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016675 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016676 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016677 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016678
Bram Moolenaar071d4272004-06-13 20:20:40 +000016679 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016680 spat = get_tv_string_chk(&argvars[0]);
16681 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16682 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16683 if (spat == NULL || mpat == NULL || epat == NULL)
16684 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016685
Bram Moolenaar071d4272004-06-13 20:20:40 +000016686 /* Handle the optional fourth argument: flags */
16687 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016688 if (dir == 0)
16689 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016690
16691 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016692 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16693 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016694 if ((flags & (SP_END | SP_SUBPAT)) != 0
16695 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016696 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016697 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016698 goto theend;
16699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016700
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016701 /* Using 'r' implies 'W', otherwise it doesn't work. */
16702 if (flags & SP_REPEAT)
16703 p_ws = FALSE;
16704
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016705 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016706 if (argvars[3].v_type == VAR_UNKNOWN
16707 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016708 skip = (char_u *)"";
16709 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016710 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016711 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016712 if (argvars[5].v_type != VAR_UNKNOWN)
16713 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016714 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016715 if (lnum_stop < 0)
16716 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016717#ifdef FEAT_RELTIME
16718 if (argvars[6].v_type != VAR_UNKNOWN)
16719 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016720 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
Bram Moolenaar76929292008-01-06 19:07:36 +000016721 if (time_limit < 0)
16722 goto theend;
16723 }
16724#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016725 }
16726 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016727 if (skip == NULL)
16728 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016729
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016730 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016731 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016732
16733theend:
16734 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016735
16736 return retval;
16737}
16738
16739/*
16740 * "searchpair()" function
16741 */
16742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016743f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016744{
16745 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16746}
16747
16748/*
16749 * "searchpairpos()" function
16750 */
16751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016752f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016753{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016754 pos_T match_pos;
16755 int lnum = 0;
16756 int col = 0;
16757
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016758 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016759 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016760
16761 if (searchpair_cmn(argvars, &match_pos) > 0)
16762 {
16763 lnum = match_pos.lnum;
16764 col = match_pos.col;
16765 }
16766
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016767 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16768 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016769}
16770
16771/*
16772 * Search for a start/middle/end thing.
16773 * Used by searchpair(), see its documentation for the details.
16774 * Returns 0 or -1 for no match,
16775 */
16776 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010016777do_searchpair(
16778 char_u *spat, /* start pattern */
16779 char_u *mpat, /* middle pattern */
16780 char_u *epat, /* end pattern */
16781 int dir, /* BACKWARD or FORWARD */
16782 char_u *skip, /* skip expression */
16783 int flags, /* SP_SETPCMARK and other SP_ values */
16784 pos_T *match_pos,
16785 linenr_T lnum_stop, /* stop at this line if not zero */
16786 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016787{
16788 char_u *save_cpo;
16789 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16790 long retval = 0;
16791 pos_T pos;
16792 pos_T firstpos;
16793 pos_T foundpos;
16794 pos_T save_cursor;
16795 pos_T save_pos;
16796 int n;
16797 int r;
16798 int nest = 1;
16799 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016800 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016801 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016802
16803 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16804 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016805 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016806
Bram Moolenaar76929292008-01-06 19:07:36 +000016807#ifdef FEAT_RELTIME
16808 /* Set the time limit, if there is one. */
16809 profile_setlimit(time_limit, &tm);
16810#endif
16811
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016812 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16813 * start/middle/end (pat3, for the top pair). */
16814 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16815 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16816 if (pat2 == NULL || pat3 == NULL)
16817 goto theend;
16818 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16819 if (*mpat == NUL)
16820 STRCPY(pat3, pat2);
16821 else
16822 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16823 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016824 if (flags & SP_START)
16825 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016826
Bram Moolenaar071d4272004-06-13 20:20:40 +000016827 save_cursor = curwin->w_cursor;
16828 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016829 clearpos(&firstpos);
16830 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016831 pat = pat3;
16832 for (;;)
16833 {
16834 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016835 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016836 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16837 /* didn't find it or found the first match again: FAIL */
16838 break;
16839
16840 if (firstpos.lnum == 0)
16841 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016842 if (equalpos(pos, foundpos))
16843 {
16844 /* Found the same position again. Can happen with a pattern that
16845 * has "\zs" at the end and searching backwards. Advance one
16846 * character and try again. */
16847 if (dir == BACKWARD)
16848 decl(&pos);
16849 else
16850 incl(&pos);
16851 }
16852 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016853
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016854 /* clear the start flag to avoid getting stuck here */
16855 options &= ~SEARCH_START;
16856
Bram Moolenaar071d4272004-06-13 20:20:40 +000016857 /* If the skip pattern matches, ignore this match. */
16858 if (*skip != NUL)
16859 {
16860 save_pos = curwin->w_cursor;
16861 curwin->w_cursor = pos;
16862 r = eval_to_bool(skip, &err, NULL, FALSE);
16863 curwin->w_cursor = save_pos;
16864 if (err)
16865 {
16866 /* Evaluating {skip} caused an error, break here. */
16867 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016868 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016869 break;
16870 }
16871 if (r)
16872 continue;
16873 }
16874
16875 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16876 {
16877 /* Found end when searching backwards or start when searching
16878 * forward: nested pair. */
16879 ++nest;
16880 pat = pat2; /* nested, don't search for middle */
16881 }
16882 else
16883 {
16884 /* Found end when searching forward or start when searching
16885 * backward: end of (nested) pair; or found middle in outer pair. */
16886 if (--nest == 1)
16887 pat = pat3; /* outer level, search for middle */
16888 }
16889
16890 if (nest == 0)
16891 {
16892 /* Found the match: return matchcount or line number. */
16893 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016894 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016895 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016896 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016897 if (flags & SP_SETPCMARK)
16898 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016899 curwin->w_cursor = pos;
16900 if (!(flags & SP_REPEAT))
16901 break;
16902 nest = 1; /* search for next unmatched */
16903 }
16904 }
16905
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016906 if (match_pos != NULL)
16907 {
16908 /* Store the match cursor position */
16909 match_pos->lnum = curwin->w_cursor.lnum;
16910 match_pos->col = curwin->w_cursor.col + 1;
16911 }
16912
Bram Moolenaar071d4272004-06-13 20:20:40 +000016913 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016914 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016915 curwin->w_cursor = save_cursor;
16916
16917theend:
16918 vim_free(pat2);
16919 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016920 if (p_cpo == empty_option)
16921 p_cpo = save_cpo;
16922 else
16923 /* Darn, evaluating the {skip} expression changed the value. */
16924 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016925
16926 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927}
16928
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016929/*
16930 * "searchpos()" function
16931 */
16932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016933f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016934{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016935 pos_T match_pos;
16936 int lnum = 0;
16937 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016938 int n;
16939 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016940
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016941 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016942 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016943
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016944 n = search_cmn(argvars, &match_pos, &flags);
16945 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016946 {
16947 lnum = match_pos.lnum;
16948 col = match_pos.col;
16949 }
16950
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016951 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16952 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016953 if (flags & SP_SUBPAT)
16954 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016955}
16956
Bram Moolenaar0d660222005-01-07 21:51:51 +000016957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016958f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016959{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016960#ifdef FEAT_CLIENTSERVER
16961 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016962 char_u *server = get_tv_string_chk(&argvars[0]);
16963 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016964
Bram Moolenaar0d660222005-01-07 21:51:51 +000016965 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016966 if (server == NULL || reply == NULL)
16967 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016968 if (check_restricted() || check_secure())
16969 return;
16970# ifdef FEAT_X11
16971 if (check_connection() == FAIL)
16972 return;
16973# endif
16974
16975 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016977 EMSG(_("E258: Unable to send to client"));
16978 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016980 rettv->vval.v_number = 0;
16981#else
16982 rettv->vval.v_number = -1;
16983#endif
16984}
16985
Bram Moolenaar0d660222005-01-07 21:51:51 +000016986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016987f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016988{
16989 char_u *r = NULL;
16990
16991#ifdef FEAT_CLIENTSERVER
16992# ifdef WIN32
16993 r = serverGetVimNames();
16994# else
16995 make_connection();
16996 if (X_DISPLAY != NULL)
16997 r = serverGetVimNames(X_DISPLAY);
16998# endif
16999#endif
17000 rettv->v_type = VAR_STRING;
17001 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002}
17003
17004/*
17005 * "setbufvar()" function
17006 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017008f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017009{
17010 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017011 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017012 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017013 char_u nbuf[NUMBUFLEN];
17014
17015 if (check_restricted() || check_secure())
17016 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017017 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17018 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017019 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020 varp = &argvars[2];
17021
17022 if (buf != NULL && varname != NULL && varp != NULL)
17023 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 if (*varname == '&')
17025 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017026 long numval;
17027 char_u *strval;
17028 int error = FALSE;
Bram Moolenaar93431df2016-07-15 20:14:44 +020017029 aco_save_T aco;
17030
17031 /* set curbuf to be our buf, temporarily */
17032 aucmd_prepbuf(&aco, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017033
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034 ++varname;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017035 numval = (long)get_tv_number_chk(varp, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017036 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017037 if (!error && strval != NULL)
17038 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar93431df2016-07-15 20:14:44 +020017039
17040 /* reset notion of buffer */
17041 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042 }
17043 else
17044 {
Bram Moolenaar93431df2016-07-15 20:14:44 +020017045 buf_T *save_curbuf = curbuf;
17046
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17048 if (bufvarname != NULL)
17049 {
Bram Moolenaar93431df2016-07-15 20:14:44 +020017050 curbuf = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017051 STRCPY(bufvarname, "b:");
17052 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017053 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017054 vim_free(bufvarname);
Bram Moolenaar93431df2016-07-15 20:14:44 +020017055 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017056 }
17057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059}
17060
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017062f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017063{
17064 dict_T *d;
17065 dictitem_T *di;
17066 char_u *csearch;
17067
17068 if (argvars[0].v_type != VAR_DICT)
17069 {
17070 EMSG(_(e_dictreq));
17071 return;
17072 }
17073
17074 if ((d = argvars[0].vval.v_dict) != NULL)
17075 {
17076 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17077 if (csearch != NULL)
17078 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017079#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017080 if (enc_utf8)
17081 {
17082 int pcc[MAX_MCO];
17083 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017084
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017085 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17086 }
17087 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017088#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017089 set_last_csearch(PTR2CHAR(csearch),
17090 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017091 }
17092
17093 di = dict_find(d, (char_u *)"forward", -1);
17094 if (di != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017095 set_csearch_direction((int)get_tv_number(&di->di_tv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017096 ? FORWARD : BACKWARD);
17097
17098 di = dict_find(d, (char_u *)"until", -1);
17099 if (di != NULL)
17100 set_csearch_until(!!get_tv_number(&di->di_tv));
17101 }
17102}
17103
Bram Moolenaar071d4272004-06-13 20:20:40 +000017104/*
17105 * "setcmdpos()" function
17106 */
17107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017108f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017110 int pos = (int)get_tv_number(&argvars[0]) - 1;
17111
17112 if (pos >= 0)
17113 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017114}
17115
17116/*
Bram Moolenaar80492532016-03-08 17:08:53 +010017117 * "setfperm({fname}, {mode})" function
17118 */
17119 static void
17120f_setfperm(typval_T *argvars, typval_T *rettv)
17121{
17122 char_u *fname;
17123 char_u modebuf[NUMBUFLEN];
17124 char_u *mode_str;
17125 int i;
17126 int mask;
17127 int mode = 0;
17128
17129 rettv->vval.v_number = 0;
17130 fname = get_tv_string_chk(&argvars[0]);
17131 if (fname == NULL)
17132 return;
17133 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
17134 if (mode_str == NULL)
17135 return;
17136 if (STRLEN(mode_str) != 9)
17137 {
17138 EMSG2(_(e_invarg2), mode_str);
17139 return;
17140 }
17141
17142 mask = 1;
17143 for (i = 8; i >= 0; --i)
17144 {
17145 if (mode_str[i] != '-')
17146 mode |= mask;
17147 mask = mask << 1;
17148 }
17149 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
17150}
17151
17152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017153 * "setline()" function
17154 */
17155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017156f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017157{
17158 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017159 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017160 list_T *l = NULL;
17161 listitem_T *li = NULL;
17162 long added = 0;
17163 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017165 lnum = get_tv_lnum(&argvars[0]);
17166 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017168 l = argvars[1].vval.v_list;
17169 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017171 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017172 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017173
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017174 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017175 for (;;)
17176 {
17177 if (l != NULL)
17178 {
17179 /* list argument, get next string */
17180 if (li == NULL)
17181 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017182 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017183 li = li->li_next;
17184 }
17185
17186 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017187 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017188 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017189
17190 /* When coming here from Insert mode, sync undo, so that this can be
17191 * undone separately from what was previously inserted. */
17192 if (u_sync_once == 2)
17193 {
17194 u_sync_once = 1; /* notify that u_sync() was called */
17195 u_sync(TRUE);
17196 }
17197
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017198 if (lnum <= curbuf->b_ml.ml_line_count)
17199 {
17200 /* existing line, replace it */
17201 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17202 {
17203 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017204 if (lnum == curwin->w_cursor.lnum)
17205 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017206 rettv->vval.v_number = 0; /* OK */
17207 }
17208 }
17209 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17210 {
17211 /* lnum is one past the last line, append the line */
17212 ++added;
17213 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17214 rettv->vval.v_number = 0; /* OK */
17215 }
17216
17217 if (l == NULL) /* only one string argument */
17218 break;
17219 ++lnum;
17220 }
17221
17222 if (added > 0)
17223 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224}
17225
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017226static void set_qf_ll_list(win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv);
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017227
Bram Moolenaar071d4272004-06-13 20:20:40 +000017228/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017229 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017230 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017232set_qf_ll_list(
17233 win_T *wp UNUSED,
17234 typval_T *list_arg UNUSED,
17235 typval_T *action_arg UNUSED,
17236 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017237{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017238#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020017239 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017240 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020017241 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017242#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017243
Bram Moolenaar2641f772005-03-25 21:58:17 +000017244 rettv->vval.v_number = -1;
17245
17246#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017247 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017248 EMSG(_(e_listreq));
17249 else
17250 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017251 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017252
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017253 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017254 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017255 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017256 if (act == NULL)
17257 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020017258 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017259 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020017260 else
17261 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017262 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020017263 else if (action_arg->v_type == VAR_UNKNOWN)
17264 action = ' ';
17265 else
17266 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017267
Bram Moolenaard106e5b2016-04-21 19:38:07 +020017268 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010017269 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017270 rettv->vval.v_number = 0;
17271 }
17272#endif
17273}
17274
17275/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017276 * "setloclist()" function
17277 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017279f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017280{
17281 win_T *win;
17282
17283 rettv->vval.v_number = -1;
17284
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017285 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017286 if (win != NULL)
17287 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17288}
17289
17290/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017291 * "setmatches()" function
17292 */
17293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017294f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017295{
17296#ifdef FEAT_SEARCH_EXTRA
17297 list_T *l;
17298 listitem_T *li;
17299 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017300 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017301
17302 rettv->vval.v_number = -1;
17303 if (argvars[0].v_type != VAR_LIST)
17304 {
17305 EMSG(_(e_listreq));
17306 return;
17307 }
17308 if ((l = argvars[0].vval.v_list) != NULL)
17309 {
17310
17311 /* To some extent make sure that we are dealing with a list from
17312 * "getmatches()". */
17313 li = l->lv_first;
17314 while (li != NULL)
17315 {
17316 if (li->li_tv.v_type != VAR_DICT
17317 || (d = li->li_tv.vval.v_dict) == NULL)
17318 {
17319 EMSG(_(e_invarg));
17320 return;
17321 }
17322 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017323 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17324 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017325 && dict_find(d, (char_u *)"priority", -1) != NULL
17326 && dict_find(d, (char_u *)"id", -1) != NULL))
17327 {
17328 EMSG(_(e_invarg));
17329 return;
17330 }
17331 li = li->li_next;
17332 }
17333
17334 clear_matches(curwin);
17335 li = l->lv_first;
17336 while (li != NULL)
17337 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017338 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017339 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017340 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017341 char_u *group;
17342 int priority;
17343 int id;
17344 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017345
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017346 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017347 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17348 {
17349 if (s == NULL)
17350 {
17351 s = list_alloc();
17352 if (s == NULL)
17353 return;
17354 }
17355
17356 /* match from matchaddpos() */
17357 for (i = 1; i < 9; i++)
17358 {
17359 sprintf((char *)buf, (char *)"pos%d", i);
17360 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17361 {
17362 if (di->di_tv.v_type != VAR_LIST)
17363 return;
17364
17365 list_append_tv(s, &di->di_tv);
17366 s->lv_refcount++;
17367 }
17368 else
17369 break;
17370 }
17371 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017372
17373 group = get_dict_string(d, (char_u *)"group", FALSE);
17374 priority = (int)get_dict_number(d, (char_u *)"priority");
17375 id = (int)get_dict_number(d, (char_u *)"id");
17376 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17377 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17378 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017379 if (i == 0)
17380 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017381 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017382 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017383 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017384 }
17385 else
17386 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017387 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017388 list_unref(s);
17389 s = NULL;
17390 }
17391
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017392 li = li->li_next;
17393 }
17394 rettv->vval.v_number = 0;
17395 }
17396#endif
17397}
17398
17399/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017400 * "setpos()" function
17401 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017403f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017404{
17405 pos_T pos;
17406 int fnum;
17407 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017408 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017409
Bram Moolenaar08250432008-02-13 11:42:46 +000017410 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017411 name = get_tv_string_chk(argvars);
17412 if (name != NULL)
17413 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017414 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017415 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017416 if (--pos.col < 0)
17417 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017418 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017419 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017420 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017421 if (fnum == curbuf->b_fnum)
17422 {
17423 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017424 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017425 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017426 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017427 curwin->w_set_curswant = FALSE;
17428 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017429 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017430 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017431 }
17432 else
17433 EMSG(_(e_invarg));
17434 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017435 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17436 {
17437 /* set mark */
17438 if (setmark_pos(name[1], &pos, fnum) == OK)
17439 rettv->vval.v_number = 0;
17440 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017441 else
17442 EMSG(_(e_invarg));
17443 }
17444 }
17445}
17446
17447/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017448 * "setqflist()" function
17449 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017451f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017452{
17453 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17454}
17455
17456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457 * "setreg()" function
17458 */
17459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017460f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017461{
17462 int regname;
17463 char_u *strregname;
17464 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017465 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017466 int append;
17467 char_u yank_type;
17468 long block_len;
17469
17470 block_len = -1;
17471 yank_type = MAUTO;
17472 append = FALSE;
17473
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017474 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017475 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017477 if (strregname == NULL)
17478 return; /* type error; errmsg already given */
17479 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480 if (regname == 0 || regname == '@')
17481 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017483 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017485 stropt = get_tv_string_chk(&argvars[2]);
17486 if (stropt == NULL)
17487 return; /* type error */
17488 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489 switch (*stropt)
17490 {
17491 case 'a': case 'A': /* append */
17492 append = TRUE;
17493 break;
17494 case 'v': case 'c': /* character-wise selection */
17495 yank_type = MCHAR;
17496 break;
17497 case 'V': case 'l': /* line-wise selection */
17498 yank_type = MLINE;
17499 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017500 case 'b': case Ctrl_V: /* block-wise selection */
17501 yank_type = MBLOCK;
17502 if (VIM_ISDIGIT(stropt[1]))
17503 {
17504 ++stropt;
17505 block_len = getdigits(&stropt) - 1;
17506 --stropt;
17507 }
17508 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017509 }
17510 }
17511
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017512 if (argvars[1].v_type == VAR_LIST)
17513 {
17514 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017515 char_u **allocval;
17516 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017517 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017518 char_u **curallocval;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020017519 list_T *ll = argvars[1].vval.v_list;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017520 listitem_T *li;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020017521 int len;
17522
17523 /* If the list is NULL handle like an empty list. */
17524 len = ll == NULL ? 0 : ll->lv_len;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017525
Bram Moolenaar7d647822014-04-05 21:28:56 +020017526 /* First half: use for pointers to result lines; second half: use for
17527 * pointers to allocated copies. */
17528 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017529 if (lstval == NULL)
17530 return;
17531 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017532 allocval = lstval + len + 2;
17533 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017534
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020017535 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017536 li = li->li_next)
17537 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017538 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017539 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017540 goto free_lstval;
17541 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017542 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017543 /* Need to make a copy, next get_tv_string_buf_chk() will
17544 * overwrite the string. */
17545 strval = vim_strsave(buf);
17546 if (strval == NULL)
17547 goto free_lstval;
17548 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017549 }
17550 *curval++ = strval;
17551 }
17552 *curval++ = NULL;
17553
17554 write_reg_contents_lst(regname, lstval, -1,
17555 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017556free_lstval:
17557 while (curallocval > allocval)
17558 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017559 vim_free(lstval);
17560 }
17561 else
17562 {
17563 strval = get_tv_string_chk(&argvars[1]);
17564 if (strval == NULL)
17565 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017566 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017567 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017568 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017569 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017570}
17571
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017572/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017573 * "settabvar()" function
17574 */
17575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017576f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017577{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017578#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017579 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017580 tabpage_T *tp;
17581#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017582 char_u *varname, *tabvarname;
17583 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017584
17585 rettv->vval.v_number = 0;
17586
17587 if (check_restricted() || check_secure())
17588 return;
17589
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017590#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017591 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017592#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017593 varname = get_tv_string_chk(&argvars[1]);
17594 varp = &argvars[2];
17595
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017596 if (varname != NULL && varp != NULL
17597#ifdef FEAT_WINDOWS
17598 && tp != NULL
17599#endif
17600 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017601 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017602#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017603 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017604 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017605#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017606
17607 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17608 if (tabvarname != NULL)
17609 {
17610 STRCPY(tabvarname, "t:");
17611 STRCPY(tabvarname + 2, varname);
17612 set_var(tabvarname, varp, TRUE);
17613 vim_free(tabvarname);
17614 }
17615
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017616#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017617 /* Restore current tabpage */
17618 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017619 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017620#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017621 }
17622}
17623
17624/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017625 * "settabwinvar()" function
17626 */
17627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017628f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017629{
17630 setwinvar(argvars, rettv, 1);
17631}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017632
17633/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017634 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017637f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017639 setwinvar(argvars, rettv, 0);
17640}
17641
17642/*
17643 * "setwinvar()" and "settabwinvar()" functions
17644 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017645
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017647setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017648{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017649 win_T *win;
17650#ifdef FEAT_WINDOWS
17651 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017652 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017653 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654#endif
17655 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017656 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017658 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659
17660 if (check_restricted() || check_secure())
17661 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017662
17663#ifdef FEAT_WINDOWS
17664 if (off == 1)
17665 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17666 else
17667 tp = curtab;
17668#endif
17669 win = find_win_by_nr(&argvars[off], tp);
17670 varname = get_tv_string_chk(&argvars[off + 1]);
17671 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672
17673 if (win != NULL && varname != NULL && varp != NULL)
17674 {
17675#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017676 need_switch_win = !(tp == curtab && win == curwin);
17677 if (!need_switch_win
17678 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017681 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017683 long numval;
17684 char_u *strval;
17685 int error = FALSE;
17686
17687 ++varname;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017688 numval = (long)get_tv_number_chk(varp, &error);
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017689 strval = get_tv_string_buf_chk(varp, nbuf);
17690 if (!error && strval != NULL)
17691 set_option_value(varname, numval, strval, OPT_LOCAL);
17692 }
17693 else
17694 {
17695 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17696 if (winvarname != NULL)
17697 {
17698 STRCPY(winvarname, "w:");
17699 STRCPY(winvarname + 2, varname);
17700 set_var(winvarname, varp, TRUE);
17701 vim_free(winvarname);
17702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703 }
17704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017706 if (need_switch_win)
17707 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708#endif
17709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710}
17711
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017712#ifdef FEAT_CRYPT
17713/*
17714 * "sha256({string})" function
17715 */
17716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017717f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017718{
17719 char_u *p;
17720
17721 p = get_tv_string(&argvars[0]);
17722 rettv->vval.v_string = vim_strsave(
17723 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17724 rettv->v_type = VAR_STRING;
17725}
17726#endif /* FEAT_CRYPT */
17727
Bram Moolenaar071d4272004-06-13 20:20:40 +000017728/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017729 * "shellescape({string})" function
17730 */
17731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017732f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017733{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017734 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017735 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017736 rettv->v_type = VAR_STRING;
17737}
17738
17739/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017740 * shiftwidth() function
17741 */
17742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017743f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017744{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017745 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017746}
17747
17748/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017749 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750 */
17751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017752f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017753{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017754 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017755
Bram Moolenaar0d660222005-01-07 21:51:51 +000017756 p = get_tv_string(&argvars[0]);
17757 rettv->vval.v_string = vim_strsave(p);
17758 simplify_filename(rettv->vval.v_string); /* simplify in place */
17759 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760}
17761
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017762#ifdef FEAT_FLOAT
17763/*
17764 * "sin()" function
17765 */
17766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017767f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017768{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017769 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017770
17771 rettv->v_type = VAR_FLOAT;
17772 if (get_float_arg(argvars, &f) == OK)
17773 rettv->vval.v_float = sin(f);
17774 else
17775 rettv->vval.v_float = 0.0;
17776}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017777
17778/*
17779 * "sinh()" function
17780 */
17781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017782f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017783{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017784 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017785
17786 rettv->v_type = VAR_FLOAT;
17787 if (get_float_arg(argvars, &f) == OK)
17788 rettv->vval.v_float = sinh(f);
17789 else
17790 rettv->vval.v_float = 0.0;
17791}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017792#endif
17793
Bram Moolenaar0d660222005-01-07 21:51:51 +000017794static int
17795#ifdef __BORLANDC__
17796 _RTLENTRYF
17797#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017798 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017799static int
17800#ifdef __BORLANDC__
17801 _RTLENTRYF
17802#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017803 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017804
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017805/* struct used in the array that's given to qsort() */
17806typedef struct
17807{
17808 listitem_T *item;
17809 int idx;
17810} sortItem_T;
17811
Bram Moolenaar0b962472016-02-22 22:51:33 +010017812/* struct storing information about current sort */
17813typedef struct
17814{
17815 int item_compare_ic;
17816 int item_compare_numeric;
17817 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017818#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010017819 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017820#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010017821 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010017822 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010017823 dict_T *item_compare_selfdict;
17824 int item_compare_func_err;
17825 int item_compare_keep_zero;
17826} sortinfo_T;
17827static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017828static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017829#define ITEM_COMPARE_FAIL 999
17830
Bram Moolenaar071d4272004-06-13 20:20:40 +000017831/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017832 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017833 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017834 static int
17835#ifdef __BORLANDC__
17836_RTLENTRYF
17837#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010017838item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017839{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017840 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017841 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017842 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017843 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017844 int res;
17845 char_u numbuf1[NUMBUFLEN];
17846 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017847
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017848 si1 = (sortItem_T *)s1;
17849 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017850 tv1 = &si1->item->li_tv;
17851 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017852
Bram Moolenaar0b962472016-02-22 22:51:33 +010017853 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017854 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017855 varnumber_T v1 = get_tv_number(tv1);
17856 varnumber_T v2 = get_tv_number(tv2);
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017857
17858 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17859 }
17860
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017861#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010017862 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017863 {
17864 float_T v1 = get_tv_float(tv1);
17865 float_T v2 = get_tv_float(tv2);
17866
17867 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17868 }
17869#endif
17870
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017871 /* tv2string() puts quotes around a string and allocates memory. Don't do
17872 * that for string variables. Use a single quote when comparing with a
17873 * non-string to do what the docs promise. */
17874 if (tv1->v_type == VAR_STRING)
17875 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010017876 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017877 p1 = (char_u *)"'";
17878 else
17879 p1 = tv1->vval.v_string;
17880 }
17881 else
17882 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17883 if (tv2->v_type == VAR_STRING)
17884 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010017885 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017886 p2 = (char_u *)"'";
17887 else
17888 p2 = tv2->vval.v_string;
17889 }
17890 else
17891 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017892 if (p1 == NULL)
17893 p1 = (char_u *)"";
17894 if (p2 == NULL)
17895 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010017896 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020017897 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010017898 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020017899 res = STRICMP(p1, p2);
17900 else
17901 res = STRCMP(p1, p2);
17902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017903 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017904 {
17905 double n1, n2;
17906 n1 = strtod((char *)p1, (char **)&p1);
17907 n2 = strtod((char *)p2, (char **)&p2);
17908 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17909 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017910
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017911 /* When the result would be zero, compare the item indexes. Makes the
17912 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010017913 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017914 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017915
Bram Moolenaar0d660222005-01-07 21:51:51 +000017916 vim_free(tofree1);
17917 vim_free(tofree2);
17918 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919}
17920
17921 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017922#ifdef __BORLANDC__
17923_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017924#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010017925item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017926{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017927 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017928 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017929 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017930 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017931 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010017932 char_u *func_name;
17933 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017935 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010017936 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017937 return 0;
17938
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017939 si1 = (sortItem_T *)s1;
17940 si2 = (sortItem_T *)s2;
17941
Bram Moolenaar1735bc92016-03-14 23:05:14 +010017942 if (partial == NULL)
17943 func_name = sortinfo->item_compare_func;
17944 else
17945 func_name = partial->pt_name;
17946
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017947 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017948 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017949 copy_tv(&si1->item->li_tv, &argv[0]);
17950 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017951
17952 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010017953 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017954 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010017955 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017956 clear_tv(&argv[0]);
17957 clear_tv(&argv[1]);
17958
17959 if (res == FAIL)
17960 res = ITEM_COMPARE_FAIL;
17961 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017962 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar0b962472016-02-22 22:51:33 +010017963 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017964 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017965 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017966
17967 /* When the result would be zero, compare the pointers themselves. Makes
17968 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010017969 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017970 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017971
Bram Moolenaar0d660222005-01-07 21:51:51 +000017972 return res;
17973}
17974
17975/*
17976 * "sort({list})" function
17977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017979do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017980{
Bram Moolenaar33570922005-01-25 22:26:29 +000017981 list_T *l;
17982 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017983 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010017984 sortinfo_T *old_sortinfo;
17985 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017986 long len;
17987 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017988
Bram Moolenaar0b962472016-02-22 22:51:33 +010017989 /* Pointer to current info struct used in compare function. Save and
17990 * restore the current one for nested calls. */
17991 old_sortinfo = sortinfo;
17992 sortinfo = &info;
17993
Bram Moolenaar0d660222005-01-07 21:51:51 +000017994 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017995 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996 else
17997 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017998 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017999 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018000 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18001 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018002 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018003 rettv->vval.v_list = l;
18004 rettv->v_type = VAR_LIST;
18005 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018006
Bram Moolenaar0d660222005-01-07 21:51:51 +000018007 len = list_len(l);
18008 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018009 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010
Bram Moolenaar0b962472016-02-22 22:51:33 +010018011 info.item_compare_ic = FALSE;
18012 info.item_compare_numeric = FALSE;
18013 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018014#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018015 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018016#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018017 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018018 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018019 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018020 if (argvars[1].v_type != VAR_UNKNOWN)
18021 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018022 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018023 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018024 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018025 else if (argvars[1].v_type == VAR_PARTIAL)
18026 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018027 else
18028 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018029 int error = FALSE;
18030
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018031 i = (long)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018032 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018033 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018034 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018035 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010018036 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018037 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010018038 else if (i != 0)
18039 {
18040 EMSG(_(e_invarg));
18041 goto theend;
18042 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018043 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018044 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010018045 if (*info.item_compare_func == NUL)
18046 {
18047 /* empty string means default sort */
18048 info.item_compare_func = NULL;
18049 }
18050 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018051 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018052 info.item_compare_func = NULL;
18053 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018054 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018055 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018056 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018057 info.item_compare_func = NULL;
18058 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018059 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018060#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018061 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018062 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018063 info.item_compare_func = NULL;
18064 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018065 }
18066#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018067 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018068 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018069 info.item_compare_func = NULL;
18070 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018071 }
18072 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018073 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018074
18075 if (argvars[2].v_type != VAR_UNKNOWN)
18076 {
18077 /* optional third argument: {dict} */
18078 if (argvars[2].v_type != VAR_DICT)
18079 {
18080 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010018081 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018082 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018083 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018084 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086
Bram Moolenaar0d660222005-01-07 21:51:51 +000018087 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018088 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018089 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018090 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018091
Bram Moolenaar327aa022014-03-25 18:24:23 +010018092 i = 0;
18093 if (sort)
18094 {
18095 /* sort(): ptrs will be the list to sort */
18096 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018097 {
18098 ptrs[i].item = li;
18099 ptrs[i].idx = i;
18100 ++i;
18101 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018102
Bram Moolenaar0b962472016-02-22 22:51:33 +010018103 info.item_compare_func_err = FALSE;
18104 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018105 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018106 if ((info.item_compare_func != NULL
18107 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018108 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018109 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018110 EMSG(_("E702: Sort compare function failed"));
18111 else
18112 {
18113 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018114 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010018115 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018116 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010018117 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018118
Bram Moolenaar0b962472016-02-22 22:51:33 +010018119 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018120 {
18121 /* Clear the List and append the items in sorted order. */
18122 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18123 l->lv_len = 0;
18124 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018125 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018126 }
18127 }
18128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018129 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018130 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018131 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018132
18133 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018134 info.item_compare_func_err = FALSE;
18135 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018136 item_compare_func_ptr = info.item_compare_func != NULL
18137 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010018138 ? item_compare2 : item_compare;
18139
18140 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18141 li = li->li_next)
18142 {
18143 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18144 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018145 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018146 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018147 {
18148 EMSG(_("E882: Uniq compare function failed"));
18149 break;
18150 }
18151 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018152
Bram Moolenaar0b962472016-02-22 22:51:33 +010018153 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018154 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018155 while (--i >= 0)
18156 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018157 li = ptrs[i].item->li_next;
18158 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018159 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018160 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018161 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018162 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018163 list_fix_watch(l, li);
18164 listitem_free(li);
18165 l->lv_len--;
18166 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018167 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018168 }
18169
18170 vim_free(ptrs);
18171 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018172theend:
18173 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018174}
18175
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018176/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018177 * "sort({list})" function
18178 */
18179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018180f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018181{
18182 do_sort_uniq(argvars, rettv, TRUE);
18183}
18184
18185/*
18186 * "uniq({list})" function
18187 */
18188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018189f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018190{
18191 do_sort_uniq(argvars, rettv, FALSE);
18192}
18193
18194/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018195 * "soundfold({word})" function
18196 */
18197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018198f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018199{
18200 char_u *s;
18201
18202 rettv->v_type = VAR_STRING;
18203 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018204#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018205 rettv->vval.v_string = eval_soundfold(s);
18206#else
18207 rettv->vval.v_string = vim_strsave(s);
18208#endif
18209}
18210
18211/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018212 * "spellbadword()" function
18213 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018215f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018216{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018217 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018218 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018219 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018220
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018221 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018222 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018223
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018224#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018225 if (argvars[0].v_type == VAR_UNKNOWN)
18226 {
18227 /* Find the start and length of the badly spelled word. */
18228 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18229 if (len != 0)
18230 word = ml_get_cursor();
18231 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018232 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018233 {
18234 char_u *str = get_tv_string_chk(&argvars[0]);
18235 int capcol = -1;
18236
18237 if (str != NULL)
18238 {
18239 /* Check the argument for spelling. */
18240 while (*str != NUL)
18241 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018242 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018243 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018244 {
18245 word = str;
18246 break;
18247 }
18248 str += len;
18249 }
18250 }
18251 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018252#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018253
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018254 list_append_string(rettv->vval.v_list, word, len);
18255 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018256 attr == HLF_SPB ? "bad" :
18257 attr == HLF_SPR ? "rare" :
18258 attr == HLF_SPL ? "local" :
18259 attr == HLF_SPC ? "caps" :
18260 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018261}
18262
18263/*
18264 * "spellsuggest()" function
18265 */
18266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018267f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018268{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018269#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018270 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018271 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018272 int maxcount;
18273 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018274 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018275 listitem_T *li;
18276 int need_capital = FALSE;
18277#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018278
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018279 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018280 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018281
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018282#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018283 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018284 {
18285 str = get_tv_string(&argvars[0]);
18286 if (argvars[1].v_type != VAR_UNKNOWN)
18287 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018288 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018289 if (maxcount <= 0)
18290 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018291 if (argvars[2].v_type != VAR_UNKNOWN)
18292 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018293 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018294 if (typeerr)
18295 return;
18296 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018297 }
18298 else
18299 maxcount = 25;
18300
Bram Moolenaar4770d092006-01-12 23:22:24 +000018301 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018302
18303 for (i = 0; i < ga.ga_len; ++i)
18304 {
18305 str = ((char_u **)ga.ga_data)[i];
18306
18307 li = listitem_alloc();
18308 if (li == NULL)
18309 vim_free(str);
18310 else
18311 {
18312 li->li_tv.v_type = VAR_STRING;
18313 li->li_tv.v_lock = 0;
18314 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018315 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018316 }
18317 }
18318 ga_clear(&ga);
18319 }
18320#endif
18321}
18322
Bram Moolenaar0d660222005-01-07 21:51:51 +000018323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018324f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018325{
18326 char_u *str;
18327 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018328 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018329 regmatch_T regmatch;
18330 char_u patbuf[NUMBUFLEN];
18331 char_u *save_cpo;
18332 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018333 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018334 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018335 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018336
18337 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18338 save_cpo = p_cpo;
18339 p_cpo = (char_u *)"";
18340
18341 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018342 if (argvars[1].v_type != VAR_UNKNOWN)
18343 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018344 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18345 if (pat == NULL)
18346 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018347 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018348 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018349 }
18350 if (pat == NULL || *pat == NUL)
18351 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018352
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018353 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018354 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018355 if (typeerr)
18356 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357
Bram Moolenaar0d660222005-01-07 21:51:51 +000018358 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18359 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018360 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018361 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018362 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018363 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018364 if (*str == NUL)
18365 match = FALSE; /* empty item at the end */
18366 else
18367 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018368 if (match)
18369 end = regmatch.startp[0];
18370 else
18371 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018372 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18373 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018374 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018375 if (list_append_string(rettv->vval.v_list, str,
18376 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018377 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018378 }
18379 if (!match)
18380 break;
18381 /* Advance to just after the match. */
18382 if (regmatch.endp[0] > str)
18383 col = 0;
18384 else
18385 {
18386 /* Don't get stuck at the same match. */
18387#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018388 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018389#else
18390 col = 1;
18391#endif
18392 }
18393 str = regmatch.endp[0];
18394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018395
Bram Moolenaar473de612013-06-08 18:19:48 +020018396 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018398
Bram Moolenaar0d660222005-01-07 21:51:51 +000018399 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018400}
18401
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018402#ifdef FEAT_FLOAT
18403/*
18404 * "sqrt()" function
18405 */
18406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018407f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018408{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018409 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018410
18411 rettv->v_type = VAR_FLOAT;
18412 if (get_float_arg(argvars, &f) == OK)
18413 rettv->vval.v_float = sqrt(f);
18414 else
18415 rettv->vval.v_float = 0.0;
18416}
18417
18418/*
18419 * "str2float()" function
18420 */
18421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018422f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018423{
18424 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18425
18426 if (*p == '+')
18427 p = skipwhite(p + 1);
18428 (void)string2float(p, &rettv->vval.v_float);
18429 rettv->v_type = VAR_FLOAT;
18430}
18431#endif
18432
Bram Moolenaar2c932302006-03-18 21:42:09 +000018433/*
18434 * "str2nr()" function
18435 */
18436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018437f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018438{
18439 int base = 10;
18440 char_u *p;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018441 varnumber_T n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018442 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018443
18444 if (argvars[1].v_type != VAR_UNKNOWN)
18445 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018446 base = (int)get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018447 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018448 {
18449 EMSG(_(e_invarg));
18450 return;
18451 }
18452 }
18453
18454 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018455 if (*p == '+')
18456 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018457 switch (base)
18458 {
18459 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18460 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18461 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18462 default: what = 0;
18463 }
18464 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018465 rettv->vval.v_number = n;
18466}
18467
Bram Moolenaar071d4272004-06-13 20:20:40 +000018468#ifdef HAVE_STRFTIME
18469/*
18470 * "strftime({format}[, {time}])" function
18471 */
18472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018473f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018474{
18475 char_u result_buf[256];
18476 struct tm *curtime;
18477 time_t seconds;
18478 char_u *p;
18479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018480 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018481
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018482 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018483 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484 seconds = time(NULL);
18485 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018486 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018487 curtime = localtime(&seconds);
18488 /* MSVC returns NULL for an invalid value of seconds. */
18489 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018490 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018491 else
18492 {
18493# ifdef FEAT_MBYTE
18494 vimconv_T conv;
18495 char_u *enc;
18496
18497 conv.vc_type = CONV_NONE;
18498 enc = enc_locale();
18499 convert_setup(&conv, p_enc, enc);
18500 if (conv.vc_type != CONV_NONE)
18501 p = string_convert(&conv, p, NULL);
18502# endif
18503 if (p != NULL)
18504 (void)strftime((char *)result_buf, sizeof(result_buf),
18505 (char *)p, curtime);
18506 else
18507 result_buf[0] = NUL;
18508
18509# ifdef FEAT_MBYTE
18510 if (conv.vc_type != CONV_NONE)
18511 vim_free(p);
18512 convert_setup(&conv, enc, p_enc);
18513 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018514 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018515 else
18516# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018517 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518
18519# ifdef FEAT_MBYTE
18520 /* Release conversion descriptors */
18521 convert_setup(&conv, NULL, NULL);
18522 vim_free(enc);
18523# endif
18524 }
18525}
18526#endif
18527
18528/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020018529 * "strgetchar()" function
18530 */
18531 static void
18532f_strgetchar(typval_T *argvars, typval_T *rettv)
18533{
18534 char_u *str;
18535 int len;
18536 int error = FALSE;
18537 int charidx;
18538
18539 rettv->vval.v_number = -1;
18540 str = get_tv_string_chk(&argvars[0]);
18541 if (str == NULL)
18542 return;
18543 len = (int)STRLEN(str);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018544 charidx = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020018545 if (error)
18546 return;
18547#ifdef FEAT_MBYTE
18548 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020018549 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020018550
18551 while (charidx >= 0 && byteidx < len)
18552 {
18553 if (charidx == 0)
18554 {
18555 rettv->vval.v_number = mb_ptr2char(str + byteidx);
18556 break;
18557 }
18558 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020018559 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020018560 }
18561 }
18562#else
18563 if (charidx < len)
18564 rettv->vval.v_number = str[charidx];
18565#endif
18566}
18567
18568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569 * "stridx()" function
18570 */
18571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018572f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573{
18574 char_u buf[NUMBUFLEN];
18575 char_u *needle;
18576 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018577 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018578 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018579 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018581 needle = get_tv_string_chk(&argvars[1]);
18582 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018583 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018584 if (needle == NULL || haystack == NULL)
18585 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018586
Bram Moolenaar33570922005-01-25 22:26:29 +000018587 if (argvars[2].v_type != VAR_UNKNOWN)
18588 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018589 int error = FALSE;
18590
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018591 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018592 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018593 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018594 if (start_idx >= 0)
18595 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018596 }
18597
18598 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18599 if (pos != NULL)
18600 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018601}
18602
18603/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018604 * "string()" function
18605 */
18606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018607f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018608{
18609 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018610 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018611
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018612 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010018613 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
18614 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018615 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018616 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018617 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618}
18619
18620/*
18621 * "strlen()" function
18622 */
18623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018624f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018626 rettv->vval.v_number = (varnumber_T)(STRLEN(
18627 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628}
18629
18630/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018631 * "strchars()" function
18632 */
18633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018634f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018635{
18636 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018637 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018638#ifdef FEAT_MBYTE
18639 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018640 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018641#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018642
18643 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018644 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018645 if (skipcc < 0 || skipcc > 1)
18646 EMSG(_(e_invarg));
18647 else
18648 {
18649#ifdef FEAT_MBYTE
18650 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18651 while (*s != NUL)
18652 {
18653 func_mb_ptr2char_adv(&s);
18654 ++len;
18655 }
18656 rettv->vval.v_number = len;
18657#else
18658 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18659#endif
18660 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018661}
18662
18663/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018664 * "strdisplaywidth()" function
18665 */
18666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018667f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020018668{
18669 char_u *s = get_tv_string(&argvars[0]);
18670 int col = 0;
18671
18672 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018673 col = (int)get_tv_number(&argvars[1]);
Bram Moolenaardc536092010-07-18 15:45:49 +020018674
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018675 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018676}
18677
18678/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018679 * "strwidth()" function
18680 */
18681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018682f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018683{
18684 char_u *s = get_tv_string(&argvars[0]);
18685
18686 rettv->vval.v_number = (varnumber_T)(
18687#ifdef FEAT_MBYTE
18688 mb_string2cells(s, -1)
18689#else
18690 STRLEN(s)
18691#endif
18692 );
18693}
18694
18695/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020018696 * "strcharpart()" function
18697 */
18698 static void
18699f_strcharpart(typval_T *argvars, typval_T *rettv)
18700{
18701#ifdef FEAT_MBYTE
18702 char_u *p;
18703 int nchar;
18704 int nbyte = 0;
18705 int charlen;
18706 int len = 0;
18707 int slen;
18708 int error = FALSE;
18709
18710 p = get_tv_string(&argvars[0]);
18711 slen = (int)STRLEN(p);
18712
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018713 nchar = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020018714 if (!error)
18715 {
18716 if (nchar > 0)
18717 while (nchar > 0 && nbyte < slen)
18718 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020018719 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020018720 --nchar;
18721 }
18722 else
18723 nbyte = nchar;
18724 if (argvars[2].v_type != VAR_UNKNOWN)
18725 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018726 charlen = (int)get_tv_number(&argvars[2]);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020018727 while (charlen > 0 && nbyte + len < slen)
18728 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020018729 int off = nbyte + len;
18730
18731 if (off < 0)
18732 len += 1;
18733 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020018734 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020018735 --charlen;
18736 }
18737 }
18738 else
18739 len = slen - nbyte; /* default: all bytes that are available. */
18740 }
18741
18742 /*
18743 * Only return the overlap between the specified part and the actual
18744 * string.
18745 */
18746 if (nbyte < 0)
18747 {
18748 len += nbyte;
18749 nbyte = 0;
18750 }
18751 else if (nbyte > slen)
18752 nbyte = slen;
18753 if (len < 0)
18754 len = 0;
18755 else if (nbyte + len > slen)
18756 len = slen - nbyte;
18757
18758 rettv->v_type = VAR_STRING;
18759 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
18760#else
18761 f_strpart(argvars, rettv);
18762#endif
18763}
18764
18765/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766 * "strpart()" function
18767 */
18768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018769f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018770{
18771 char_u *p;
18772 int n;
18773 int len;
18774 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018775 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018777 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018778 slen = (int)STRLEN(p);
18779
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018780 n = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018781 if (error)
18782 len = 0;
18783 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018784 len = (int)get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785 else
18786 len = slen - n; /* default len: all bytes that are available. */
18787
18788 /*
18789 * Only return the overlap between the specified part and the actual
18790 * string.
18791 */
18792 if (n < 0)
18793 {
18794 len += n;
18795 n = 0;
18796 }
18797 else if (n > slen)
18798 n = slen;
18799 if (len < 0)
18800 len = 0;
18801 else if (n + len > slen)
18802 len = slen - n;
18803
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018804 rettv->v_type = VAR_STRING;
18805 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806}
18807
18808/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018809 * "strridx()" function
18810 */
18811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018812f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018813{
18814 char_u buf[NUMBUFLEN];
18815 char_u *needle;
18816 char_u *haystack;
18817 char_u *rest;
18818 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018819 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018820
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018821 needle = get_tv_string_chk(&argvars[1]);
18822 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018823
18824 rettv->vval.v_number = -1;
18825 if (needle == NULL || haystack == NULL)
18826 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018827
18828 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018829 if (argvars[2].v_type != VAR_UNKNOWN)
18830 {
18831 /* Third argument: upper limit for index */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018832 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018833 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018834 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018835 }
18836 else
18837 end_idx = haystack_len;
18838
Bram Moolenaar0d660222005-01-07 21:51:51 +000018839 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018840 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018841 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018842 lastmatch = haystack + end_idx;
18843 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018844 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018845 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018846 for (rest = haystack; *rest != '\0'; ++rest)
18847 {
18848 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018849 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018850 break;
18851 lastmatch = rest;
18852 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018853 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018854
18855 if (lastmatch == NULL)
18856 rettv->vval.v_number = -1;
18857 else
18858 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18859}
18860
18861/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018862 * "strtrans()" function
18863 */
18864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018865f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018867 rettv->v_type = VAR_STRING;
18868 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018869}
18870
18871/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018872 * "submatch()" function
18873 */
18874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018875f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018876{
Bram Moolenaar41571762014-04-02 19:00:58 +020018877 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018878 int no;
18879 int retList = 0;
18880
18881 no = (int)get_tv_number_chk(&argvars[0], &error);
18882 if (error)
18883 return;
18884 error = FALSE;
18885 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018886 retList = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar41571762014-04-02 19:00:58 +020018887 if (error)
18888 return;
18889
18890 if (retList == 0)
18891 {
18892 rettv->v_type = VAR_STRING;
18893 rettv->vval.v_string = reg_submatch(no);
18894 }
18895 else
18896 {
18897 rettv->v_type = VAR_LIST;
18898 rettv->vval.v_list = reg_submatch_list(no);
18899 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018900}
18901
18902/*
18903 * "substitute()" function
18904 */
18905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018906f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018907{
18908 char_u patbuf[NUMBUFLEN];
18909 char_u subbuf[NUMBUFLEN];
18910 char_u flagsbuf[NUMBUFLEN];
18911
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018912 char_u *str = get_tv_string_chk(&argvars[0]);
18913 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18914 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18915 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18916
Bram Moolenaar0d660222005-01-07 21:51:51 +000018917 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018918 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18919 rettv->vval.v_string = NULL;
18920 else
18921 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018922}
18923
18924/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018925 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018928f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018929{
18930 int id = 0;
18931#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018932 linenr_T lnum;
18933 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018934 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018935 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018936
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018937 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018938 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18939 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018940
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018941 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018942 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018943 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018944#endif
18945
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018946 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018947}
18948
18949/*
18950 * "synIDattr(id, what [, mode])" function
18951 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018953f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954{
18955 char_u *p = NULL;
18956#ifdef FEAT_SYN_HL
18957 int id;
18958 char_u *what;
18959 char_u *mode;
18960 char_u modebuf[NUMBUFLEN];
18961 int modec;
18962
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018963 id = (int)get_tv_number(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018964 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018965 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018966 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018967 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018969 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970 modec = 0; /* replace invalid with current */
18971 }
18972 else
18973 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020018974#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020018975 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018976 modec = 'g';
18977 else
18978#endif
18979 if (t_colors > 1)
18980 modec = 'c';
18981 else
18982 modec = 't';
18983 }
18984
18985
18986 switch (TOLOWER_ASC(what[0]))
18987 {
18988 case 'b':
18989 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18990 p = highlight_color(id, what, modec);
18991 else /* bold */
18992 p = highlight_has_attr(id, HL_BOLD, modec);
18993 break;
18994
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018995 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018996 p = highlight_color(id, what, modec);
18997 break;
18998
18999 case 'i':
19000 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19001 p = highlight_has_attr(id, HL_INVERSE, modec);
19002 else /* italic */
19003 p = highlight_has_attr(id, HL_ITALIC, modec);
19004 break;
19005
19006 case 'n': /* name */
19007 p = get_highlight_name(NULL, id - 1);
19008 break;
19009
19010 case 'r': /* reverse */
19011 p = highlight_has_attr(id, HL_INVERSE, modec);
19012 break;
19013
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019014 case 's':
19015 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19016 p = highlight_color(id, what, modec);
19017 else /* standout */
19018 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019019 break;
19020
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019021 case 'u':
19022 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19023 /* underline */
19024 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19025 else
19026 /* undercurl */
19027 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019028 break;
19029 }
19030
19031 if (p != NULL)
19032 p = vim_strsave(p);
19033#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019034 rettv->v_type = VAR_STRING;
19035 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019036}
19037
19038/*
19039 * "synIDtrans(id)" function
19040 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019042f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019043{
19044 int id;
19045
19046#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019047 id = (int)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048
19049 if (id > 0)
19050 id = syn_get_final_id(id);
19051 else
19052#endif
19053 id = 0;
19054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019055 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019056}
19057
19058/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019059 * "synconcealed(lnum, col)" function
19060 */
19061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019062f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019063{
19064#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019065 linenr_T lnum;
19066 colnr_T col;
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019067 int syntax_flags = 0;
19068 int cchar;
19069 int matchid = 0;
19070 char_u str[NUMBUFLEN];
19071#endif
19072
19073 rettv->v_type = VAR_LIST;
19074 rettv->vval.v_list = NULL;
19075
19076#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19077 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019078 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019079
19080 vim_memset(str, NUL, sizeof(str));
19081
19082 if (rettv_list_alloc(rettv) != FAIL)
19083 {
19084 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19085 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19086 && curwin->w_p_cole > 0)
19087 {
19088 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19089 syntax_flags = get_syntax_info(&matchid);
19090
19091 /* get the conceal character */
19092 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19093 {
19094 cchar = syn_get_sub_char();
19095 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19096 cchar = lcs_conceal;
19097 if (cchar != NUL)
19098 {
19099# ifdef FEAT_MBYTE
19100 if (has_mbyte)
19101 (*mb_char2bytes)(cchar, str);
19102 else
19103# endif
19104 str[0] = cchar;
19105 }
19106 }
19107 }
19108
19109 list_append_number(rettv->vval.v_list,
19110 (syntax_flags & HL_CONCEAL) != 0);
19111 /* -1 to auto-determine strlen */
19112 list_append_string(rettv->vval.v_list, str, -1);
19113 list_append_number(rettv->vval.v_list, matchid);
19114 }
19115#endif
19116}
19117
19118/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019119 * "synstack(lnum, col)" function
19120 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019122f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019123{
19124#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019125 linenr_T lnum;
19126 colnr_T col;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019127 int i;
19128 int id;
19129#endif
19130
19131 rettv->v_type = VAR_LIST;
19132 rettv->vval.v_list = NULL;
19133
19134#ifdef FEAT_SYN_HL
19135 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019136 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019137
19138 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019139 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019140 && rettv_list_alloc(rettv) != FAIL)
19141 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019142 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019143 for (i = 0; ; ++i)
19144 {
19145 id = syn_get_stack_item(i);
19146 if (id < 0)
19147 break;
19148 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19149 break;
19150 }
19151 }
19152#endif
19153}
19154
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019156get_cmd_output_as_rettv(
19157 typval_T *argvars,
19158 typval_T *rettv,
19159 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019161 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019162 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019163 char_u *infile = NULL;
19164 char_u buf[NUMBUFLEN];
19165 int err = FALSE;
19166 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019167 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019168 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019169
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019170 rettv->v_type = VAR_STRING;
19171 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019172 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019173 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019174
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019175 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019176 {
19177 /*
19178 * Write the string to a temp file, to be used for input of the shell
19179 * command.
19180 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019181 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019182 {
19183 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019184 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019185 }
19186
19187 fd = mch_fopen((char *)infile, WRITEBIN);
19188 if (fd == NULL)
19189 {
19190 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019191 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019192 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019193 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019194 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019195 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19196 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019197 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019198 else
19199 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019200 size_t len;
19201
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019202 p = get_tv_string_buf_chk(&argvars[1], buf);
19203 if (p == NULL)
19204 {
19205 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019206 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019207 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019208 len = STRLEN(p);
19209 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019210 err = TRUE;
19211 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019212 if (fclose(fd) != 0)
19213 err = TRUE;
19214 if (err)
19215 {
19216 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019217 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019218 }
19219 }
19220
Bram Moolenaar52a72462014-08-29 15:53:52 +020019221 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19222 * echoes typeahead, that messes up the display. */
19223 if (!msg_silent)
19224 flags += SHELL_COOKED;
19225
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019226 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019227 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019228 int len;
19229 listitem_T *li;
19230 char_u *s = NULL;
19231 char_u *start;
19232 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019233 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019234
Bram Moolenaar52a72462014-08-29 15:53:52 +020019235 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019236 if (res == NULL)
19237 goto errret;
19238
19239 list = list_alloc();
19240 if (list == NULL)
19241 goto errret;
19242
19243 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019244 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019245 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019246 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019247 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019248 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019249
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019250 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019251 if (s == NULL)
19252 goto errret;
19253
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019254 for (p = s; start < end; ++p, ++start)
19255 *p = *start == NUL ? NL : *start;
19256 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019257
19258 li = listitem_alloc();
19259 if (li == NULL)
19260 {
19261 vim_free(s);
19262 goto errret;
19263 }
19264 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019265 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019266 li->li_tv.vval.v_string = s;
19267 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019268 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019269
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019270 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019271 rettv->v_type = VAR_LIST;
19272 rettv->vval.v_list = list;
19273 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019274 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019275 else
19276 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019277 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019278#ifdef USE_CR
19279 /* translate <CR> into <NL> */
19280 if (res != NULL)
19281 {
19282 char_u *s;
19283
19284 for (s = res; *s; ++s)
19285 {
19286 if (*s == CAR)
19287 *s = NL;
19288 }
19289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290#else
19291# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019292 /* translate <CR><NL> into <NL> */
19293 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019294 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019295 char_u *s, *d;
19296
19297 d = res;
19298 for (s = res; *s; ++s)
19299 {
19300 if (s[0] == CAR && s[1] == NL)
19301 ++s;
19302 *d++ = *s;
19303 }
19304 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019306# endif
19307#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019308 rettv->vval.v_string = res;
19309 res = NULL;
19310 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019311
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019312errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019313 if (infile != NULL)
19314 {
19315 mch_remove(infile);
19316 vim_free(infile);
19317 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019318 if (res != NULL)
19319 vim_free(res);
19320 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020019321 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019322}
19323
19324/*
19325 * "system()" function
19326 */
19327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019328f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019329{
19330 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19331}
19332
19333/*
19334 * "systemlist()" function
19335 */
19336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019337f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019338{
19339 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019340}
19341
19342/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019343 * "tabpagebuflist()" function
19344 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019346f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019347{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019348#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019349 tabpage_T *tp;
19350 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019351
19352 if (argvars[0].v_type == VAR_UNKNOWN)
19353 wp = firstwin;
19354 else
19355 {
19356 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19357 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019358 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019359 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019360 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019361 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019362 for (; wp != NULL; wp = wp->w_next)
19363 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019364 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019365 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019366 }
19367#endif
19368}
19369
19370
19371/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019372 * "tabpagenr()" function
19373 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019375f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019376{
19377 int nr = 1;
19378#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019379 char_u *arg;
19380
19381 if (argvars[0].v_type != VAR_UNKNOWN)
19382 {
19383 arg = get_tv_string_chk(&argvars[0]);
19384 nr = 0;
19385 if (arg != NULL)
19386 {
19387 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019388 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019389 else
19390 EMSG2(_(e_invexpr2), arg);
19391 }
19392 }
19393 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019394 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019395#endif
19396 rettv->vval.v_number = nr;
19397}
19398
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019399
19400#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019401static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019402
19403/*
19404 * Common code for tabpagewinnr() and winnr().
19405 */
19406 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019407get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019408{
19409 win_T *twin;
19410 int nr = 1;
19411 win_T *wp;
19412 char_u *arg;
19413
19414 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19415 if (argvar->v_type != VAR_UNKNOWN)
19416 {
19417 arg = get_tv_string_chk(argvar);
19418 if (arg == NULL)
19419 nr = 0; /* type error; errmsg already given */
19420 else if (STRCMP(arg, "$") == 0)
19421 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19422 else if (STRCMP(arg, "#") == 0)
19423 {
19424 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19425 if (twin == NULL)
19426 nr = 0;
19427 }
19428 else
19429 {
19430 EMSG2(_(e_invexpr2), arg);
19431 nr = 0;
19432 }
19433 }
19434
19435 if (nr > 0)
19436 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19437 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019438 {
19439 if (wp == NULL)
19440 {
19441 /* didn't find it in this tabpage */
19442 nr = 0;
19443 break;
19444 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019445 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019446 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019447 return nr;
19448}
19449#endif
19450
19451/*
19452 * "tabpagewinnr()" function
19453 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019455f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019456{
19457 int nr = 1;
19458#ifdef FEAT_WINDOWS
19459 tabpage_T *tp;
19460
19461 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19462 if (tp == NULL)
19463 nr = 0;
19464 else
19465 nr = get_winnr(tp, &argvars[1]);
19466#endif
19467 rettv->vval.v_number = nr;
19468}
19469
19470
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019471/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019472 * "tagfiles()" function
19473 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019475f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019476{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019477 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019478 tagname_T tn;
19479 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019480
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019481 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019482 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019483 fname = alloc(MAXPATHL);
19484 if (fname == NULL)
19485 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019486
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019487 for (first = TRUE; ; first = FALSE)
19488 if (get_tagfname(&tn, first, fname) == FAIL
19489 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019490 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019491 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019492 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019493}
19494
19495/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019496 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019497 */
19498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019499f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019500{
19501 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019502
19503 tag_pattern = get_tv_string(&argvars[0]);
19504
19505 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019506 if (*tag_pattern == NUL)
19507 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019508
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019509 if (rettv_list_alloc(rettv) == OK)
19510 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019511}
19512
19513/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514 * "tempname()" function
19515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019517f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019518{
19519 static int x = 'A';
19520
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019521 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019522 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019523
19524 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19525 * names. Skip 'I' and 'O', they are used for shell redirection. */
19526 do
19527 {
19528 if (x == 'Z')
19529 x = '0';
19530 else if (x == '9')
19531 x = 'A';
19532 else
19533 {
19534#ifdef EBCDIC
19535 if (x == 'I')
19536 x = 'J';
19537 else if (x == 'R')
19538 x = 'S';
19539 else
19540#endif
19541 ++x;
19542 }
19543 } while (x == 'I' || x == 'O');
19544}
19545
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019546#ifdef FEAT_FLOAT
19547/*
19548 * "tan()" function
19549 */
19550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019551f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019552{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019553 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019554
19555 rettv->v_type = VAR_FLOAT;
19556 if (get_float_arg(argvars, &f) == OK)
19557 rettv->vval.v_float = tan(f);
19558 else
19559 rettv->vval.v_float = 0.0;
19560}
19561
19562/*
19563 * "tanh()" function
19564 */
19565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019566f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019567{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019568 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019569
19570 rettv->v_type = VAR_FLOAT;
19571 if (get_float_arg(argvars, &f) == OK)
19572 rettv->vval.v_float = tanh(f);
19573 else
19574 rettv->vval.v_float = 0.0;
19575}
19576#endif
19577
Bram Moolenaar574860b2016-05-24 17:33:34 +020019578/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020019579 * "test_alloc_fail(id, countdown, repeat)" function
19580 */
19581 static void
19582f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
19583{
19584 if (argvars[0].v_type != VAR_NUMBER
19585 || argvars[0].vval.v_number <= 0
19586 || argvars[1].v_type != VAR_NUMBER
19587 || argvars[1].vval.v_number < 0
19588 || argvars[2].v_type != VAR_NUMBER)
19589 EMSG(_(e_invarg));
19590 else
19591 {
19592 alloc_fail_id = argvars[0].vval.v_number;
19593 if (alloc_fail_id >= aid_last)
19594 EMSG(_(e_invarg));
19595 alloc_fail_countdown = argvars[1].vval.v_number;
19596 alloc_fail_repeat = argvars[2].vval.v_number;
19597 did_outofmem_msg = FALSE;
19598 }
19599}
19600
19601/*
Bram Moolenaar5c719942016-07-09 23:40:45 +020019602 * "test_autochdir()"
19603 */
19604 static void
19605f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
19606{
19607#if defined(FEAT_AUTOCHDIR)
19608 test_autochdir = TRUE;
19609#endif
19610}
19611
19612/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020019613 * "test_disable_char_avail({expr})" function
19614 */
19615 static void
19616f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
19617{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019618 disable_char_avail_for_testing = (int)get_tv_number(&argvars[0]);
Bram Moolenaar8e8df252016-05-25 21:23:21 +020019619}
19620
19621/*
Bram Moolenaar574860b2016-05-24 17:33:34 +020019622 * "test_garbagecollect_now()" function
19623 */
19624 static void
19625f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
19626{
19627 /* This is dangerous, any Lists and Dicts used internally may be freed
19628 * while still in use. */
19629 garbage_collect(TRUE);
19630}
19631
19632#ifdef FEAT_JOB_CHANNEL
19633 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020019634f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020019635{
19636 rettv->v_type = VAR_CHANNEL;
19637 rettv->vval.v_channel = NULL;
19638}
19639#endif
19640
19641 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020019642f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020019643{
19644 rettv->v_type = VAR_DICT;
19645 rettv->vval.v_dict = NULL;
19646}
19647
19648#ifdef FEAT_JOB_CHANNEL
19649 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020019650f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020019651{
19652 rettv->v_type = VAR_JOB;
19653 rettv->vval.v_job = NULL;
19654}
19655#endif
19656
19657 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020019658f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020019659{
19660 rettv->v_type = VAR_LIST;
19661 rettv->vval.v_list = NULL;
19662}
19663
19664 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020019665f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020019666{
19667 rettv->v_type = VAR_PARTIAL;
19668 rettv->vval.v_partial = NULL;
19669}
19670
19671 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020019672f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020019673{
19674 rettv->v_type = VAR_STRING;
19675 rettv->vval.v_string = NULL;
19676}
19677
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020019678 static void
19679f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
19680{
19681 time_for_testing = (time_t)get_tv_number(&argvars[0]);
19682}
19683
Bram Moolenaar975b5272016-03-15 23:10:59 +010019684#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
19685/*
19686 * Get a callback from "arg". It can be a Funcref or a function name.
19687 * When "arg" is zero return an empty string.
19688 * Return NULL for an invalid argument.
19689 */
19690 char_u *
19691get_callback(typval_T *arg, partial_T **pp)
19692{
19693 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
19694 {
19695 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010019696 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010019697 return (*pp)->pt_name;
19698 }
19699 *pp = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +020019700 if (arg->v_type == VAR_FUNC)
19701 {
19702 func_ref(arg->vval.v_string);
19703 return arg->vval.v_string;
19704 }
19705 if (arg->v_type == VAR_STRING)
Bram Moolenaar975b5272016-03-15 23:10:59 +010019706 return arg->vval.v_string;
19707 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
19708 return (char_u *)"";
19709 EMSG(_("E921: Invalid callback argument"));
19710 return NULL;
19711}
Bram Moolenaar1436d8d2016-07-11 22:41:15 +020019712
19713/*
19714 * Unref/free "callback" and "partial" retured by get_callback().
19715 */
19716 void
19717free_callback(char_u *callback, partial_T *partial)
19718{
19719 if (partial != NULL)
19720 partial_unref(partial);
19721 else if (callback != NULL)
19722 {
19723 func_unref(callback);
19724 vim_free(callback);
19725 }
19726}
Bram Moolenaar975b5272016-03-15 23:10:59 +010019727#endif
19728
19729#ifdef FEAT_TIMERS
19730/*
19731 * "timer_start(time, callback [, options])" function
19732 */
19733 static void
19734f_timer_start(typval_T *argvars, typval_T *rettv)
19735{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019736 long msec = (long)get_tv_number(&argvars[0]);
Bram Moolenaar975b5272016-03-15 23:10:59 +010019737 timer_T *timer;
19738 int repeat = 0;
19739 char_u *callback;
19740 dict_T *dict;
19741
Bram Moolenaar38499922016-04-22 20:46:52 +020019742 if (check_secure())
19743 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010019744 if (argvars[2].v_type != VAR_UNKNOWN)
19745 {
19746 if (argvars[2].v_type != VAR_DICT
19747 || (dict = argvars[2].vval.v_dict) == NULL)
19748 {
19749 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
19750 return;
19751 }
19752 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
19753 repeat = get_dict_number(dict, (char_u *)"repeat");
19754 }
19755
19756 timer = create_timer(msec, repeat);
19757 callback = get_callback(&argvars[1], &timer->tr_partial);
19758 if (callback == NULL)
19759 {
19760 stop_timer(timer);
19761 rettv->vval.v_number = -1;
19762 }
19763 else
19764 {
19765 timer->tr_callback = vim_strsave(callback);
19766 rettv->vval.v_number = timer->tr_id;
19767 }
19768}
19769
19770/*
19771 * "timer_stop(timer)" function
19772 */
19773 static void
19774f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
19775{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020019776 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010019777
Bram Moolenaare40d75f2016-05-15 18:00:19 +020019778 if (argvars[0].v_type != VAR_NUMBER)
19779 {
Bram Moolenaared59aa62016-07-09 17:41:12 +020019780 EMSG(_(e_number_exp));
19781 return;
Bram Moolenaare40d75f2016-05-15 18:00:19 +020019782 }
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019783 timer = find_timer((int)get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010019784 if (timer != NULL)
19785 stop_timer(timer);
19786}
19787#endif
19788
Bram Moolenaard52d9742005-08-21 22:20:28 +000019789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790 * "tolower(string)" function
19791 */
19792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019793f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019794{
19795 char_u *p;
19796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019797 p = vim_strsave(get_tv_string(&argvars[0]));
19798 rettv->v_type = VAR_STRING;
19799 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800
19801 if (p != NULL)
19802 while (*p != NUL)
19803 {
19804#ifdef FEAT_MBYTE
19805 int l;
19806
19807 if (enc_utf8)
19808 {
19809 int c, lc;
19810
19811 c = utf_ptr2char(p);
19812 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019813 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814 /* TODO: reallocate string when byte count changes. */
19815 if (utf_char2len(lc) == l)
19816 utf_char2bytes(lc, p);
19817 p += l;
19818 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019819 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820 p += l; /* skip multi-byte character */
19821 else
19822#endif
19823 {
19824 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19825 ++p;
19826 }
19827 }
19828}
19829
19830/*
19831 * "toupper(string)" function
19832 */
19833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019834f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019836 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019837 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838}
19839
19840/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019841 * "tr(string, fromstr, tostr)" function
19842 */
19843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019844f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019845{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019846 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019847 char_u *fromstr;
19848 char_u *tostr;
19849 char_u *p;
19850#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019851 int inlen;
19852 int fromlen;
19853 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019854 int idx;
19855 char_u *cpstr;
19856 int cplen;
19857 int first = TRUE;
19858#endif
19859 char_u buf[NUMBUFLEN];
19860 char_u buf2[NUMBUFLEN];
19861 garray_T ga;
19862
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019863 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019864 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19865 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019866
19867 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019868 rettv->v_type = VAR_STRING;
19869 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019870 if (fromstr == NULL || tostr == NULL)
19871 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019872 ga_init2(&ga, (int)sizeof(char), 80);
19873
19874#ifdef FEAT_MBYTE
19875 if (!has_mbyte)
19876#endif
19877 /* not multi-byte: fromstr and tostr must be the same length */
19878 if (STRLEN(fromstr) != STRLEN(tostr))
19879 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019880#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019881error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019882#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019883 EMSG2(_(e_invarg2), fromstr);
19884 ga_clear(&ga);
19885 return;
19886 }
19887
19888 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019889 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019890 {
19891#ifdef FEAT_MBYTE
19892 if (has_mbyte)
19893 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019894 inlen = (*mb_ptr2len)(in_str);
19895 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019896 cplen = inlen;
19897 idx = 0;
19898 for (p = fromstr; *p != NUL; p += fromlen)
19899 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019900 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019901 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019902 {
19903 for (p = tostr; *p != NUL; p += tolen)
19904 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019905 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019906 if (idx-- == 0)
19907 {
19908 cplen = tolen;
19909 cpstr = p;
19910 break;
19911 }
19912 }
19913 if (*p == NUL) /* tostr is shorter than fromstr */
19914 goto error;
19915 break;
19916 }
19917 ++idx;
19918 }
19919
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019920 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019921 {
19922 /* Check that fromstr and tostr have the same number of
19923 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019924 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019925 first = FALSE;
19926 for (p = tostr; *p != NUL; p += tolen)
19927 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019928 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019929 --idx;
19930 }
19931 if (idx != 0)
19932 goto error;
19933 }
19934
Bram Moolenaarcde88542015-08-11 19:14:00 +020019935 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019936 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019937 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019938
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019939 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019940 }
19941 else
19942#endif
19943 {
19944 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019945 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019946 if (p != NULL)
19947 ga_append(&ga, tostr[p - fromstr]);
19948 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019949 ga_append(&ga, *in_str);
19950 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019951 }
19952 }
19953
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019954 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019955 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019956 ga_append(&ga, NUL);
19957
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019958 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019959}
19960
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019961#ifdef FEAT_FLOAT
19962/*
19963 * "trunc({float})" function
19964 */
19965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019966f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019967{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019968 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019969
19970 rettv->v_type = VAR_FLOAT;
19971 if (get_float_arg(argvars, &f) == OK)
19972 /* trunc() is not in C90, use floor() or ceil() instead. */
19973 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19974 else
19975 rettv->vval.v_float = 0.0;
19976}
19977#endif
19978
Bram Moolenaar8299df92004-07-10 09:47:34 +000019979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980 * "type(expr)" function
19981 */
19982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019983f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019984{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010019985 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019986
19987 switch (argvars[0].v_type)
19988 {
19989 case VAR_NUMBER: n = 0; break;
19990 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010019991 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019992 case VAR_FUNC: n = 2; break;
19993 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019994 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019995 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010019996 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010019997 if (argvars[0].vval.v_number == VVAL_FALSE
19998 || argvars[0].vval.v_number == VVAL_TRUE)
19999 n = 6;
20000 else
20001 n = 7;
20002 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020003 case VAR_JOB: n = 8; break;
20004 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020005 case VAR_UNKNOWN:
20006 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20007 n = -1;
20008 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020009 }
20010 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020011}
20012
20013/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020014 * "undofile(name)" function
20015 */
20016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020017f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020018{
20019 rettv->v_type = VAR_STRING;
20020#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020021 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020022 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020023
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020024 if (*fname == NUL)
20025 {
20026 /* If there is no file name there will be no undo file. */
20027 rettv->vval.v_string = NULL;
20028 }
20029 else
20030 {
20031 char_u *ffname = FullName_save(fname, FALSE);
20032
20033 if (ffname != NULL)
20034 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20035 vim_free(ffname);
20036 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020037 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020038#else
20039 rettv->vval.v_string = NULL;
20040#endif
20041}
20042
20043/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020044 * "undotree()" function
20045 */
20046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020047f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020048{
20049 if (rettv_dict_alloc(rettv) == OK)
20050 {
20051 dict_T *dict = rettv->vval.v_dict;
20052 list_T *list;
20053
Bram Moolenaar730cde92010-06-27 05:18:54 +020020054 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020055 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020056 dict_add_nr_str(dict, "save_last",
20057 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020058 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20059 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020060 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020061
20062 list = list_alloc();
20063 if (list != NULL)
20064 {
20065 u_eval_tree(curbuf->b_u_oldhead, list);
20066 dict_add_list(dict, "entries", list);
20067 }
20068 }
20069}
20070
20071/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020072 * "values(dict)" function
20073 */
20074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020075f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020076{
20077 dict_list(argvars, rettv, 1);
20078}
20079
20080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081 * "virtcol(string)" function
20082 */
20083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020084f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085{
20086 colnr_T vcol = 0;
20087 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020088 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020090 fp = var2fpos(&argvars[0], FALSE, &fnum);
20091 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20092 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093 {
20094 getvvcol(curwin, fp, NULL, NULL, &vcol);
20095 ++vcol;
20096 }
20097
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020098 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099}
20100
20101/*
20102 * "visualmode()" function
20103 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020104 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010020105f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020106{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107 char_u str[2];
20108
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020109 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020110 str[0] = curbuf->b_visual_mode_eval;
20111 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020112 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020113
20114 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020115 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020116 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117}
20118
20119/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020120 * "wildmenumode()" function
20121 */
20122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020123f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020124{
20125#ifdef FEAT_WILDMENU
20126 if (wild_menu_showing)
20127 rettv->vval.v_number = 1;
20128#endif
20129}
20130
20131/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132 * "winbufnr(nr)" function
20133 */
20134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020135f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136{
20137 win_T *wp;
20138
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020139 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020140 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020141 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020143 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144}
20145
20146/*
20147 * "wincol()" function
20148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020150f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020151{
20152 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020153 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020154}
20155
20156/*
20157 * "winheight(nr)" function
20158 */
20159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020160f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020161{
20162 win_T *wp;
20163
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020164 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020166 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020167 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020168 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020169}
20170
20171/*
20172 * "winline()" function
20173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020175f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176{
20177 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020178 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179}
20180
20181/*
20182 * "winnr()" function
20183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020185f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020186{
20187 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020188
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020190 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020192 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020193}
20194
20195/*
20196 * "winrestcmd()" function
20197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020199f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200{
20201#ifdef FEAT_WINDOWS
20202 win_T *wp;
20203 int winnr = 1;
20204 garray_T ga;
20205 char_u buf[50];
20206
20207 ga_init2(&ga, (int)sizeof(char), 70);
20208 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20209 {
20210 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20211 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20213 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214 ++winnr;
20215 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020216 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020218 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020220 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020222 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223}
20224
20225/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020226 * "winrestview()" function
20227 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020229f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020230{
20231 dict_T *dict;
20232
20233 if (argvars[0].v_type != VAR_DICT
20234 || (dict = argvars[0].vval.v_dict) == NULL)
20235 EMSG(_(e_invarg));
20236 else
20237 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020238 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020239 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
Bram Moolenaar82c25852014-05-28 16:47:16 +020020240 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020241 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020242#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020243 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020244 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020245#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020246 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20247 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020248 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar82c25852014-05-28 16:47:16 +020020249 curwin->w_set_curswant = FALSE;
20250 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020251
Bram Moolenaar82c25852014-05-28 16:47:16 +020020252 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020253 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020254#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020255 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020256 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020257#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020258 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020259 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
Bram Moolenaar82c25852014-05-28 16:47:16 +020020260 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020261 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020262
20263 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020264 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010020265# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020020266 win_new_width(curwin, W_WIDTH(curwin));
20267# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020268 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020269
Bram Moolenaarb851a962014-10-31 15:45:52 +010020270 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020271 curwin->w_topline = 1;
20272 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20273 curwin->w_topline = curbuf->b_ml.ml_line_count;
20274#ifdef FEAT_DIFF
20275 check_topfill(curwin, TRUE);
20276#endif
20277 }
20278}
20279
20280/*
20281 * "winsaveview()" function
20282 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020284f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020285{
20286 dict_T *dict;
20287
Bram Moolenaara800b422010-06-27 01:15:55 +020020288 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020289 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020290 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020291
20292 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20293 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20294#ifdef FEAT_VIRTUALEDIT
20295 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20296#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020297 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020298 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20299
20300 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20301#ifdef FEAT_DIFF
20302 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20303#endif
20304 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20305 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20306}
20307
20308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020309 * "winwidth(nr)" function
20310 */
20311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020312f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313{
20314 win_T *wp;
20315
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020316 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020317 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020318 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020319 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010020320#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020321 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020322#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020323 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020324#endif
20325}
20326
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020328 * "wordcount()" function
20329 */
20330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020331f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020332{
20333 if (rettv_dict_alloc(rettv) == FAIL)
20334 return;
20335 cursor_pos_info(rettv->vval.v_dict);
20336}
20337
20338/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020339 * Write list of strings to file
20340 */
20341 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020342write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020343{
20344 listitem_T *li;
20345 int c;
20346 int ret = OK;
20347 char_u *s;
20348
20349 for (li = list->lv_first; li != NULL; li = li->li_next)
20350 {
20351 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20352 {
20353 if (*s == '\n')
20354 c = putc(NUL, fd);
20355 else
20356 c = putc(*s, fd);
20357 if (c == EOF)
20358 {
20359 ret = FAIL;
20360 break;
20361 }
20362 }
20363 if (!binary || li->li_next != NULL)
20364 if (putc('\n', fd) == EOF)
20365 {
20366 ret = FAIL;
20367 break;
20368 }
20369 if (ret == FAIL)
20370 {
20371 EMSG(_(e_write));
20372 break;
20373 }
20374 }
20375 return ret;
20376}
20377
20378/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020379 * "writefile()" function
20380 */
20381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020382f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020383{
20384 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020385 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020386 char_u *fname;
20387 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020388 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020389
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020390 if (check_restricted() || check_secure())
20391 return;
20392
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020393 if (argvars[0].v_type != VAR_LIST)
20394 {
20395 EMSG2(_(e_listarg), "writefile()");
20396 return;
20397 }
20398 if (argvars[0].vval.v_list == NULL)
20399 return;
20400
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020401 if (argvars[2].v_type != VAR_UNKNOWN)
20402 {
20403 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20404 binary = TRUE;
20405 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20406 append = TRUE;
20407 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020408
20409 /* Always open the file in binary mode, library functions have a mind of
20410 * their own about CR-LF conversion. */
20411 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020412 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20413 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020414 {
20415 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20416 ret = -1;
20417 }
20418 else
20419 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020420 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20421 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020422 fclose(fd);
20423 }
20424
20425 rettv->vval.v_number = ret;
20426}
20427
20428/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020429 * "xor(expr, expr)" function
20430 */
20431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020432f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020433{
20434 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20435 ^ get_tv_number_chk(&argvars[1], NULL);
20436}
20437
20438
20439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020440 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020441 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020442 */
20443 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020444var2fpos(
20445 typval_T *varp,
20446 int dollar_lnum, /* TRUE when $ is last line */
20447 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020448{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020449 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020450 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020451 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020452
Bram Moolenaara5525202006-03-02 22:52:09 +000020453 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020454 if (varp->v_type == VAR_LIST)
20455 {
20456 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020457 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020458 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020459 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020460
20461 l = varp->vval.v_list;
20462 if (l == NULL)
20463 return NULL;
20464
20465 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020466 pos.lnum = list_find_nr(l, 0L, &error);
20467 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020468 return NULL; /* invalid line number */
20469
20470 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020471 pos.col = list_find_nr(l, 1L, &error);
20472 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020473 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020474 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020475
20476 /* We accept "$" for the column number: last column. */
20477 li = list_find(l, 1L);
20478 if (li != NULL && li->li_tv.v_type == VAR_STRING
20479 && li->li_tv.vval.v_string != NULL
20480 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20481 pos.col = len + 1;
20482
Bram Moolenaara5525202006-03-02 22:52:09 +000020483 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020484 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020485 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020486 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020487
Bram Moolenaara5525202006-03-02 22:52:09 +000020488#ifdef FEAT_VIRTUALEDIT
20489 /* Get the virtual offset. Defaults to zero. */
20490 pos.coladd = list_find_nr(l, 2L, &error);
20491 if (error)
20492 pos.coladd = 0;
20493#endif
20494
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020495 return &pos;
20496 }
20497
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020498 name = get_tv_string_chk(varp);
20499 if (name == NULL)
20500 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020501 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020502 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020503 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20504 {
20505 if (VIsual_active)
20506 return &VIsual;
20507 return &curwin->w_cursor;
20508 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020509 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020510 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020511 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020512 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20513 return NULL;
20514 return pp;
20515 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020516
20517#ifdef FEAT_VIRTUALEDIT
20518 pos.coladd = 0;
20519#endif
20520
Bram Moolenaar477933c2007-07-17 14:32:23 +000020521 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020522 {
20523 pos.col = 0;
20524 if (name[1] == '0') /* "w0": first visible line */
20525 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020526 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020527 pos.lnum = curwin->w_topline;
20528 return &pos;
20529 }
20530 else if (name[1] == '$') /* "w$": last visible line */
20531 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020532 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020533 pos.lnum = curwin->w_botline - 1;
20534 return &pos;
20535 }
20536 }
20537 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020538 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020539 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020540 {
20541 pos.lnum = curbuf->b_ml.ml_line_count;
20542 pos.col = 0;
20543 }
20544 else
20545 {
20546 pos.lnum = curwin->w_cursor.lnum;
20547 pos.col = (colnr_T)STRLEN(ml_get_curline());
20548 }
20549 return &pos;
20550 }
20551 return NULL;
20552}
20553
20554/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020555 * Convert list in "arg" into a position and optional file number.
20556 * When "fnump" is NULL there is no file number, only 3 items.
20557 * Note that the column is passed on as-is, the caller may want to decrement
20558 * it to use 1 for the first column.
20559 * Return FAIL when conversion is not possible, doesn't check the position for
20560 * validity.
20561 */
20562 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020563list2fpos(
20564 typval_T *arg,
20565 pos_T *posp,
20566 int *fnump,
20567 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020568{
20569 list_T *l = arg->vval.v_list;
20570 long i = 0;
20571 long n;
20572
Bram Moolenaar493c1782014-05-28 14:34:46 +020020573 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20574 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020575 if (arg->v_type != VAR_LIST
20576 || l == NULL
20577 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020578 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020579 return FAIL;
20580
20581 if (fnump != NULL)
20582 {
20583 n = list_find_nr(l, i++, NULL); /* fnum */
20584 if (n < 0)
20585 return FAIL;
20586 if (n == 0)
20587 n = curbuf->b_fnum; /* current buffer */
20588 *fnump = n;
20589 }
20590
20591 n = list_find_nr(l, i++, NULL); /* lnum */
20592 if (n < 0)
20593 return FAIL;
20594 posp->lnum = n;
20595
20596 n = list_find_nr(l, i++, NULL); /* col */
20597 if (n < 0)
20598 return FAIL;
20599 posp->col = n;
20600
20601#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020602 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020603 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020604 posp->coladd = 0;
20605 else
20606 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020607#endif
20608
Bram Moolenaar493c1782014-05-28 14:34:46 +020020609 if (curswantp != NULL)
20610 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20611
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020612 return OK;
20613}
20614
20615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020616 * Get the length of an environment variable name.
20617 * Advance "arg" to the first character after the name.
20618 * Return 0 for error.
20619 */
20620 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020621get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020622{
20623 char_u *p;
20624 int len;
20625
20626 for (p = *arg; vim_isIDc(*p); ++p)
20627 ;
20628 if (p == *arg) /* no name found */
20629 return 0;
20630
20631 len = (int)(p - *arg);
20632 *arg = p;
20633 return len;
20634}
20635
20636/*
20637 * Get the length of the name of a function or internal variable.
20638 * "arg" is advanced to the first non-white character after the name.
20639 * Return 0 if something is wrong.
20640 */
20641 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020642get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020643{
20644 char_u *p;
20645 int len;
20646
20647 /* Find the end of the name. */
20648 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020649 {
20650 if (*p == ':')
20651 {
20652 /* "s:" is start of "s:var", but "n:" is not and can be used in
20653 * slice "[n:]". Also "xx:" is not a namespace. */
20654 len = (int)(p - *arg);
20655 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20656 || len > 1)
20657 break;
20658 }
20659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020660 if (p == *arg) /* no name found */
20661 return 0;
20662
20663 len = (int)(p - *arg);
20664 *arg = skipwhite(p);
20665
20666 return len;
20667}
20668
20669/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020670 * Get the length of the name of a variable or function.
20671 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020672 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020673 * Return -1 if curly braces expansion failed.
20674 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020675 * If the name contains 'magic' {}'s, expand them and return the
20676 * expanded name in an allocated string via 'alias' - caller must free.
20677 */
20678 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020679get_name_len(
20680 char_u **arg,
20681 char_u **alias,
20682 int evaluate,
20683 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684{
20685 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020686 char_u *p;
20687 char_u *expr_start;
20688 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020689
20690 *alias = NULL; /* default to no alias */
20691
20692 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20693 && (*arg)[2] == (int)KE_SNR)
20694 {
20695 /* hard coded <SNR>, already translated */
20696 *arg += 3;
20697 return get_id_len(arg) + 3;
20698 }
20699 len = eval_fname_script(*arg);
20700 if (len > 0)
20701 {
20702 /* literal "<SID>", "s:" or "<SNR>" */
20703 *arg += len;
20704 }
20705
Bram Moolenaar071d4272004-06-13 20:20:40 +000020706 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020707 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020709 p = find_name_end(*arg, &expr_start, &expr_end,
20710 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020711 if (expr_start != NULL)
20712 {
20713 char_u *temp_string;
20714
20715 if (!evaluate)
20716 {
20717 len += (int)(p - *arg);
20718 *arg = skipwhite(p);
20719 return len;
20720 }
20721
20722 /*
20723 * Include any <SID> etc in the expanded string:
20724 * Thus the -len here.
20725 */
20726 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20727 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020728 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020729 *alias = temp_string;
20730 *arg = skipwhite(p);
20731 return (int)STRLEN(temp_string);
20732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020733
20734 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020735 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020736 EMSG2(_(e_invexpr2), *arg);
20737
20738 return len;
20739}
20740
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020741/*
20742 * Find the end of a variable or function name, taking care of magic braces.
20743 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20744 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020745 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020746 * Return a pointer to just after the name. Equal to "arg" if there is no
20747 * valid name.
20748 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020749 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020750find_name_end(
20751 char_u *arg,
20752 char_u **expr_start,
20753 char_u **expr_end,
20754 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020755{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020756 int mb_nest = 0;
20757 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020759 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020761 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020762 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020763 *expr_start = NULL;
20764 *expr_end = NULL;
20765 }
20766
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020767 /* Quick check for valid starting character. */
20768 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20769 return arg;
20770
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020771 for (p = arg; *p != NUL
20772 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020773 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020774 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020775 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020776 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020777 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020778 if (*p == '\'')
20779 {
20780 /* skip over 'string' to avoid counting [ and ] inside it. */
20781 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20782 ;
20783 if (*p == NUL)
20784 break;
20785 }
20786 else if (*p == '"')
20787 {
20788 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20789 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20790 if (*p == '\\' && p[1] != NUL)
20791 ++p;
20792 if (*p == NUL)
20793 break;
20794 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020795 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20796 {
20797 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020798 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020799 len = (int)(p - arg);
20800 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020801 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020802 break;
20803 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020805 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020806 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020807 if (*p == '[')
20808 ++br_nest;
20809 else if (*p == ']')
20810 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020811 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020812
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020813 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020814 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020815 if (*p == '{')
20816 {
20817 mb_nest++;
20818 if (expr_start != NULL && *expr_start == NULL)
20819 *expr_start = p;
20820 }
20821 else if (*p == '}')
20822 {
20823 mb_nest--;
20824 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20825 *expr_end = p;
20826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020828 }
20829
20830 return p;
20831}
20832
20833/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020834 * Expands out the 'magic' {}'s in a variable/function name.
20835 * Note that this can call itself recursively, to deal with
20836 * constructs like foo{bar}{baz}{bam}
20837 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20838 * "in_start" ^
20839 * "expr_start" ^
20840 * "expr_end" ^
20841 * "in_end" ^
20842 *
20843 * Returns a new allocated string, which the caller must free.
20844 * Returns NULL for failure.
20845 */
20846 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020847make_expanded_name(
20848 char_u *in_start,
20849 char_u *expr_start,
20850 char_u *expr_end,
20851 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020852{
20853 char_u c1;
20854 char_u *retval = NULL;
20855 char_u *temp_result;
20856 char_u *nextcmd = NULL;
20857
20858 if (expr_end == NULL || in_end == NULL)
20859 return NULL;
20860 *expr_start = NUL;
20861 *expr_end = NUL;
20862 c1 = *in_end;
20863 *in_end = NUL;
20864
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020865 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020866 if (temp_result != NULL && nextcmd == NULL)
20867 {
20868 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20869 + (in_end - expr_end) + 1));
20870 if (retval != NULL)
20871 {
20872 STRCPY(retval, in_start);
20873 STRCAT(retval, temp_result);
20874 STRCAT(retval, expr_end + 1);
20875 }
20876 }
20877 vim_free(temp_result);
20878
20879 *in_end = c1; /* put char back for error messages */
20880 *expr_start = '{';
20881 *expr_end = '}';
20882
20883 if (retval != NULL)
20884 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020885 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020886 if (expr_start != NULL)
20887 {
20888 /* Further expansion! */
20889 temp_result = make_expanded_name(retval, expr_start,
20890 expr_end, temp_result);
20891 vim_free(retval);
20892 retval = temp_result;
20893 }
20894 }
20895
20896 return retval;
20897}
20898
20899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020900 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020901 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902 */
20903 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020904eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020905{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020906 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20907}
20908
20909/*
20910 * Return TRUE if character "c" can be used as the first character in a
20911 * variable or function name (excluding '{' and '}').
20912 */
20913 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020914eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020915{
20916 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020917}
20918
20919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020920 * Set number v: variable to "val".
20921 */
20922 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020923set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020924{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020925 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020926}
20927
20928/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020929 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020930 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020931 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010020932get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020933{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020934 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020935}
20936
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020937/*
20938 * Get string v: variable value. Uses a static buffer, can only be used once.
20939 */
20940 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020941get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020942{
20943 return get_tv_string(&vimvars[idx].vv_tv);
20944}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020945
Bram Moolenaar071d4272004-06-13 20:20:40 +000020946/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020947 * Get List v: variable value. Caller must take care of reference count when
20948 * needed.
20949 */
20950 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020951get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000020952{
20953 return vimvars[idx].vv_list;
20954}
20955
20956/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020957 * Set v:char to character "c".
20958 */
20959 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020960set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020961{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020962 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020963
20964#ifdef FEAT_MBYTE
20965 if (has_mbyte)
20966 buf[(*mb_char2bytes)(c, buf)] = NUL;
20967 else
20968#endif
20969 {
20970 buf[0] = c;
20971 buf[1] = NUL;
20972 }
20973 set_vim_var_string(VV_CHAR, buf, -1);
20974}
20975
20976/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020977 * Set v:count to "count" and v:count1 to "count1".
20978 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020979 */
20980 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020981set_vcount(
20982 long count,
20983 long count1,
20984 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020985{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020986 if (set_prevcount)
20987 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020988 vimvars[VV_COUNT].vv_nr = count;
20989 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020990}
20991
20992/*
20993 * Set string v: variable to a copy of "val".
20994 */
20995 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020996set_vim_var_string(
20997 int idx,
20998 char_u *val,
20999 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021000{
Bram Moolenaara542c682016-01-31 16:28:04 +010021001 clear_tv(&vimvars[idx].vv_di.di_tv);
21002 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021003 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021004 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021006 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021008 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021009}
21010
21011/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021012 * Set List v: variable to "val".
21013 */
21014 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021015set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021016{
Bram Moolenaara542c682016-01-31 16:28:04 +010021017 clear_tv(&vimvars[idx].vv_di.di_tv);
21018 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021019 vimvars[idx].vv_list = val;
21020 if (val != NULL)
21021 ++val->lv_refcount;
21022}
21023
21024/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021025 * Set Dictionary v: variable to "val".
21026 */
21027 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021028set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021029{
21030 int todo;
21031 hashitem_T *hi;
21032
Bram Moolenaara542c682016-01-31 16:28:04 +010021033 clear_tv(&vimvars[idx].vv_di.di_tv);
21034 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021035 vimvars[idx].vv_dict = val;
21036 if (val != NULL)
21037 {
21038 ++val->dv_refcount;
21039
21040 /* Set readonly */
21041 todo = (int)val->dv_hashtab.ht_used;
21042 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21043 {
21044 if (HASHITEM_EMPTY(hi))
21045 continue;
21046 --todo;
21047 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21048 }
21049 }
21050}
21051
21052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021053 * Set v:register if needed.
21054 */
21055 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021056set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021057{
21058 char_u regname;
21059
21060 if (c == 0 || c == ' ')
21061 regname = '"';
21062 else
21063 regname = c;
21064 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021065 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021066 set_vim_var_string(VV_REG, &regname, 1);
21067}
21068
21069/*
21070 * Get or set v:exception. If "oldval" == NULL, return the current value.
21071 * Otherwise, restore the value to "oldval" and return NULL.
21072 * Must always be called in pairs to save and restore v:exception! Does not
21073 * take care of memory allocations.
21074 */
21075 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021076v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077{
21078 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021079 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021080
Bram Moolenaare9a41262005-01-15 22:18:47 +000021081 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021082 return NULL;
21083}
21084
21085/*
21086 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21087 * Otherwise, restore the value to "oldval" and return NULL.
21088 * Must always be called in pairs to save and restore v:throwpoint! Does not
21089 * take care of memory allocations.
21090 */
21091 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021092v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093{
21094 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021095 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096
Bram Moolenaare9a41262005-01-15 22:18:47 +000021097 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098 return NULL;
21099}
21100
21101#if defined(FEAT_AUTOCMD) || defined(PROTO)
21102/*
21103 * Set v:cmdarg.
21104 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21105 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21106 * Must always be called in pairs!
21107 */
21108 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021109set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110{
21111 char_u *oldval;
21112 char_u *newval;
21113 unsigned len;
21114
Bram Moolenaare9a41262005-01-15 22:18:47 +000021115 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021116 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021118 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021119 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021120 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121 }
21122
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021123 if (eap->force_bin == FORCE_BIN)
21124 len = 6;
21125 else if (eap->force_bin == FORCE_NOBIN)
21126 len = 8;
21127 else
21128 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021129
21130 if (eap->read_edit)
21131 len += 7;
21132
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021133 if (eap->force_ff != 0)
21134 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21135# ifdef FEAT_MBYTE
21136 if (eap->force_enc != 0)
21137 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021138 if (eap->bad_char != 0)
21139 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021140# endif
21141
21142 newval = alloc(len + 1);
21143 if (newval == NULL)
21144 return NULL;
21145
21146 if (eap->force_bin == FORCE_BIN)
21147 sprintf((char *)newval, " ++bin");
21148 else if (eap->force_bin == FORCE_NOBIN)
21149 sprintf((char *)newval, " ++nobin");
21150 else
21151 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021152
21153 if (eap->read_edit)
21154 STRCAT(newval, " ++edit");
21155
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021156 if (eap->force_ff != 0)
21157 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21158 eap->cmd + eap->force_ff);
21159# ifdef FEAT_MBYTE
21160 if (eap->force_enc != 0)
21161 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21162 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021163 if (eap->bad_char == BAD_KEEP)
21164 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21165 else if (eap->bad_char == BAD_DROP)
21166 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21167 else if (eap->bad_char != 0)
21168 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021169# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021170 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021171 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021172}
21173#endif
21174
21175/*
21176 * Get the value of internal variable "name".
21177 * Return OK or FAIL.
21178 */
21179 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021180get_var_tv(
21181 char_u *name,
21182 int len, /* length of "name" */
21183 typval_T *rettv, /* NULL when only checking existence */
21184 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21185 int verbose, /* may give error message */
21186 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187{
21188 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021189 typval_T *tv = NULL;
21190 typval_T atv;
21191 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021193
21194 /* truncate the name, so that we can use strcmp() */
21195 cc = name[len];
21196 name[len] = NUL;
21197
21198 /*
21199 * Check for "b:changedtick".
21200 */
21201 if (STRCMP(name, "b:changedtick") == 0)
21202 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021203 atv.v_type = VAR_NUMBER;
21204 atv.vval.v_number = curbuf->b_changedtick;
21205 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021206 }
21207
21208 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209 * Check for user-defined variables.
21210 */
21211 else
21212 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021213 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021214 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021215 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021216 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021217 if (dip != NULL)
21218 *dip = v;
21219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021220 }
21221
Bram Moolenaare9a41262005-01-15 22:18:47 +000021222 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021223 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021224 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021225 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226 ret = FAIL;
21227 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021228 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021229 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230
21231 name[len] = cc;
21232
21233 return ret;
21234}
21235
21236/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021237 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21238 * Also handle function call with Funcref variable: func(expr)
21239 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21240 */
21241 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021242handle_subscript(
21243 char_u **arg,
21244 typval_T *rettv,
21245 int evaluate, /* do more than finding the end */
21246 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021247{
21248 int ret = OK;
21249 dict_T *selfdict = NULL;
21250 char_u *s;
21251 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021252 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021253
21254 while (ret == OK
21255 && (**arg == '['
21256 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021257 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
21258 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021259 && !vim_iswhite(*(*arg - 1)))
21260 {
21261 if (**arg == '(')
21262 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010021263 partial_T *pt = NULL;
21264
Bram Moolenaard9fba312005-06-26 22:34:35 +000021265 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021266 if (evaluate)
21267 {
21268 functv = *rettv;
21269 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021270
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021271 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021272 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021273 {
21274 pt = functv.vval.v_partial;
21275 s = pt->pt_name;
21276 }
21277 else
21278 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021279 }
21280 else
21281 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021282 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021283 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021284 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000021285
21286 /* Clear the funcref afterwards, so that deleting it while
21287 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021288 if (evaluate)
21289 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021290
21291 /* Stop the expression evaluation when immediately aborting on
21292 * error, or when an interrupt occurred or an exception was thrown
21293 * but not caught. */
21294 if (aborting())
21295 {
21296 if (ret == OK)
21297 clear_tv(rettv);
21298 ret = FAIL;
21299 }
21300 dict_unref(selfdict);
21301 selfdict = NULL;
21302 }
21303 else /* **arg == '[' || **arg == '.' */
21304 {
21305 dict_unref(selfdict);
21306 if (rettv->v_type == VAR_DICT)
21307 {
21308 selfdict = rettv->vval.v_dict;
21309 if (selfdict != NULL)
21310 ++selfdict->dv_refcount;
21311 }
21312 else
21313 selfdict = NULL;
21314 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21315 {
21316 clear_tv(rettv);
21317 ret = FAIL;
21318 }
21319 }
21320 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021321
Bram Moolenaar1d429612016-05-24 15:44:17 +020021322 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
21323 * Don't do this when "Func" is already a partial that was bound
21324 * explicitly (pt_auto is FALSE). */
21325 if (selfdict != NULL
21326 && (rettv->v_type == VAR_FUNC
21327 || (rettv->v_type == VAR_PARTIAL
21328 && (rettv->vval.v_partial->pt_auto
21329 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021330 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021331 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
21332 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021333 char_u *tofree = NULL;
21334 ufunc_T *fp;
21335 char_u fname_buf[FLEN_FIXED + 1];
21336 int error;
21337
21338 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021339 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021340 fp = find_func(fname);
21341 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021342
Bram Moolenaar65639032016-03-16 21:40:30 +010021343 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021344 {
Bram Moolenaar65639032016-03-16 21:40:30 +010021345 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
21346
21347 if (pt != NULL)
21348 {
21349 pt->pt_refcount = 1;
21350 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020021351 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010021352 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021353 if (rettv->v_type == VAR_FUNC)
21354 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021355 /* Just a function: Take over the function name and use
21356 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021357 pt->pt_name = rettv->vval.v_string;
21358 }
21359 else
21360 {
21361 partial_T *ret_pt = rettv->vval.v_partial;
21362 int i;
21363
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021364 /* Partial: copy the function name, use selfdict and copy
21365 * args. Can't take over name or args, the partial might
21366 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021367 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021368 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021369 if (ret_pt->pt_argc > 0)
21370 {
21371 pt->pt_argv = (typval_T *)alloc(
21372 sizeof(typval_T) * ret_pt->pt_argc);
21373 if (pt->pt_argv == NULL)
21374 /* out of memory: drop the arguments */
21375 pt->pt_argc = 0;
21376 else
21377 {
21378 pt->pt_argc = ret_pt->pt_argc;
21379 for (i = 0; i < pt->pt_argc; i++)
21380 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
21381 }
21382 }
21383 partial_unref(ret_pt);
21384 }
Bram Moolenaar65639032016-03-16 21:40:30 +010021385 rettv->v_type = VAR_PARTIAL;
21386 rettv->vval.v_partial = pt;
21387 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021388 }
21389 }
21390
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021391 dict_unref(selfdict);
21392 return ret;
21393}
21394
21395/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021396 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021397 * value).
21398 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021399 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021400alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021401{
Bram Moolenaar33570922005-01-25 22:26:29 +000021402 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021403}
21404
21405/*
21406 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021407 * The string "s" must have been allocated, it is consumed.
21408 * Return NULL for out of memory, the variable otherwise.
21409 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021410 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021411alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021412{
Bram Moolenaar33570922005-01-25 22:26:29 +000021413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021415 rettv = alloc_tv();
21416 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021417 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021418 rettv->v_type = VAR_STRING;
21419 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021420 }
21421 else
21422 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021423 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021424}
21425
21426/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021427 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021428 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021429 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021430free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431{
21432 if (varp != NULL)
21433 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021434 switch (varp->v_type)
21435 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021436 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021437 func_unref(varp->vval.v_string);
21438 /*FALLTHROUGH*/
21439 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021440 vim_free(varp->vval.v_string);
21441 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021442 case VAR_PARTIAL:
21443 partial_unref(varp->vval.v_partial);
21444 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021445 case VAR_LIST:
21446 list_unref(varp->vval.v_list);
21447 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021448 case VAR_DICT:
21449 dict_unref(varp->vval.v_dict);
21450 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021451 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021452#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021453 job_unref(varp->vval.v_job);
21454 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021455#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021456 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021457#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021458 channel_unref(varp->vval.v_channel);
21459 break;
21460#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021461 case VAR_NUMBER:
21462 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021463 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021464 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021465 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021467 vim_free(varp);
21468 }
21469}
21470
21471/*
21472 * Free the memory for a variable value and set the value to NULL or 0.
21473 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021474 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021475clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476{
21477 if (varp != NULL)
21478 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021479 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021480 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021481 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021482 func_unref(varp->vval.v_string);
21483 /*FALLTHROUGH*/
21484 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021485 vim_free(varp->vval.v_string);
21486 varp->vval.v_string = NULL;
21487 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021488 case VAR_PARTIAL:
21489 partial_unref(varp->vval.v_partial);
21490 varp->vval.v_partial = NULL;
21491 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021492 case VAR_LIST:
21493 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021494 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021495 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021496 case VAR_DICT:
21497 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021498 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021499 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021500 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021501 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021502 varp->vval.v_number = 0;
21503 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021504 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021505#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021506 varp->vval.v_float = 0.0;
21507 break;
21508#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021509 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021510#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021511 job_unref(varp->vval.v_job);
21512 varp->vval.v_job = NULL;
21513#endif
21514 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021515 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021516#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021517 channel_unref(varp->vval.v_channel);
21518 varp->vval.v_channel = NULL;
21519#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021520 case VAR_UNKNOWN:
21521 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021522 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021523 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021524 }
21525}
21526
21527/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021528 * Set the value of a variable to NULL without freeing items.
21529 */
21530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021531init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021532{
21533 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021534 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021535}
21536
21537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538 * Get the number value of a variable.
21539 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021540 * For incompatible types, return 0.
21541 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21542 * caller of incompatible types: it sets *denote to TRUE if "denote"
21543 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021545 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021546get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021548 int error = FALSE;
21549
21550 return get_tv_number_chk(varp, &error); /* return 0L on error */
21551}
21552
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021553 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021554get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021555{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021556 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021557
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021558 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021559 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021560 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021561 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021562 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021563#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021564 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021565 break;
21566#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021567 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021568 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021569 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021570 break;
21571 case VAR_STRING:
21572 if (varp->vval.v_string != NULL)
21573 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021574 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021575 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021576 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021577 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021578 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021579 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021580 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021581 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021582 case VAR_SPECIAL:
21583 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21584 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021585 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021586#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021587 EMSG(_("E910: Using a Job as a Number"));
21588 break;
21589#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021590 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021591#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021592 EMSG(_("E913: Using a Channel as a Number"));
21593 break;
21594#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021595 case VAR_UNKNOWN:
21596 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021597 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021598 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021599 if (denote == NULL) /* useful for values that must be unsigned */
21600 n = -1;
21601 else
21602 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021603 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604}
21605
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021606#ifdef FEAT_FLOAT
21607 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021608get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021609{
21610 switch (varp->v_type)
21611 {
21612 case VAR_NUMBER:
21613 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021614 case VAR_FLOAT:
21615 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021616 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021617 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021618 EMSG(_("E891: Using a Funcref as a Float"));
21619 break;
21620 case VAR_STRING:
21621 EMSG(_("E892: Using a String as a Float"));
21622 break;
21623 case VAR_LIST:
21624 EMSG(_("E893: Using a List as a Float"));
21625 break;
21626 case VAR_DICT:
21627 EMSG(_("E894: Using a Dictionary as a Float"));
21628 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021629 case VAR_SPECIAL:
21630 EMSG(_("E907: Using a special value as a Float"));
21631 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021632 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021633# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021634 EMSG(_("E911: Using a Job as a Float"));
21635 break;
21636# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021637 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021638# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021639 EMSG(_("E914: Using a Channel as a Float"));
21640 break;
21641# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021642 case VAR_UNKNOWN:
21643 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021644 break;
21645 }
21646 return 0;
21647}
21648#endif
21649
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021651 * Get the lnum from the first argument.
21652 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021653 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654 */
21655 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021656get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657{
Bram Moolenaar33570922005-01-25 22:26:29 +000021658 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021659 linenr_T lnum;
21660
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021661 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662 if (lnum == 0) /* no valid number, try using line() */
21663 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021664 rettv.v_type = VAR_NUMBER;
21665 f_line(argvars, &rettv);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021666 lnum = (linenr_T)rettv.vval.v_number;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021667 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021668 }
21669 return lnum;
21670}
21671
21672/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021673 * Get the lnum from the first argument.
21674 * Also accepts "$", then "buf" is used.
21675 * Returns 0 on error.
21676 */
21677 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021678get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021679{
21680 if (argvars[0].v_type == VAR_STRING
21681 && argvars[0].vval.v_string != NULL
21682 && argvars[0].vval.v_string[0] == '$'
21683 && buf != NULL)
21684 return buf->b_ml.ml_line_count;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021685 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar661b1822005-07-28 22:36:45 +000021686}
21687
21688/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021689 * Get the string value of a variable.
21690 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021691 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21692 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021693 * If the String variable has never been set, return an empty string.
21694 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021695 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21696 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021697 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010021698 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021699get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021700{
21701 static char_u mybuf[NUMBUFLEN];
21702
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021703 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704}
21705
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010021706 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021707get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021709 char_u *res = get_tv_string_buf_chk(varp, buf);
21710
21711 return res != NULL ? res : (char_u *)"";
21712}
21713
Bram Moolenaar7d647822014-04-05 21:28:56 +020021714/*
21715 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21716 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021717 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021718get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021719{
21720 static char_u mybuf[NUMBUFLEN];
21721
21722 return get_tv_string_buf_chk(varp, mybuf);
21723}
21724
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021725 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021726get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021727{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021728 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021730 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021731 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
21732 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021733 return buf;
21734 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021735 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021736 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021737 break;
21738 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021739 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021740 break;
21741 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021742 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021743 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021744 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021745#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021746 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021747 break;
21748#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021749 case VAR_STRING:
21750 if (varp->vval.v_string != NULL)
21751 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021752 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021753 case VAR_SPECIAL:
21754 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21755 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021756 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021757#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021758 {
21759 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010021760 char *status;
21761
21762 if (job == NULL)
21763 return (char_u *)"no process";
21764 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010021765 : job->jv_status == JOB_ENDED ? "dead"
21766 : "run";
21767# ifdef UNIX
21768 vim_snprintf((char *)buf, NUMBUFLEN,
21769 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021770# elif defined(WIN32)
21771 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010021772 "process %ld %s",
21773 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021774 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010021775# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021776 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010021777 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
21778# endif
21779 return buf;
21780 }
21781#endif
21782 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021783 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021784#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021785 {
21786 channel_T *channel = varp->vval.v_channel;
21787 char *status = channel_status(channel);
21788
Bram Moolenaar5cefd402016-02-16 12:44:26 +010021789 if (channel == NULL)
21790 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
21791 else
21792 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010021793 "channel %d %s", channel->ch_id, status);
21794 return buf;
21795 }
21796#endif
21797 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021798 case VAR_UNKNOWN:
21799 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021800 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021801 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021802 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021803}
21804
21805/*
21806 * Find variable "name" in the list of variables.
21807 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021808 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021809 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021810 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021811 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021812 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021813find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021814{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021815 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021816 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021817
Bram Moolenaara7043832005-01-21 11:56:39 +000021818 ht = find_var_ht(name, &varname);
21819 if (htp != NULL)
21820 *htp = ht;
21821 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021822 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021823 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021824}
21825
21826/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021827 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021828 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021829 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021830 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021831find_var_in_ht(
21832 hashtab_T *ht,
21833 int htname,
21834 char_u *varname,
21835 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021836{
Bram Moolenaar33570922005-01-25 22:26:29 +000021837 hashitem_T *hi;
21838
21839 if (*varname == NUL)
21840 {
21841 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021842 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021843 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021844 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021845 case 'g': return &globvars_var;
21846 case 'v': return &vimvars_var;
21847 case 'b': return &curbuf->b_bufvar;
21848 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021849#ifdef FEAT_WINDOWS
21850 case 't': return &curtab->tp_winvar;
21851#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021852 case 'l': return current_funccal == NULL
21853 ? NULL : &current_funccal->l_vars_var;
21854 case 'a': return current_funccal == NULL
21855 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021856 }
21857 return NULL;
21858 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021859
21860 hi = hash_find(ht, varname);
21861 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021862 {
21863 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021864 * worked find the variable again. Don't auto-load a script if it was
21865 * loaded already, otherwise it would be loaded every time when
21866 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021867 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021868 {
21869 /* Note: script_autoload() may make "hi" invalid. It must either
21870 * be obtained again or not used. */
21871 if (!script_autoload(varname, FALSE) || aborting())
21872 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021873 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021874 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021875 if (HASHITEM_EMPTY(hi))
21876 return NULL;
21877 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021878 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021879}
21880
21881/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021882 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021883 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021884 * Set "varname" to the start of name without ':'.
21885 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021886 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021887find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021888{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021889 hashitem_T *hi;
21890
Bram Moolenaar73627d02015-08-11 15:46:09 +020021891 if (name[0] == NUL)
21892 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893 if (name[1] != ':')
21894 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021895 /* The name must not start with a colon or #. */
21896 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021897 return NULL;
21898 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021899
21900 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021901 hi = hash_find(&compat_hashtab, name);
21902 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021903 return &compat_hashtab;
21904
Bram Moolenaar071d4272004-06-13 20:20:40 +000021905 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021906 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021907 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908 }
21909 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021910 if (*name == 'g') /* global variable */
21911 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021912 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21913 */
21914 if (vim_strchr(name + 2, ':') != NULL
21915 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021916 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021917 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021918 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021919 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021920 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021921#ifdef FEAT_WINDOWS
21922 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021923 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021924#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021925 if (*name == 'v') /* v: variable */
21926 return &vimvarht;
21927 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021928 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021929 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021930 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021931 if (*name == 's' /* script variable */
21932 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21933 return &SCRIPT_VARS(current_SID);
21934 return NULL;
21935}
21936
21937/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021938 * Get function call environment based on bactrace debug level
21939 */
21940 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021941get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021942{
21943 int i;
21944 funccall_T *funccal;
21945 funccall_T *temp_funccal;
21946
21947 funccal = current_funccal;
21948 if (debug_backtrace_level > 0)
21949 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021950 for (i = 0; i < debug_backtrace_level; i++)
21951 {
21952 temp_funccal = funccal->caller;
21953 if (temp_funccal)
21954 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021955 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021956 /* backtrace level overflow. reset to max */
21957 debug_backtrace_level = i;
21958 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021959 }
21960 return funccal;
21961}
21962
21963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021964 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021965 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021966 * Returns NULL when it doesn't exist.
21967 */
21968 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021969get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021970{
Bram Moolenaar33570922005-01-25 22:26:29 +000021971 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021972
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021973 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021974 if (v == NULL)
21975 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021976 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021977}
21978
21979/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021980 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021981 * sourcing this script and when executing functions defined in the script.
21982 */
21983 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021984new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021985{
Bram Moolenaara7043832005-01-21 11:56:39 +000021986 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021987 hashtab_T *ht;
21988 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021989
Bram Moolenaar071d4272004-06-13 20:20:40 +000021990 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21991 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021992 /* Re-allocating ga_data means that an ht_array pointing to
21993 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021994 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021995 for (i = 1; i <= ga_scripts.ga_len; ++i)
21996 {
21997 ht = &SCRIPT_VARS(i);
21998 if (ht->ht_mask == HT_INIT_SIZE - 1)
21999 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022000 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022001 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022002 }
22003
Bram Moolenaar071d4272004-06-13 20:20:40 +000022004 while (ga_scripts.ga_len < id)
22005 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022006 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022007 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022008 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022009 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022010 }
22011 }
22012}
22013
22014/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022015 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22016 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022017 */
22018 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022019init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022020{
Bram Moolenaar33570922005-01-25 22:26:29 +000022021 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022022 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022023 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022024 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022025 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022026 dict_var->di_tv.vval.v_dict = dict;
22027 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022028 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022029 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22030 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022031}
22032
22033/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022034 * Unreference a dictionary initialized by init_var_dict().
22035 */
22036 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022037unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022038{
22039 /* Now the dict needs to be freed if no one else is using it, go back to
22040 * normal reference counting. */
22041 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22042 dict_unref(dict);
22043}
22044
22045/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022047 * Frees all allocated variables and the value they contain.
22048 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022049 */
22050 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022051vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022052{
22053 vars_clear_ext(ht, TRUE);
22054}
22055
22056/*
22057 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22058 */
22059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022060vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061{
Bram Moolenaara7043832005-01-21 11:56:39 +000022062 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022063 hashitem_T *hi;
22064 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022065
Bram Moolenaar33570922005-01-25 22:26:29 +000022066 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022067 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022068 for (hi = ht->ht_array; todo > 0; ++hi)
22069 {
22070 if (!HASHITEM_EMPTY(hi))
22071 {
22072 --todo;
22073
Bram Moolenaar33570922005-01-25 22:26:29 +000022074 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022075 * ht_array might change then. hash_clear() takes care of it
22076 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022077 v = HI2DI(hi);
22078 if (free_val)
22079 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022080 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022081 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022082 }
22083 }
22084 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022085 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022086}
22087
Bram Moolenaara7043832005-01-21 11:56:39 +000022088/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022089 * Delete a variable from hashtab "ht" at item "hi".
22090 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022093delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022094{
Bram Moolenaar33570922005-01-25 22:26:29 +000022095 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022096
22097 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022098 clear_tv(&di->di_tv);
22099 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100}
22101
22102/*
22103 * List the value of one internal variable.
22104 */
22105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022106list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022107{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022108 char_u *tofree;
22109 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022110 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022111
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022112 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022113 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022114 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022115 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022116}
22117
Bram Moolenaar071d4272004-06-13 20:20:40 +000022118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022119list_one_var_a(
22120 char_u *prefix,
22121 char_u *name,
22122 int type,
22123 char_u *string,
22124 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022125{
Bram Moolenaar31859182007-08-14 20:41:13 +000022126 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22127 msg_start();
22128 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022129 if (name != NULL) /* "a:" vars don't have a name stored */
22130 msg_puts(name);
22131 msg_putchar(' ');
22132 msg_advance(22);
22133 if (type == VAR_NUMBER)
22134 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022135 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022136 msg_putchar('*');
22137 else if (type == VAR_LIST)
22138 {
22139 msg_putchar('[');
22140 if (*string == '[')
22141 ++string;
22142 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022143 else if (type == VAR_DICT)
22144 {
22145 msg_putchar('{');
22146 if (*string == '{')
22147 ++string;
22148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022149 else
22150 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022151
Bram Moolenaar071d4272004-06-13 20:20:40 +000022152 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022153
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022154 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022155 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022156 if (*first)
22157 {
22158 msg_clr_eos();
22159 *first = FALSE;
22160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161}
22162
22163/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022164 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022165 * If the variable already exists, the value is updated.
22166 * Otherwise the variable is created.
22167 */
22168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022169set_var(
22170 char_u *name,
22171 typval_T *tv,
22172 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022173{
Bram Moolenaar33570922005-01-25 22:26:29 +000022174 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022176 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022177
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022178 ht = find_var_ht(name, &varname);
22179 if (ht == NULL || *varname == NUL)
22180 {
22181 EMSG2(_(e_illvar), name);
22182 return;
22183 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022184 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022185
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022186 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
22187 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022188 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022189
Bram Moolenaar33570922005-01-25 22:26:29 +000022190 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022191 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022192 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022193 if (var_check_ro(v->di_flags, name, FALSE)
22194 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022195 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022196
22197 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022198 * Handle setting internal v: variables separately where needed to
22199 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022200 */
22201 if (ht == &vimvarht)
22202 {
22203 if (v->di_tv.v_type == VAR_STRING)
22204 {
22205 vim_free(v->di_tv.vval.v_string);
22206 if (copy || tv->v_type != VAR_STRING)
22207 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22208 else
22209 {
22210 /* Take over the string to avoid an extra alloc/free. */
22211 v->di_tv.vval.v_string = tv->vval.v_string;
22212 tv->vval.v_string = NULL;
22213 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022214 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022215 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022216 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022217 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022218 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022219 if (STRCMP(varname, "searchforward") == 0)
22220 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022221#ifdef FEAT_SEARCH_EXTRA
22222 else if (STRCMP(varname, "hlsearch") == 0)
22223 {
22224 no_hlsearch = !v->di_tv.vval.v_number;
22225 redraw_all_later(SOME_VALID);
22226 }
22227#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022228 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022229 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022230 else if (v->di_tv.v_type != tv->v_type)
22231 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022232 }
22233
22234 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022235 }
22236 else /* add a new variable */
22237 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022238 /* Can't add "v:" variable. */
22239 if (ht == &vimvarht)
22240 {
22241 EMSG2(_(e_illvar), name);
22242 return;
22243 }
22244
Bram Moolenaar92124a32005-06-17 22:03:40 +000022245 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022246 if (!valid_varname(varname))
22247 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022248
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022249 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22250 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022251 if (v == NULL)
22252 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022253 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022254 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022255 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022256 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022257 return;
22258 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022259 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022260 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022261
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022262 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022263 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022264 else
22265 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022266 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022267 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022268 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022270}
22271
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022272/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022273 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022274 * Also give an error message.
22275 */
Bram Moolenaarcd524592016-07-17 14:57:05 +020022276 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022277var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022278{
22279 if (flags & DI_FLAGS_RO)
22280 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022281 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022282 return TRUE;
22283 }
22284 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22285 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022286 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022287 return TRUE;
22288 }
22289 return FALSE;
22290}
22291
22292/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022293 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22294 * Also give an error message.
22295 */
22296 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022297var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022298{
22299 if (flags & DI_FLAGS_FIX)
22300 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022301 EMSG2(_("E795: Cannot delete variable %s"),
22302 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022303 return TRUE;
22304 }
22305 return FALSE;
22306}
22307
22308/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022309 * Check if a funcref is assigned to a valid variable name.
22310 * Return TRUE and give an error if not.
22311 */
Bram Moolenaarcd524592016-07-17 14:57:05 +020022312 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022313var_check_func_name(
22314 char_u *name, /* points to start of variable name */
22315 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022316{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022317 /* Allow for w: b: s: and t:. */
22318 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022319 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22320 ? name[2] : name[0]))
22321 {
22322 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22323 name);
22324 return TRUE;
22325 }
22326 /* Don't allow hiding a function. When "v" is not NULL we might be
22327 * assigning another function to the same var, the type is checked
22328 * below. */
22329 if (new_var && function_exists(name))
22330 {
22331 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22332 name);
22333 return TRUE;
22334 }
22335 return FALSE;
22336}
22337
22338/*
22339 * Check if a variable name is valid.
22340 * Return FALSE and give an error if not.
22341 */
Bram Moolenaarcd524592016-07-17 14:57:05 +020022342 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022343valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022344{
22345 char_u *p;
22346
22347 for (p = varname; *p != NUL; ++p)
22348 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22349 && *p != AUTOLOAD_CHAR)
22350 {
22351 EMSG2(_(e_illvar), varname);
22352 return FALSE;
22353 }
22354 return TRUE;
22355}
22356
22357/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022358 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022359 * Also give an error message, using "name" or _("name") when use_gettext is
22360 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022361 */
Bram Moolenaarcd524592016-07-17 14:57:05 +020022362 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022363tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022364{
22365 if (lock & VAR_LOCKED)
22366 {
22367 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022368 name == NULL ? (char_u *)_("Unknown")
22369 : use_gettext ? (char_u *)_(name)
22370 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022371 return TRUE;
22372 }
22373 if (lock & VAR_FIXED)
22374 {
22375 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022376 name == NULL ? (char_u *)_("Unknown")
22377 : use_gettext ? (char_u *)_(name)
22378 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022379 return TRUE;
22380 }
22381 return FALSE;
22382}
22383
22384/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022385 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022386 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022387 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022388 * It is OK for "from" and "to" to point to the same item. This is used to
22389 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022390 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022391 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022392copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022393{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022394 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022395 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022396 switch (from->v_type)
22397 {
22398 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022399 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022400 to->vval.v_number = from->vval.v_number;
22401 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022402 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022403#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022404 to->vval.v_float = from->vval.v_float;
22405 break;
22406#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022407 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022408#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022409 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010022410 if (to->vval.v_job != NULL)
22411 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022412 break;
22413#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022414 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022415#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022416 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022417 if (to->vval.v_channel != NULL)
22418 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022419 break;
22420#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022421 case VAR_STRING:
22422 case VAR_FUNC:
22423 if (from->vval.v_string == NULL)
22424 to->vval.v_string = NULL;
22425 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022426 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022427 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022428 if (from->v_type == VAR_FUNC)
22429 func_ref(to->vval.v_string);
22430 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022431 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022432 case VAR_PARTIAL:
22433 if (from->vval.v_partial == NULL)
22434 to->vval.v_partial = NULL;
22435 else
22436 {
22437 to->vval.v_partial = from->vval.v_partial;
22438 ++to->vval.v_partial->pt_refcount;
22439 }
22440 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022441 case VAR_LIST:
22442 if (from->vval.v_list == NULL)
22443 to->vval.v_list = NULL;
22444 else
22445 {
22446 to->vval.v_list = from->vval.v_list;
22447 ++to->vval.v_list->lv_refcount;
22448 }
22449 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022450 case VAR_DICT:
22451 if (from->vval.v_dict == NULL)
22452 to->vval.v_dict = NULL;
22453 else
22454 {
22455 to->vval.v_dict = from->vval.v_dict;
22456 ++to->vval.v_dict->dv_refcount;
22457 }
22458 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022459 case VAR_UNKNOWN:
22460 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022461 break;
22462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463}
22464
22465/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022466 * Make a copy of an item.
22467 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022468 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22469 * reference to an already copied list/dict can be used.
22470 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022471 */
Bram Moolenaarcd524592016-07-17 14:57:05 +020022472 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022473item_copy(
22474 typval_T *from,
22475 typval_T *to,
22476 int deep,
22477 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022478{
22479 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022480 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022481
Bram Moolenaar33570922005-01-25 22:26:29 +000022482 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022483 {
22484 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022485 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022486 }
22487 ++recurse;
22488
22489 switch (from->v_type)
22490 {
22491 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022492 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022493 case VAR_STRING:
22494 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022495 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010022496 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022497 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022498 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022499 copy_tv(from, to);
22500 break;
22501 case VAR_LIST:
22502 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022503 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022504 if (from->vval.v_list == NULL)
22505 to->vval.v_list = NULL;
22506 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22507 {
22508 /* use the copy made earlier */
22509 to->vval.v_list = from->vval.v_list->lv_copylist;
22510 ++to->vval.v_list->lv_refcount;
22511 }
22512 else
22513 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22514 if (to->vval.v_list == NULL)
22515 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022516 break;
22517 case VAR_DICT:
22518 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022519 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022520 if (from->vval.v_dict == NULL)
22521 to->vval.v_dict = NULL;
22522 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22523 {
22524 /* use the copy made earlier */
22525 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22526 ++to->vval.v_dict->dv_refcount;
22527 }
22528 else
22529 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22530 if (to->vval.v_dict == NULL)
22531 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022532 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022533 case VAR_UNKNOWN:
22534 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022535 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022536 }
22537 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022538 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022539}
22540
22541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022542 * ":echo expr1 ..." print each argument separated with a space, add a
22543 * newline at the end.
22544 * ":echon expr1 ..." print each argument plain.
22545 */
22546 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022547ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022548{
22549 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022550 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022551 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022552 char_u *p;
22553 int needclr = TRUE;
22554 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022555 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022556
22557 if (eap->skip)
22558 ++emsg_skip;
22559 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22560 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022561 /* If eval1() causes an error message the text from the command may
22562 * still need to be cleared. E.g., "echo 22,44". */
22563 need_clr_eos = needclr;
22564
Bram Moolenaar071d4272004-06-13 20:20:40 +000022565 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022566 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022567 {
22568 /*
22569 * Report the invalid expression unless the expression evaluation
22570 * has been cancelled due to an aborting error, an interrupt, or an
22571 * exception.
22572 */
22573 if (!aborting())
22574 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022575 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576 break;
22577 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022578 need_clr_eos = FALSE;
22579
Bram Moolenaar071d4272004-06-13 20:20:40 +000022580 if (!eap->skip)
22581 {
22582 if (atstart)
22583 {
22584 atstart = FALSE;
22585 /* Call msg_start() after eval1(), evaluating the expression
22586 * may cause a message to appear. */
22587 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022588 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022589 /* Mark the saved text as finishing the line, so that what
22590 * follows is displayed on a new line when scrolling back
22591 * at the more prompt. */
22592 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022593 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022595 }
22596 else if (eap->cmdidx == CMD_echo)
22597 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022598 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022599 if (p != NULL)
22600 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022602 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022603 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022604 if (*p != TAB && needclr)
22605 {
22606 /* remove any text still there from the command */
22607 msg_clr_eos();
22608 needclr = FALSE;
22609 }
22610 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022611 }
22612 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022613 {
22614#ifdef FEAT_MBYTE
22615 if (has_mbyte)
22616 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022617 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022618
22619 (void)msg_outtrans_len_attr(p, i, echo_attr);
22620 p += i - 1;
22621 }
22622 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022623#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022624 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022626 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022627 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022628 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022629 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630 arg = skipwhite(arg);
22631 }
22632 eap->nextcmd = check_nextcmd(arg);
22633
22634 if (eap->skip)
22635 --emsg_skip;
22636 else
22637 {
22638 /* remove text that may still be there from the command */
22639 if (needclr)
22640 msg_clr_eos();
22641 if (eap->cmdidx == CMD_echo)
22642 msg_end();
22643 }
22644}
22645
22646/*
22647 * ":echohl {name}".
22648 */
22649 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022650ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651{
22652 int id;
22653
22654 id = syn_name2id(eap->arg);
22655 if (id == 0)
22656 echo_attr = 0;
22657 else
22658 echo_attr = syn_id2attr(id);
22659}
22660
22661/*
22662 * ":execute expr1 ..." execute the result of an expression.
22663 * ":echomsg expr1 ..." Print a message
22664 * ":echoerr expr1 ..." Print an error
22665 * Each gets spaces around each argument and a newline at the end for
22666 * echo commands
22667 */
22668 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022669ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022670{
22671 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022672 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022673 int ret = OK;
22674 char_u *p;
22675 garray_T ga;
22676 int len;
22677 int save_did_emsg;
22678
22679 ga_init2(&ga, 1, 80);
22680
22681 if (eap->skip)
22682 ++emsg_skip;
22683 while (*arg != NUL && *arg != '|' && *arg != '\n')
22684 {
22685 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022686 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022687 {
22688 /*
22689 * Report the invalid expression unless the expression evaluation
22690 * has been cancelled due to an aborting error, an interrupt, or an
22691 * exception.
22692 */
22693 if (!aborting())
22694 EMSG2(_(e_invexpr2), p);
22695 ret = FAIL;
22696 break;
22697 }
22698
22699 if (!eap->skip)
22700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022701 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022702 len = (int)STRLEN(p);
22703 if (ga_grow(&ga, len + 2) == FAIL)
22704 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022705 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022706 ret = FAIL;
22707 break;
22708 }
22709 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022710 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022711 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712 ga.ga_len += len;
22713 }
22714
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022715 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022716 arg = skipwhite(arg);
22717 }
22718
22719 if (ret != FAIL && ga.ga_data != NULL)
22720 {
22721 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022722 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022723 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022724 out_flush();
22725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 else if (eap->cmdidx == CMD_echoerr)
22727 {
22728 /* We don't want to abort following commands, restore did_emsg. */
22729 save_did_emsg = did_emsg;
22730 EMSG((char_u *)ga.ga_data);
22731 if (!force_abort)
22732 did_emsg = save_did_emsg;
22733 }
22734 else if (eap->cmdidx == CMD_execute)
22735 do_cmdline((char_u *)ga.ga_data,
22736 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22737 }
22738
22739 ga_clear(&ga);
22740
22741 if (eap->skip)
22742 --emsg_skip;
22743
22744 eap->nextcmd = check_nextcmd(arg);
22745}
22746
22747/*
22748 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22749 * "arg" points to the "&" or '+' when called, to "option" when returning.
22750 * Returns NULL when no option name found. Otherwise pointer to the char
22751 * after the option name.
22752 */
22753 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022754find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755{
22756 char_u *p = *arg;
22757
22758 ++p;
22759 if (*p == 'g' && p[1] == ':')
22760 {
22761 *opt_flags = OPT_GLOBAL;
22762 p += 2;
22763 }
22764 else if (*p == 'l' && p[1] == ':')
22765 {
22766 *opt_flags = OPT_LOCAL;
22767 p += 2;
22768 }
22769 else
22770 *opt_flags = 0;
22771
22772 if (!ASCII_ISALPHA(*p))
22773 return NULL;
22774 *arg = p;
22775
22776 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22777 p += 4; /* termcap option */
22778 else
22779 while (ASCII_ISALPHA(*p))
22780 ++p;
22781 return p;
22782}
22783
22784/*
22785 * ":function"
22786 */
22787 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022788ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022789{
22790 char_u *theline;
22791 int j;
22792 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022793 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022794 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022795 char_u *name = NULL;
22796 char_u *p;
22797 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022798 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022799 garray_T newargs;
22800 garray_T newlines;
22801 int varargs = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022802 int flags = 0;
22803 ufunc_T *fp;
22804 int indent;
22805 int nesting;
22806 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022807 dictitem_T *v;
22808 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022809 static int func_nr = 0; /* number for nameless function */
22810 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022811 hashtab_T *ht;
22812 int todo;
22813 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022814 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022815
22816 /*
22817 * ":function" without argument: list functions.
22818 */
22819 if (ends_excmd(*eap->arg))
22820 {
22821 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022822 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022823 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022824 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022825 {
22826 if (!HASHITEM_EMPTY(hi))
22827 {
22828 --todo;
22829 fp = HI2UF(hi);
22830 if (!isdigit(*fp->uf_name))
22831 list_func_head(fp, FALSE);
22832 }
22833 }
22834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022835 eap->nextcmd = check_nextcmd(eap->arg);
22836 return;
22837 }
22838
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022839 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022840 * ":function /pat": list functions matching pattern.
22841 */
22842 if (*eap->arg == '/')
22843 {
22844 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22845 if (!eap->skip)
22846 {
22847 regmatch_T regmatch;
22848
22849 c = *p;
22850 *p = NUL;
22851 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22852 *p = c;
22853 if (regmatch.regprog != NULL)
22854 {
22855 regmatch.rm_ic = p_ic;
22856
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022857 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022858 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22859 {
22860 if (!HASHITEM_EMPTY(hi))
22861 {
22862 --todo;
22863 fp = HI2UF(hi);
22864 if (!isdigit(*fp->uf_name)
22865 && vim_regexec(&regmatch, fp->uf_name, 0))
22866 list_func_head(fp, FALSE);
22867 }
22868 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022869 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022870 }
22871 }
22872 if (*p == '/')
22873 ++p;
22874 eap->nextcmd = check_nextcmd(p);
22875 return;
22876 }
22877
22878 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022879 * Get the function name. There are these situations:
22880 * func normal function name
22881 * "name" == func, "fudi.fd_dict" == NULL
22882 * dict.func new dictionary entry
22883 * "name" == NULL, "fudi.fd_dict" set,
22884 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22885 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022886 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022887 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22888 * dict.func existing dict entry that's not a Funcref
22889 * "name" == NULL, "fudi.fd_dict" set,
22890 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022891 * s:func script-local function name
22892 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022894 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010022895 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022896 paren = (vim_strchr(p, '(') != NULL);
22897 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022898 {
22899 /*
22900 * Return on an invalid expression in braces, unless the expression
22901 * evaluation has been cancelled due to an aborting error, an
22902 * interrupt, or an exception.
22903 */
22904 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022905 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022906 if (!eap->skip && fudi.fd_newkey != NULL)
22907 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022908 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022909 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022911 else
22912 eap->skip = TRUE;
22913 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022914
Bram Moolenaar071d4272004-06-13 20:20:40 +000022915 /* An error in a function call during evaluation of an expression in magic
22916 * braces should not cause the function not to be defined. */
22917 saved_did_emsg = did_emsg;
22918 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919
22920 /*
22921 * ":function func" with only function name: list function.
22922 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022923 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022924 {
22925 if (!ends_excmd(*skipwhite(p)))
22926 {
22927 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022928 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022929 }
22930 eap->nextcmd = check_nextcmd(p);
22931 if (eap->nextcmd != NULL)
22932 *p = NUL;
22933 if (!eap->skip && !got_int)
22934 {
22935 fp = find_func(name);
22936 if (fp != NULL)
22937 {
22938 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022939 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022940 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022941 if (FUNCLINE(fp, j) == NULL)
22942 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022943 msg_putchar('\n');
22944 msg_outnum((long)(j + 1));
22945 if (j < 9)
22946 msg_putchar(' ');
22947 if (j < 99)
22948 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022949 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 out_flush(); /* show a line at a time */
22951 ui_breakcheck();
22952 }
22953 if (!got_int)
22954 {
22955 msg_putchar('\n');
22956 msg_puts((char_u *)" endfunction");
22957 }
22958 }
22959 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022960 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022962 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022963 }
22964
22965 /*
22966 * ":function name(arg1, arg2)" Define function.
22967 */
22968 p = skipwhite(p);
22969 if (*p != '(')
22970 {
22971 if (!eap->skip)
22972 {
22973 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022974 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022975 }
22976 /* attempt to continue by skipping some text */
22977 if (vim_strchr(p, '(') != NULL)
22978 p = vim_strchr(p, '(');
22979 }
22980 p = skipwhite(p + 1);
22981
Bram Moolenaar071d4272004-06-13 20:20:40 +000022982 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22983
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022984 if (!eap->skip)
22985 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022986 /* Check the name of the function. Unless it's a dictionary function
22987 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022988 if (name != NULL)
22989 arg = name;
22990 else
22991 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022992 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010022993 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
22994 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022995 {
22996 if (*arg == K_SPECIAL)
22997 j = 3;
22998 else
22999 j = 0;
23000 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23001 : eval_isnamec(arg[j])))
23002 ++j;
23003 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023004 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023005 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023006 /* Disallow using the g: dict. */
23007 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23008 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023009 }
23010
Bram Moolenaar069c1e72016-07-15 21:25:08 +020023011 if (get_function_args(&p, ')', &newargs, &varargs, eap->skip) == FAIL)
23012 goto errret_2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023013
Bram Moolenaare9a41262005-01-15 22:18:47 +000023014 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023015 for (;;)
23016 {
23017 p = skipwhite(p);
23018 if (STRNCMP(p, "range", 5) == 0)
23019 {
23020 flags |= FC_RANGE;
23021 p += 5;
23022 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023023 else if (STRNCMP(p, "dict", 4) == 0)
23024 {
23025 flags |= FC_DICT;
23026 p += 4;
23027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023028 else if (STRNCMP(p, "abort", 5) == 0)
23029 {
23030 flags |= FC_ABORT;
23031 p += 5;
23032 }
23033 else
23034 break;
23035 }
23036
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023037 /* When there is a line break use what follows for the function body.
23038 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23039 if (*p == '\n')
23040 line_arg = p + 1;
23041 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023042 EMSG(_(e_trailing));
23043
23044 /*
23045 * Read the body of the function, until ":endfunction" is found.
23046 */
23047 if (KeyTyped)
23048 {
23049 /* Check if the function already exists, don't let the user type the
23050 * whole function before telling him it doesn't work! For a script we
23051 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023052 if (!eap->skip && !eap->forceit)
23053 {
23054 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23055 EMSG(_(e_funcdict));
23056 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023057 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023059
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023060 if (!eap->skip && did_emsg)
23061 goto erret;
23062
Bram Moolenaar071d4272004-06-13 20:20:40 +000023063 msg_putchar('\n'); /* don't overwrite the function name */
23064 cmdline_row = msg_row;
23065 }
23066
23067 indent = 2;
23068 nesting = 0;
23069 for (;;)
23070 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023071 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023072 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023073 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023074 saved_wait_return = FALSE;
23075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023076 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023077 sourcing_lnum_off = sourcing_lnum;
23078
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023079 if (line_arg != NULL)
23080 {
23081 /* Use eap->arg, split up in parts by line breaks. */
23082 theline = line_arg;
23083 p = vim_strchr(theline, '\n');
23084 if (p == NULL)
23085 line_arg += STRLEN(line_arg);
23086 else
23087 {
23088 *p = NUL;
23089 line_arg = p + 1;
23090 }
23091 }
23092 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023093 theline = getcmdline(':', 0L, indent);
23094 else
23095 theline = eap->getline(':', eap->cookie, indent);
23096 if (KeyTyped)
23097 lines_left = Rows - 1;
23098 if (theline == NULL)
23099 {
23100 EMSG(_("E126: Missing :endfunction"));
23101 goto erret;
23102 }
23103
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023104 /* Detect line continuation: sourcing_lnum increased more than one. */
23105 if (sourcing_lnum > sourcing_lnum_off + 1)
23106 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23107 else
23108 sourcing_lnum_off = 0;
23109
Bram Moolenaar071d4272004-06-13 20:20:40 +000023110 if (skip_until != NULL)
23111 {
23112 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23113 * don't check for ":endfunc". */
23114 if (STRCMP(theline, skip_until) == 0)
23115 {
23116 vim_free(skip_until);
23117 skip_until = NULL;
23118 }
23119 }
23120 else
23121 {
23122 /* skip ':' and blanks*/
23123 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23124 ;
23125
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023126 /* Check for "endfunction". */
23127 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023128 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023129 if (line_arg == NULL)
23130 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023131 break;
23132 }
23133
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023134 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023135 * at "end". */
23136 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23137 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023138 else if (STRNCMP(p, "if", 2) == 0
23139 || STRNCMP(p, "wh", 2) == 0
23140 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023141 || STRNCMP(p, "try", 3) == 0)
23142 indent += 2;
23143
23144 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023145 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023146 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023147 if (*p == '!')
23148 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023149 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010023150 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010023151 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023152 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023153 ++nesting;
23154 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155 }
23156 }
23157
23158 /* Check for ":append" or ":insert". */
23159 p = skip_range(p, NULL);
23160 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23161 || (p[0] == 'i'
23162 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23163 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23164 skip_until = vim_strsave((char_u *)".");
23165
23166 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23167 arg = skipwhite(skiptowhite(p));
23168 if (arg[0] == '<' && arg[1] =='<'
23169 && ((p[0] == 'p' && p[1] == 'y'
23170 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23171 || (p[0] == 'p' && p[1] == 'e'
23172 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23173 || (p[0] == 't' && p[1] == 'c'
23174 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023175 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23176 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023177 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23178 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023179 || (p[0] == 'm' && p[1] == 'z'
23180 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023181 ))
23182 {
23183 /* ":python <<" continues until a dot, like ":append" */
23184 p = skipwhite(arg + 2);
23185 if (*p == NUL)
23186 skip_until = vim_strsave((char_u *)".");
23187 else
23188 skip_until = vim_strsave(p);
23189 }
23190 }
23191
23192 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023193 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023194 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023195 if (line_arg == NULL)
23196 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023197 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023198 }
23199
23200 /* Copy the line to newly allocated memory. get_one_sourceline()
23201 * allocates 250 bytes per line, this saves 80% on average. The cost
23202 * is an extra alloc/free. */
23203 p = vim_strsave(theline);
23204 if (p != NULL)
23205 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023206 if (line_arg == NULL)
23207 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023208 theline = p;
23209 }
23210
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023211 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23212
23213 /* Add NULL lines for continuation lines, so that the line count is
23214 * equal to the index in the growarray. */
23215 while (sourcing_lnum_off-- > 0)
23216 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023217
23218 /* Check for end of eap->arg. */
23219 if (line_arg != NULL && *line_arg == NUL)
23220 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023221 }
23222
23223 /* Don't define the function when skipping commands or when an error was
23224 * detected. */
23225 if (eap->skip || did_emsg)
23226 goto erret;
23227
23228 /*
23229 * If there are no errors, add the function
23230 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023231 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023232 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023233 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023234 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023235 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023236 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023237 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023238 goto erret;
23239 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023240
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023241 fp = find_func(name);
23242 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023243 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023244 if (!eap->forceit)
23245 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023246 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023247 goto erret;
23248 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023249 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023250 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023251 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023252 name);
23253 goto erret;
23254 }
23255 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023256 ga_clear_strings(&(fp->uf_args));
23257 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023258 vim_free(name);
23259 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023261 }
23262 else
23263 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023264 char numbuf[20];
23265
23266 fp = NULL;
23267 if (fudi.fd_newkey == NULL && !eap->forceit)
23268 {
23269 EMSG(_(e_funcdict));
23270 goto erret;
23271 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023272 if (fudi.fd_di == NULL)
23273 {
23274 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023275 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023276 goto erret;
23277 }
23278 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023279 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023280 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023281
23282 /* Give the function a sequential number. Can only be used with a
23283 * Funcref! */
23284 vim_free(name);
23285 sprintf(numbuf, "%d", ++func_nr);
23286 name = vim_strsave((char_u *)numbuf);
23287 if (name == NULL)
23288 goto erret;
23289 }
23290
23291 if (fp == NULL)
23292 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023293 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023294 {
23295 int slen, plen;
23296 char_u *scriptname;
23297
23298 /* Check that the autoload name matches the script name. */
23299 j = FAIL;
23300 if (sourcing_name != NULL)
23301 {
23302 scriptname = autoload_name(name);
23303 if (scriptname != NULL)
23304 {
23305 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023306 plen = (int)STRLEN(p);
23307 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023308 if (slen > plen && fnamecmp(p,
23309 sourcing_name + slen - plen) == 0)
23310 j = OK;
23311 vim_free(scriptname);
23312 }
23313 }
23314 if (j == FAIL)
23315 {
23316 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23317 goto erret;
23318 }
23319 }
23320
23321 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023322 if (fp == NULL)
23323 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023324
23325 if (fudi.fd_dict != NULL)
23326 {
23327 if (fudi.fd_di == NULL)
23328 {
23329 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023330 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023331 if (fudi.fd_di == NULL)
23332 {
23333 vim_free(fp);
23334 goto erret;
23335 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023336 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23337 {
23338 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023339 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023340 goto erret;
23341 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023342 }
23343 else
23344 /* overwrite existing dict entry */
23345 clear_tv(&fudi.fd_di->di_tv);
23346 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023347 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023348 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023349 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023350
23351 /* behave like "dict" was used */
23352 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023353 }
23354
Bram Moolenaar071d4272004-06-13 20:20:40 +000023355 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023356 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023357 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23358 {
23359 vim_free(fp);
23360 goto erret;
23361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023362 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023363 fp->uf_args = newargs;
23364 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023365#ifdef FEAT_PROFILE
23366 fp->uf_tml_count = NULL;
23367 fp->uf_tml_total = NULL;
23368 fp->uf_tml_self = NULL;
23369 fp->uf_profiling = FALSE;
23370 if (prof_def_func())
23371 func_do_profile(fp);
23372#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023373 fp->uf_varargs = varargs;
23374 fp->uf_flags = flags;
23375 fp->uf_calls = 0;
23376 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023377 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023378
23379erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023380 ga_clear_strings(&newargs);
Bram Moolenaar069c1e72016-07-15 21:25:08 +020023381errret_2:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023382 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023383ret_free:
23384 vim_free(skip_until);
23385 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023386 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023387 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023388 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023389}
23390
23391/*
23392 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023393 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023394 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023395 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023396 * TFN_INT: internal function name OK
23397 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023398 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023399 * Advances "pp" to just after the function name (if no error).
23400 */
23401 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023402trans_function_name(
23403 char_u **pp,
23404 int skip, /* only find the end, don't evaluate */
23405 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010023406 funcdict_T *fdp, /* return: info about dictionary used */
23407 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023409 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023410 char_u *start;
23411 char_u *end;
23412 int lead;
23413 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023414 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023415 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023416
23417 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023418 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023419 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023420
23421 /* Check for hard coded <SNR>: already translated function ID (from a user
23422 * command). */
23423 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23424 && (*pp)[2] == (int)KE_SNR)
23425 {
23426 *pp += 3;
23427 len = get_id_len(pp) + 3;
23428 return vim_strnsave(start, len);
23429 }
23430
23431 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23432 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023433 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023434 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023435 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023436
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023437 /* Note that TFN_ flags use the same values as GLV_ flags. */
23438 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023439 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023440 if (end == start)
23441 {
23442 if (!skip)
23443 EMSG(_("E129: Function name required"));
23444 goto theend;
23445 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023446 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023447 {
23448 /*
23449 * Report an invalid expression in braces, unless the expression
23450 * evaluation has been cancelled due to an aborting error, an
23451 * interrupt, or an exception.
23452 */
23453 if (!aborting())
23454 {
23455 if (end != NULL)
23456 EMSG2(_(e_invarg2), start);
23457 }
23458 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023459 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023460 goto theend;
23461 }
23462
23463 if (lv.ll_tv != NULL)
23464 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023465 if (fdp != NULL)
23466 {
23467 fdp->fd_dict = lv.ll_dict;
23468 fdp->fd_newkey = lv.ll_newkey;
23469 lv.ll_newkey = NULL;
23470 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023471 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023472 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23473 {
23474 name = vim_strsave(lv.ll_tv->vval.v_string);
23475 *pp = end;
23476 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010023477 else if (lv.ll_tv->v_type == VAR_PARTIAL
23478 && lv.ll_tv->vval.v_partial != NULL)
23479 {
23480 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
23481 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010023482 if (partial != NULL)
23483 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010023484 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023485 else
23486 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023487 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23488 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023489 EMSG(_(e_funcref));
23490 else
23491 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023492 name = NULL;
23493 }
23494 goto theend;
23495 }
23496
23497 if (lv.ll_name == NULL)
23498 {
23499 /* Error found, but continue after the function name. */
23500 *pp = end;
23501 goto theend;
23502 }
23503
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023504 /* Check if the name is a Funcref. If so, use the value. */
23505 if (lv.ll_exp_name != NULL)
23506 {
23507 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010023508 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023509 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023510 if (name == lv.ll_exp_name)
23511 name = NULL;
23512 }
23513 else
23514 {
23515 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010023516 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023517 if (name == *pp)
23518 name = NULL;
23519 }
23520 if (name != NULL)
23521 {
23522 name = vim_strsave(name);
23523 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023524 if (STRNCMP(name, "<SNR>", 5) == 0)
23525 {
23526 /* Change "<SNR>" to the byte sequence. */
23527 name[0] = K_SPECIAL;
23528 name[1] = KS_EXTRA;
23529 name[2] = (int)KE_SNR;
23530 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23531 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023532 goto theend;
23533 }
23534
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023535 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023536 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023537 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023538 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23539 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23540 {
23541 /* When there was "s:" already or the name expanded to get a
23542 * leading "s:" then remove it. */
23543 lv.ll_name += 2;
23544 len -= 2;
23545 lead = 2;
23546 }
23547 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023548 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023549 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023550 /* skip over "s:" and "g:" */
23551 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023552 lv.ll_name += 2;
23553 len = (int)(end - lv.ll_name);
23554 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023555
23556 /*
23557 * Copy the function name to allocated memory.
23558 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23559 * Accept <SNR>123_name() outside a script.
23560 */
23561 if (skip)
23562 lead = 0; /* do nothing */
23563 else if (lead > 0)
23564 {
23565 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023566 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23567 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023568 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023569 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023570 if (current_SID <= 0)
23571 {
23572 EMSG(_(e_usingsid));
23573 goto theend;
23574 }
23575 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23576 lead += (int)STRLEN(sid_buf);
23577 }
23578 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023579 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023580 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023581 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023582 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023583 goto theend;
23584 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023585 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023586 {
23587 char_u *cp = vim_strchr(lv.ll_name, ':');
23588
23589 if (cp != NULL && cp < end)
23590 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023591 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023592 goto theend;
23593 }
23594 }
23595
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023596 name = alloc((unsigned)(len + lead + 1));
23597 if (name != NULL)
23598 {
23599 if (lead > 0)
23600 {
23601 name[0] = K_SPECIAL;
23602 name[1] = KS_EXTRA;
23603 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023604 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023605 STRCPY(name + 3, sid_buf);
23606 }
23607 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023608 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023609 }
23610 *pp = end;
23611
23612theend:
23613 clear_lval(&lv);
23614 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023615}
23616
23617/*
23618 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23619 * Return 2 if "p" starts with "s:".
23620 * Return 0 otherwise.
23621 */
23622 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023623eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023624{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023625 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23626 * the standard library function. */
23627 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23628 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023629 return 5;
23630 if (p[0] == 's' && p[1] == ':')
23631 return 2;
23632 return 0;
23633}
23634
23635/*
23636 * Return TRUE if "p" starts with "<SID>" or "s:".
23637 * Only works if eval_fname_script() returned non-zero for "p"!
23638 */
23639 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023640eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023641{
23642 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23643}
23644
23645/*
23646 * List the head of the function: "name(arg1, arg2)".
23647 */
23648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023649list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023650{
23651 int j;
23652
23653 msg_start();
23654 if (indent)
23655 MSG_PUTS(" ");
23656 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023657 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023658 {
23659 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023660 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023661 }
23662 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023663 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023664 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023665 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023666 {
23667 if (j)
23668 MSG_PUTS(", ");
23669 msg_puts(FUNCARG(fp, j));
23670 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023671 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023672 {
23673 if (j)
23674 MSG_PUTS(", ");
23675 MSG_PUTS("...");
23676 }
23677 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023678 if (fp->uf_flags & FC_ABORT)
23679 MSG_PUTS(" abort");
23680 if (fp->uf_flags & FC_RANGE)
23681 MSG_PUTS(" range");
23682 if (fp->uf_flags & FC_DICT)
23683 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023684 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023685 if (p_verbose > 0)
23686 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023687}
23688
23689/*
23690 * Find a function by name, return pointer to it in ufuncs.
23691 * Return NULL for unknown function.
23692 */
23693 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023694find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023695{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023696 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023697
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023698 hi = hash_find(&func_hashtab, name);
23699 if (!HASHITEM_EMPTY(hi))
23700 return HI2UF(hi);
23701 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023702}
23703
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023704#if defined(EXITFREE) || defined(PROTO)
23705 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023706free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023707{
23708 hashitem_T *hi;
23709
23710 /* Need to start all over every time, because func_free() may change the
23711 * hash table. */
23712 while (func_hashtab.ht_used > 0)
23713 for (hi = func_hashtab.ht_array; ; ++hi)
23714 if (!HASHITEM_EMPTY(hi))
23715 {
23716 func_free(HI2UF(hi));
23717 break;
23718 }
23719}
23720#endif
23721
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023722 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023723translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023724{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023725 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023726 return find_internal_func(name) >= 0;
23727 return find_func(name) != NULL;
23728}
23729
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023730/*
23731 * Return TRUE if a function "name" exists.
23732 */
23733 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023734function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023735{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023736 char_u *nm = name;
23737 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023738 int n = FALSE;
23739
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023740 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010023741 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023742 nm = skipwhite(nm);
23743
23744 /* Only accept "funcname", "funcname ", "funcname (..." and
23745 * "funcname(...", not "funcname!...". */
23746 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023747 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023748 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023749 return n;
23750}
23751
Bram Moolenaara1544c02013-05-30 12:35:52 +020023752 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023753get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023754{
23755 char_u *nm = name;
23756 char_u *p;
23757
Bram Moolenaar65639032016-03-16 21:40:30 +010023758 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020023759
23760 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023761 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023762 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023763
Bram Moolenaara1544c02013-05-30 12:35:52 +020023764 vim_free(p);
23765 return NULL;
23766}
23767
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023768/*
23769 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023770 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23771 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023772 */
23773 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023774builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023775{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023776 char_u *p;
23777
23778 if (!ASCII_ISLOWER(name[0]))
23779 return FALSE;
23780 p = vim_strchr(name, AUTOLOAD_CHAR);
23781 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023782}
23783
Bram Moolenaar05159a02005-02-26 23:04:13 +000023784#if defined(FEAT_PROFILE) || defined(PROTO)
23785/*
23786 * Start profiling function "fp".
23787 */
23788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023789func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023790{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023791 int len = fp->uf_lines.ga_len;
23792
23793 if (len == 0)
23794 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023795 fp->uf_tm_count = 0;
23796 profile_zero(&fp->uf_tm_self);
23797 profile_zero(&fp->uf_tm_total);
23798 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023799 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023800 if (fp->uf_tml_total == NULL)
23801 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023802 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023803 if (fp->uf_tml_self == NULL)
23804 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023805 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023806 fp->uf_tml_idx = -1;
23807 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23808 || fp->uf_tml_self == NULL)
23809 return; /* out of memory */
23810
23811 fp->uf_profiling = TRUE;
23812}
23813
23814/*
23815 * Dump the profiling results for all functions in file "fd".
23816 */
23817 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023818func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023819{
23820 hashitem_T *hi;
23821 int todo;
23822 ufunc_T *fp;
23823 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023824 ufunc_T **sorttab;
23825 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023826
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023827 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023828 if (todo == 0)
23829 return; /* nothing to dump */
23830
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023831 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023832
Bram Moolenaar05159a02005-02-26 23:04:13 +000023833 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23834 {
23835 if (!HASHITEM_EMPTY(hi))
23836 {
23837 --todo;
23838 fp = HI2UF(hi);
23839 if (fp->uf_profiling)
23840 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023841 if (sorttab != NULL)
23842 sorttab[st_len++] = fp;
23843
Bram Moolenaar05159a02005-02-26 23:04:13 +000023844 if (fp->uf_name[0] == K_SPECIAL)
23845 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23846 else
23847 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23848 if (fp->uf_tm_count == 1)
23849 fprintf(fd, "Called 1 time\n");
23850 else
23851 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23852 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23853 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23854 fprintf(fd, "\n");
23855 fprintf(fd, "count total (s) self (s)\n");
23856
23857 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23858 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023859 if (FUNCLINE(fp, i) == NULL)
23860 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023861 prof_func_line(fd, fp->uf_tml_count[i],
23862 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023863 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23864 }
23865 fprintf(fd, "\n");
23866 }
23867 }
23868 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023869
23870 if (sorttab != NULL && st_len > 0)
23871 {
23872 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23873 prof_total_cmp);
23874 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23875 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23876 prof_self_cmp);
23877 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23878 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023879
23880 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023881}
Bram Moolenaar73830342005-02-28 22:48:19 +000023882
23883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023884prof_sort_list(
23885 FILE *fd,
23886 ufunc_T **sorttab,
23887 int st_len,
23888 char *title,
23889 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023890{
23891 int i;
23892 ufunc_T *fp;
23893
23894 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23895 fprintf(fd, "count total (s) self (s) function\n");
23896 for (i = 0; i < 20 && i < st_len; ++i)
23897 {
23898 fp = sorttab[i];
23899 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23900 prefer_self);
23901 if (fp->uf_name[0] == K_SPECIAL)
23902 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23903 else
23904 fprintf(fd, " %s()\n", fp->uf_name);
23905 }
23906 fprintf(fd, "\n");
23907}
23908
23909/*
23910 * Print the count and times for one function or function line.
23911 */
23912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023913prof_func_line(
23914 FILE *fd,
23915 int count,
23916 proftime_T *total,
23917 proftime_T *self,
23918 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023919{
23920 if (count > 0)
23921 {
23922 fprintf(fd, "%5d ", count);
23923 if (prefer_self && profile_equal(total, self))
23924 fprintf(fd, " ");
23925 else
23926 fprintf(fd, "%s ", profile_msg(total));
23927 if (!prefer_self && profile_equal(total, self))
23928 fprintf(fd, " ");
23929 else
23930 fprintf(fd, "%s ", profile_msg(self));
23931 }
23932 else
23933 fprintf(fd, " ");
23934}
23935
23936/*
23937 * Compare function for total time sorting.
23938 */
23939 static int
23940#ifdef __BORLANDC__
23941_RTLENTRYF
23942#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023943prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023944{
23945 ufunc_T *p1, *p2;
23946
23947 p1 = *(ufunc_T **)s1;
23948 p2 = *(ufunc_T **)s2;
23949 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23950}
23951
23952/*
23953 * Compare function for self time sorting.
23954 */
23955 static int
23956#ifdef __BORLANDC__
23957_RTLENTRYF
23958#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023959prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023960{
23961 ufunc_T *p1, *p2;
23962
23963 p1 = *(ufunc_T **)s1;
23964 p2 = *(ufunc_T **)s2;
23965 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23966}
23967
Bram Moolenaar05159a02005-02-26 23:04:13 +000023968#endif
23969
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023970/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023971 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023972 * Return TRUE if a package was loaded.
23973 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023974 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023975script_autoload(
23976 char_u *name,
23977 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023978{
23979 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023980 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023981 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023982 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023983
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023984 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023985 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023986 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023987 return FALSE;
23988
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023989 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023990
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023991 /* Find the name in the list of previously loaded package names. Skip
23992 * "autoload/", it's always the same. */
23993 for (i = 0; i < ga_loaded.ga_len; ++i)
23994 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23995 break;
23996 if (!reload && i < ga_loaded.ga_len)
23997 ret = FALSE; /* was loaded already */
23998 else
23999 {
24000 /* Remember the name if it wasn't loaded already. */
24001 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24002 {
24003 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24004 tofree = NULL;
24005 }
24006
24007 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024008 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024009 ret = TRUE;
24010 }
24011
24012 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024013 return ret;
24014}
24015
24016/*
24017 * Return the autoload script name for a function or variable name.
24018 * Returns NULL when out of memory.
24019 */
24020 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024021autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024022{
24023 char_u *p;
24024 char_u *scriptname;
24025
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024026 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024027 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24028 if (scriptname == NULL)
24029 return FALSE;
24030 STRCPY(scriptname, "autoload/");
24031 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024032 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024033 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024034 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024035 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024036 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024037}
24038
Bram Moolenaar071d4272004-06-13 20:20:40 +000024039#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24040
24041/*
24042 * Function given to ExpandGeneric() to obtain the list of user defined
24043 * function names.
24044 */
24045 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024046get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024047{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024048 static long_u done;
24049 static hashitem_T *hi;
24050 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024051
24052 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024053 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024054 done = 0;
24055 hi = func_hashtab.ht_array;
24056 }
24057 if (done < func_hashtab.ht_used)
24058 {
24059 if (done++ > 0)
24060 ++hi;
24061 while (HASHITEM_EMPTY(hi))
24062 ++hi;
24063 fp = HI2UF(hi);
24064
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024065 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024066 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024067
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024068 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24069 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024070
24071 cat_func_name(IObuff, fp);
24072 if (xp->xp_context != EXPAND_USER_FUNC)
24073 {
24074 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024075 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024076 STRCAT(IObuff, ")");
24077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024078 return IObuff;
24079 }
24080 return NULL;
24081}
24082
24083#endif /* FEAT_CMDL_COMPL */
24084
24085/*
24086 * Copy the function name of "fp" to buffer "buf".
24087 * "buf" must be able to hold the function name plus three bytes.
24088 * Takes care of script-local function names.
24089 */
24090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024091cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024092{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024093 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024094 {
24095 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024096 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024097 }
24098 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024099 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024100}
24101
24102/*
24103 * ":delfunction {name}"
24104 */
24105 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024106ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024107{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024108 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024109 char_u *p;
24110 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024111 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024112
24113 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024114 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024115 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024116 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024117 {
24118 if (fudi.fd_dict != NULL && !eap->skip)
24119 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024120 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024122 if (!ends_excmd(*skipwhite(p)))
24123 {
24124 vim_free(name);
24125 EMSG(_(e_trailing));
24126 return;
24127 }
24128 eap->nextcmd = check_nextcmd(p);
24129 if (eap->nextcmd != NULL)
24130 *p = NUL;
24131
24132 if (!eap->skip)
24133 fp = find_func(name);
24134 vim_free(name);
24135
24136 if (!eap->skip)
24137 {
24138 if (fp == NULL)
24139 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024140 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024141 return;
24142 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024143 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024144 {
24145 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24146 return;
24147 }
24148
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024149 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024150 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024151 /* Delete the dict item that refers to the function, it will
24152 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024153 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024154 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024155 else
24156 func_free(fp);
24157 }
24158}
24159
24160/*
24161 * Free a function and remove it from the list of functions.
24162 */
24163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024164func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024165{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024166 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024167
24168 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024169 ga_clear_strings(&(fp->uf_args));
24170 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024171#ifdef FEAT_PROFILE
24172 vim_free(fp->uf_tml_count);
24173 vim_free(fp->uf_tml_total);
24174 vim_free(fp->uf_tml_self);
24175#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024176
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024177 /* remove the function from the function hashtable */
24178 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24179 if (HASHITEM_EMPTY(hi))
24180 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024181 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024182 hash_remove(&func_hashtab, hi);
24183
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024184 vim_free(fp);
24185}
24186
24187/*
24188 * Unreference a Function: decrement the reference count and free it when it
24189 * becomes zero. Only for numbered functions.
24190 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024191 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024192func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024193{
24194 ufunc_T *fp;
24195
Bram Moolenaar069c1e72016-07-15 21:25:08 +020024196 if (name == NULL)
24197 return;
24198 else if (isdigit(*name))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024199 {
24200 fp = find_func(name);
24201 if (fp == NULL)
Bram Moolenaara9673212016-06-01 22:21:06 +020024202 {
Bram Moolenaarb89a25f2016-06-01 23:08:39 +020024203#ifdef EXITFREE
24204 if (!entered_free_all_mem)
24205#endif
Bram Moolenaara9673212016-06-01 22:21:06 +020024206 EMSG2(_(e_intern2), "func_unref()");
24207 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024208 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024209 {
24210 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024211 * when "uf_calls" becomes zero. */
24212 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024213 func_free(fp);
24214 }
24215 }
Bram Moolenaar069c1e72016-07-15 21:25:08 +020024216 else if (STRNCMP(name, "<lambda>", 8) == 0)
24217 {
24218 /* fail silently, when lambda function isn't found. */
24219 fp = find_func(name);
24220 if (fp != NULL && --fp->uf_refcount <= 0)
24221 {
24222 /* Only delete it when it's not being used. Otherwise it's done
24223 * when "uf_calls" becomes zero. */
24224 if (fp->uf_calls == 0)
24225 func_free(fp);
24226 }
24227 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024228}
24229
24230/*
24231 * Count a reference to a Function.
24232 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024233 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024234func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024235{
24236 ufunc_T *fp;
24237
Bram Moolenaar069c1e72016-07-15 21:25:08 +020024238 if (name == NULL)
24239 return;
24240 else if (isdigit(*name))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024241 {
24242 fp = find_func(name);
24243 if (fp == NULL)
24244 EMSG2(_(e_intern2), "func_ref()");
24245 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024246 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024247 }
Bram Moolenaar069c1e72016-07-15 21:25:08 +020024248 else if (STRNCMP(name, "<lambda>", 8) == 0)
24249 {
24250 /* fail silently, when lambda function isn't found. */
24251 fp = find_func(name);
24252 if (fp != NULL)
24253 ++fp->uf_refcount;
24254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024255}
24256
24257/*
24258 * Call a user function.
24259 */
24260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024261call_user_func(
24262 ufunc_T *fp, /* pointer to function */
24263 int argcount, /* nr of args */
24264 typval_T *argvars, /* arguments */
24265 typval_T *rettv, /* return value */
24266 linenr_T firstline, /* first line of range */
24267 linenr_T lastline, /* last line of range */
24268 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024269{
Bram Moolenaar33570922005-01-25 22:26:29 +000024270 char_u *save_sourcing_name;
24271 linenr_T save_sourcing_lnum;
24272 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024273 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024274 int save_did_emsg;
24275 static int depth = 0;
24276 dictitem_T *v;
24277 int fixvar_idx = 0; /* index in fixvar[] */
24278 int i;
24279 int ai;
Bram Moolenaar069c1e72016-07-15 21:25:08 +020024280 int islambda = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000024281 char_u numbuf[NUMBUFLEN];
24282 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024283 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024284#ifdef FEAT_PROFILE
24285 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024286 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024288
24289 /* If depth of calling is getting too high, don't execute the function */
24290 if (depth >= p_mfd)
24291 {
24292 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024293 rettv->v_type = VAR_NUMBER;
24294 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024295 return;
24296 }
24297 ++depth;
24298
24299 line_breakcheck(); /* check for CTRL-C hit */
24300
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024301 fc = (funccall_T *)alloc(sizeof(funccall_T));
24302 fc->caller = current_funccal;
24303 current_funccal = fc;
24304 fc->func = fp;
24305 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024306 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024307 fc->linenr = 0;
24308 fc->returned = FALSE;
24309 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024310 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024311 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24312 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024313
Bram Moolenaar069c1e72016-07-15 21:25:08 +020024314 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
24315 islambda = TRUE;
24316
Bram Moolenaar33570922005-01-25 22:26:29 +000024317 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024318 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024319 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24320 * each argument variable and saves a lot of time.
24321 */
24322 /*
24323 * Init l: variables.
24324 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024325 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024326 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024327 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024328 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24329 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024330 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024331 name = v->di_key;
24332 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024333 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024334 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024335 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024336 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024337 v->di_tv.vval.v_dict = selfdict;
24338 ++selfdict->dv_refcount;
24339 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024340
Bram Moolenaar33570922005-01-25 22:26:29 +000024341 /*
24342 * Init a: variables.
24343 * Set a:0 to "argcount".
24344 * Set a:000 to a list with room for the "..." arguments.
24345 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024346 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024347 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024348 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024349 /* Use "name" to avoid a warning from some compiler that checks the
24350 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024351 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024352 name = v->di_key;
24353 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024354 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024355 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024356 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024357 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024358 v->di_tv.vval.v_list = &fc->l_varlist;
24359 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24360 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24361 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024362
24363 /*
24364 * Set a:firstline to "firstline" and a:lastline to "lastline".
24365 * Set a:name to named arguments.
24366 * Set a:N to the "..." arguments.
24367 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024368 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024369 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024370 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024371 (varnumber_T)lastline);
24372 for (i = 0; i < argcount; ++i)
24373 {
Bram Moolenaar069c1e72016-07-15 21:25:08 +020024374 int addlocal = FALSE;
24375 dictitem_T *v2;
24376
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024377 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024378 if (ai < 0)
Bram Moolenaar069c1e72016-07-15 21:25:08 +020024379 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024380 /* named argument a:name */
24381 name = FUNCARG(fp, i);
Bram Moolenaar069c1e72016-07-15 21:25:08 +020024382 if (islambda)
24383 addlocal = TRUE;
24384 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024385 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024386 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024387 /* "..." argument a:1, a:2, etc. */
24388 sprintf((char *)numbuf, "%d", ai + 1);
24389 name = numbuf;
24390 }
24391 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24392 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024393 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024394 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar069c1e72016-07-15 21:25:08 +020024395
24396 if (addlocal)
24397 v2 = v;
Bram Moolenaar33570922005-01-25 22:26:29 +000024398 }
24399 else
24400 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024401 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24402 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024403 if (v == NULL)
24404 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024405 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar069c1e72016-07-15 21:25:08 +020024406
24407 if (addlocal)
24408 {
24409 v2 = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24410 + STRLEN(name)));
24411 if (v2 == NULL)
24412 {
24413 vim_free(v);
24414 break;
24415 }
24416 v2->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
24417 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024418 }
24419 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024420 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024421
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024422 /* Note: the values are copied directly to avoid alloc/free.
24423 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024424 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024425 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024426
Bram Moolenaar069c1e72016-07-15 21:25:08 +020024427 /* Named arguments can be accessed without the "a:" prefix in lambda
24428 * expressions. Add to the l: dict. */
24429 if (addlocal)
24430 {
24431 STRCPY(v2->di_key, name);
24432 copy_tv(&v->di_tv, &v2->di_tv);
24433 v2->di_tv.v_lock = VAR_FIXED;
24434 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v2));
24435 }
24436
Bram Moolenaar33570922005-01-25 22:26:29 +000024437 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24438 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024439 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24440 fc->l_listitems[ai].li_tv = argvars[i];
24441 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024442 }
24443 }
24444
Bram Moolenaar071d4272004-06-13 20:20:40 +000024445 /* Don't redraw while executing the function. */
24446 ++RedrawingDisabled;
24447 save_sourcing_name = sourcing_name;
24448 save_sourcing_lnum = sourcing_lnum;
24449 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024450 /* need space for function name + ("function " + 3) or "[number]" */
24451 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24452 + STRLEN(fp->uf_name) + 20;
24453 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024454 if (sourcing_name != NULL)
24455 {
24456 if (save_sourcing_name != NULL
24457 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024458 sprintf((char *)sourcing_name, "%s[%d]..",
24459 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024460 else
24461 STRCPY(sourcing_name, "function ");
24462 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24463
24464 if (p_verbose >= 12)
24465 {
24466 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024467 verbose_enter_scroll();
24468
Bram Moolenaar555b2802005-05-19 21:08:39 +000024469 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024470 if (p_verbose >= 14)
24471 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024472 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024473 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024474 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024475 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024476
24477 msg_puts((char_u *)"(");
24478 for (i = 0; i < argcount; ++i)
24479 {
24480 if (i > 0)
24481 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024482 if (argvars[i].v_type == VAR_NUMBER)
24483 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024484 else
24485 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024486 /* Do not want errors such as E724 here. */
24487 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024488 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024489 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024490 if (s != NULL)
24491 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024492 if (vim_strsize(s) > MSG_BUF_CLEN)
24493 {
24494 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24495 s = buf;
24496 }
24497 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024498 vim_free(tofree);
24499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024500 }
24501 }
24502 msg_puts((char_u *)")");
24503 }
24504 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024505
24506 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024507 --no_wait_return;
24508 }
24509 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024510#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024511 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024512 {
24513 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24514 func_do_profile(fp);
24515 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024516 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024517 {
24518 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024519 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024520 profile_zero(&fp->uf_tm_children);
24521 }
24522 script_prof_save(&wait_start);
24523 }
24524#endif
24525
Bram Moolenaar071d4272004-06-13 20:20:40 +000024526 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024527 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024528 save_did_emsg = did_emsg;
24529 did_emsg = FALSE;
24530
24531 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024532 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024533 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24534
24535 --RedrawingDisabled;
24536
24537 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024538 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024539 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024540 clear_tv(rettv);
24541 rettv->v_type = VAR_NUMBER;
24542 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024543 }
24544
Bram Moolenaar05159a02005-02-26 23:04:13 +000024545#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024546 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024547 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024548 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024549 profile_end(&call_start);
24550 profile_sub_wait(&wait_start, &call_start);
24551 profile_add(&fp->uf_tm_total, &call_start);
24552 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024553 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024554 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024555 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24556 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024557 }
24558 }
24559#endif
24560
Bram Moolenaar071d4272004-06-13 20:20:40 +000024561 /* when being verbose, mention the return value */
24562 if (p_verbose >= 12)
24563 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024564 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024565 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024566
Bram Moolenaar071d4272004-06-13 20:20:40 +000024567 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024568 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024569 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024570 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024571 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024572 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024573 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024574 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024575 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024576 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024577 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024578
Bram Moolenaar555b2802005-05-19 21:08:39 +000024579 /* The value may be very long. Skip the middle part, so that we
24580 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024581 * truncate it at the end. Don't want errors such as E724 here. */
24582 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024583 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024584 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024585 if (s != NULL)
24586 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024587 if (vim_strsize(s) > MSG_BUF_CLEN)
24588 {
24589 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24590 s = buf;
24591 }
24592 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024593 vim_free(tofree);
24594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024595 }
24596 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024597
24598 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024599 --no_wait_return;
24600 }
24601
24602 vim_free(sourcing_name);
24603 sourcing_name = save_sourcing_name;
24604 sourcing_lnum = save_sourcing_lnum;
24605 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024606#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024607 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024608 script_prof_restore(&wait_start);
24609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024610
24611 if (p_verbose >= 12 && sourcing_name != NULL)
24612 {
24613 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024614 verbose_enter_scroll();
24615
Bram Moolenaar555b2802005-05-19 21:08:39 +000024616 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024617 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024618
24619 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024620 --no_wait_return;
24621 }
24622
24623 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024624 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024625 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024626
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024627 /* If the a:000 list and the l: and a: dicts are not referenced we can
24628 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024629 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24630 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24631 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24632 {
24633 free_funccal(fc, FALSE);
24634 }
24635 else
24636 {
24637 hashitem_T *hi;
24638 listitem_T *li;
24639 int todo;
24640
24641 /* "fc" is still in use. This can happen when returning "a:000" or
24642 * assigning "l:" to a global variable.
24643 * Link "fc" in the list for garbage collection later. */
24644 fc->caller = previous_funccal;
24645 previous_funccal = fc;
24646
24647 /* Make a copy of the a: variables, since we didn't do that above. */
24648 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24649 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24650 {
24651 if (!HASHITEM_EMPTY(hi))
24652 {
24653 --todo;
24654 v = HI2DI(hi);
24655 copy_tv(&v->di_tv, &v->di_tv);
24656 }
24657 }
24658
24659 /* Make a copy of the a:000 items, since we didn't do that above. */
24660 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24661 copy_tv(&li->li_tv, &li->li_tv);
24662 }
24663}
24664
24665/*
24666 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024667 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024668 */
24669 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024670can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024671{
24672 return (fc->l_varlist.lv_copyID != copyID
24673 && fc->l_vars.dv_copyID != copyID
24674 && fc->l_avars.dv_copyID != copyID);
24675}
24676
24677/*
24678 * Free "fc" and what it contains.
24679 */
24680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024681free_funccal(
24682 funccall_T *fc,
24683 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024684{
24685 listitem_T *li;
24686
24687 /* The a: variables typevals may not have been allocated, only free the
24688 * allocated variables. */
24689 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24690
24691 /* free all l: variables */
24692 vars_clear(&fc->l_vars.dv_hashtab);
24693
24694 /* Free the a:000 variables if they were allocated. */
24695 if (free_val)
24696 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24697 clear_tv(&li->li_tv);
24698
24699 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024700}
24701
24702/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024703 * Add a number variable "name" to dict "dp" with value "nr".
24704 */
24705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024706add_nr_var(
24707 dict_T *dp,
24708 dictitem_T *v,
24709 char *name,
24710 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024711{
24712 STRCPY(v->di_key, name);
24713 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24714 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24715 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024716 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024717 v->di_tv.vval.v_number = nr;
24718}
24719
24720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024721 * ":return [expr]"
24722 */
24723 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024724ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024725{
24726 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024727 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024728 int returning = FALSE;
24729
24730 if (current_funccal == NULL)
24731 {
24732 EMSG(_("E133: :return not inside a function"));
24733 return;
24734 }
24735
24736 if (eap->skip)
24737 ++emsg_skip;
24738
24739 eap->nextcmd = NULL;
24740 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024741 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024742 {
24743 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024744 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024745 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024746 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024747 }
24748 /* It's safer to return also on error. */
24749 else if (!eap->skip)
24750 {
24751 /*
24752 * Return unless the expression evaluation has been cancelled due to an
24753 * aborting error, an interrupt, or an exception.
24754 */
24755 if (!aborting())
24756 returning = do_return(eap, FALSE, TRUE, NULL);
24757 }
24758
24759 /* When skipping or the return gets pending, advance to the next command
24760 * in this line (!returning). Otherwise, ignore the rest of the line.
24761 * Following lines will be ignored by get_func_line(). */
24762 if (returning)
24763 eap->nextcmd = NULL;
24764 else if (eap->nextcmd == NULL) /* no argument */
24765 eap->nextcmd = check_nextcmd(arg);
24766
24767 if (eap->skip)
24768 --emsg_skip;
24769}
24770
24771/*
24772 * Return from a function. Possibly makes the return pending. Also called
24773 * for a pending return at the ":endtry" or after returning from an extra
24774 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024775 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024776 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024777 * FALSE when the return gets pending.
24778 */
24779 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024780do_return(
24781 exarg_T *eap,
24782 int reanimate,
24783 int is_cmd,
24784 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024785{
24786 int idx;
24787 struct condstack *cstack = eap->cstack;
24788
24789 if (reanimate)
24790 /* Undo the return. */
24791 current_funccal->returned = FALSE;
24792
24793 /*
24794 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24795 * not in its finally clause (which then is to be executed next) is found.
24796 * In this case, make the ":return" pending for execution at the ":endtry".
24797 * Otherwise, return normally.
24798 */
24799 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24800 if (idx >= 0)
24801 {
24802 cstack->cs_pending[idx] = CSTP_RETURN;
24803
24804 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024805 /* A pending return again gets pending. "rettv" points to an
24806 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024807 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024808 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024809 else
24810 {
24811 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024812 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024813 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024814 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024815
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024816 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024817 {
24818 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024819 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024820 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024821 else
24822 EMSG(_(e_outofmem));
24823 }
24824 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024825 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024826
24827 if (reanimate)
24828 {
24829 /* The pending return value could be overwritten by a ":return"
24830 * without argument in a finally clause; reset the default
24831 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024832 current_funccal->rettv->v_type = VAR_NUMBER;
24833 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024834 }
24835 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024836 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024837 }
24838 else
24839 {
24840 current_funccal->returned = TRUE;
24841
24842 /* If the return is carried out now, store the return value. For
24843 * a return immediately after reanimation, the value is already
24844 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024845 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024846 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024847 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024848 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024849 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024850 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024851 }
24852 }
24853
24854 return idx < 0;
24855}
24856
24857/*
24858 * Free the variable with a pending return value.
24859 */
24860 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024861discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024862{
Bram Moolenaar33570922005-01-25 22:26:29 +000024863 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024864}
24865
24866/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024867 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024868 * is an allocated string. Used by report_pending() for verbose messages.
24869 */
24870 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024871get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024872{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024873 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024874 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024875 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024876
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024877 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024878 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024879 if (s == NULL)
24880 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024881
24882 STRCPY(IObuff, ":return ");
24883 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24884 if (STRLEN(s) + 8 >= IOSIZE)
24885 STRCPY(IObuff + IOSIZE - 4, "...");
24886 vim_free(tofree);
24887 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024888}
24889
24890/*
24891 * Get next function line.
24892 * Called by do_cmdline() to get the next line.
24893 * Returns allocated string, or NULL for end of function.
24894 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024895 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024896get_func_line(
24897 int c UNUSED,
24898 void *cookie,
24899 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024900{
Bram Moolenaar33570922005-01-25 22:26:29 +000024901 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024902 ufunc_T *fp = fcp->func;
24903 char_u *retval;
24904 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024905
24906 /* If breakpoints have been added/deleted need to check for it. */
24907 if (fcp->dbg_tick != debug_tick)
24908 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024909 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024910 sourcing_lnum);
24911 fcp->dbg_tick = debug_tick;
24912 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024913#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024914 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024915 func_line_end(cookie);
24916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024917
Bram Moolenaar05159a02005-02-26 23:04:13 +000024918 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024919 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24920 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024921 retval = NULL;
24922 else
24923 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024924 /* Skip NULL lines (continuation lines). */
24925 while (fcp->linenr < gap->ga_len
24926 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24927 ++fcp->linenr;
24928 if (fcp->linenr >= gap->ga_len)
24929 retval = NULL;
24930 else
24931 {
24932 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24933 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024934#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024935 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024936 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024937#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024939 }
24940
24941 /* Did we encounter a breakpoint? */
24942 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24943 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024944 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024945 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024946 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024947 sourcing_lnum);
24948 fcp->dbg_tick = debug_tick;
24949 }
24950
24951 return retval;
24952}
24953
Bram Moolenaar05159a02005-02-26 23:04:13 +000024954#if defined(FEAT_PROFILE) || defined(PROTO)
24955/*
24956 * Called when starting to read a function line.
24957 * "sourcing_lnum" must be correct!
24958 * When skipping lines it may not actually be executed, but we won't find out
24959 * until later and we need to store the time now.
24960 */
24961 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024962func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024963{
24964 funccall_T *fcp = (funccall_T *)cookie;
24965 ufunc_T *fp = fcp->func;
24966
24967 if (fp->uf_profiling && sourcing_lnum >= 1
24968 && sourcing_lnum <= fp->uf_lines.ga_len)
24969 {
24970 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024971 /* Skip continuation lines. */
24972 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24973 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024974 fp->uf_tml_execed = FALSE;
24975 profile_start(&fp->uf_tml_start);
24976 profile_zero(&fp->uf_tml_children);
24977 profile_get_wait(&fp->uf_tml_wait);
24978 }
24979}
24980
24981/*
24982 * Called when actually executing a function line.
24983 */
24984 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024985func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024986{
24987 funccall_T *fcp = (funccall_T *)cookie;
24988 ufunc_T *fp = fcp->func;
24989
24990 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24991 fp->uf_tml_execed = TRUE;
24992}
24993
24994/*
24995 * Called when done with a function line.
24996 */
24997 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024998func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024999{
25000 funccall_T *fcp = (funccall_T *)cookie;
25001 ufunc_T *fp = fcp->func;
25002
25003 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25004 {
25005 if (fp->uf_tml_execed)
25006 {
25007 ++fp->uf_tml_count[fp->uf_tml_idx];
25008 profile_end(&fp->uf_tml_start);
25009 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025010 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025011 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25012 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025013 }
25014 fp->uf_tml_idx = -1;
25015 }
25016}
25017#endif
25018
Bram Moolenaar071d4272004-06-13 20:20:40 +000025019/*
25020 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025021 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025022 */
25023 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025024func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025025{
Bram Moolenaar33570922005-01-25 22:26:29 +000025026 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025027
25028 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25029 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025030 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025031 || fcp->returned);
25032}
25033
25034/*
25035 * return TRUE if cookie indicates a function which "abort"s on errors.
25036 */
25037 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025038func_has_abort(
25039 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025040{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025041 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025042}
25043
25044#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25045typedef enum
25046{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025047 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25048 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25049 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025050} var_flavour_T;
25051
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025052static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025053
25054 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025055var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025056{
25057 char_u *p = varname;
25058
25059 if (ASCII_ISUPPER(*p))
25060 {
25061 while (*(++p))
25062 if (ASCII_ISLOWER(*p))
25063 return VAR_FLAVOUR_SESSION;
25064 return VAR_FLAVOUR_VIMINFO;
25065 }
25066 else
25067 return VAR_FLAVOUR_DEFAULT;
25068}
25069#endif
25070
25071#if defined(FEAT_VIMINFO) || defined(PROTO)
25072/*
25073 * Restore global vars that start with a capital from the viminfo file
25074 */
25075 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025076read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025077{
25078 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025079 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025080 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025081 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025082
25083 if (!writing && (find_viminfo_parameter('!') != NULL))
25084 {
25085 tab = vim_strchr(virp->vir_line + 1, '\t');
25086 if (tab != NULL)
25087 {
25088 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025089 switch (*tab)
25090 {
25091 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025092#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025093 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025094#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025095 case 'D': type = VAR_DICT; break;
25096 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025097 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025099
25100 tab = vim_strchr(tab, '\t');
25101 if (tab != NULL)
25102 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025103 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025104 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025105 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025106 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025107#ifdef FEAT_FLOAT
25108 else if (type == VAR_FLOAT)
25109 (void)string2float(tab + 1, &tv.vval.v_float);
25110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025111 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025112 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025113 if (type == VAR_DICT || type == VAR_LIST)
25114 {
25115 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25116
25117 if (etv == NULL)
25118 /* Failed to parse back the dict or list, use it as a
25119 * string. */
25120 tv.v_type = VAR_STRING;
25121 else
25122 {
25123 vim_free(tv.vval.v_string);
25124 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025125 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025126 }
25127 }
25128
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025129 /* when in a function use global variables */
25130 save_funccal = current_funccal;
25131 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025132 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025133 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025134
25135 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025136 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025137 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25138 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025139 }
25140 }
25141 }
25142
25143 return viminfo_readline(virp);
25144}
25145
25146/*
25147 * Write global vars that start with a capital to the viminfo file
25148 */
25149 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025150write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025151{
Bram Moolenaar33570922005-01-25 22:26:29 +000025152 hashitem_T *hi;
25153 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025154 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025155 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025156 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025157 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025158 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025159
25160 if (find_viminfo_parameter('!') == NULL)
25161 return;
25162
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025163 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025164
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025165 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025166 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025167 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025168 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025169 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025170 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025171 this_var = HI2DI(hi);
25172 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025173 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025174 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025175 {
25176 case VAR_STRING: s = "STR"; break;
25177 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025178 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025179 case VAR_DICT: s = "DIC"; break;
25180 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025181 case VAR_SPECIAL: s = "XPL"; break;
25182
25183 case VAR_UNKNOWN:
25184 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025185 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025186 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025187 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025188 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025189 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025190 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025191 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025192 if (p != NULL)
25193 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025194 vim_free(tofree);
25195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025196 }
25197 }
25198}
25199#endif
25200
25201#if defined(FEAT_SESSION) || defined(PROTO)
25202 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025203store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025204{
Bram Moolenaar33570922005-01-25 22:26:29 +000025205 hashitem_T *hi;
25206 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025207 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025208 char_u *p, *t;
25209
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025210 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025211 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025212 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025213 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025214 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025215 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025216 this_var = HI2DI(hi);
25217 if ((this_var->di_tv.v_type == VAR_NUMBER
25218 || this_var->di_tv.v_type == VAR_STRING)
25219 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025220 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025221 /* Escape special characters with a backslash. Turn a LF and
25222 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025223 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025224 (char_u *)"\\\"\n\r");
25225 if (p == NULL) /* out of memory */
25226 break;
25227 for (t = p; *t != NUL; ++t)
25228 if (*t == '\n')
25229 *t = 'n';
25230 else if (*t == '\r')
25231 *t = 'r';
25232 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025233 this_var->di_key,
25234 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25235 : ' ',
25236 p,
25237 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25238 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025239 || put_eol(fd) == FAIL)
25240 {
25241 vim_free(p);
25242 return FAIL;
25243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025244 vim_free(p);
25245 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025246#ifdef FEAT_FLOAT
25247 else if (this_var->di_tv.v_type == VAR_FLOAT
25248 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25249 {
25250 float_T f = this_var->di_tv.vval.v_float;
25251 int sign = ' ';
25252
25253 if (f < 0)
25254 {
25255 f = -f;
25256 sign = '-';
25257 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025258 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025259 this_var->di_key, sign, f) < 0)
25260 || put_eol(fd) == FAIL)
25261 return FAIL;
25262 }
25263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025264 }
25265 }
25266 return OK;
25267}
25268#endif
25269
Bram Moolenaar661b1822005-07-28 22:36:45 +000025270/*
25271 * Display script name where an item was last set.
25272 * Should only be invoked when 'verbose' is non-zero.
25273 */
25274 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025275last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025276{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025277 char_u *p;
25278
Bram Moolenaar661b1822005-07-28 22:36:45 +000025279 if (scriptID != 0)
25280 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025281 p = home_replace_save(NULL, get_scriptname(scriptID));
25282 if (p != NULL)
25283 {
25284 verbose_enter();
25285 MSG_PUTS(_("\n\tLast set from "));
25286 MSG_PUTS(p);
25287 vim_free(p);
25288 verbose_leave();
25289 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025290 }
25291}
25292
Bram Moolenaard812df62008-11-09 12:46:09 +000025293/*
25294 * List v:oldfiles in a nice way.
25295 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025296 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025297ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025298{
25299 list_T *l = vimvars[VV_OLDFILES].vv_list;
25300 listitem_T *li;
25301 int nr = 0;
25302
25303 if (l == NULL)
25304 msg((char_u *)_("No old files"));
25305 else
25306 {
25307 msg_start();
25308 msg_scroll = TRUE;
25309 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25310 {
25311 msg_outnum((long)++nr);
25312 MSG_PUTS(": ");
25313 msg_outtrans(get_tv_string(&li->li_tv));
25314 msg_putchar('\n');
25315 out_flush(); /* output one line at a time */
25316 ui_breakcheck();
25317 }
25318 /* Assume "got_int" was set to truncate the listing. */
25319 got_int = FALSE;
25320
25321#ifdef FEAT_BROWSE_CMD
25322 if (cmdmod.browse)
25323 {
25324 quit_more = FALSE;
25325 nr = prompt_for_number(FALSE);
25326 msg_starthere();
25327 if (nr > 0)
25328 {
25329 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25330 (long)nr);
25331
25332 if (p != NULL)
25333 {
25334 p = expand_env_save(p);
25335 eap->arg = p;
25336 eap->cmdidx = CMD_edit;
25337 cmdmod.browse = FALSE;
25338 do_exedit(eap, NULL);
25339 vim_free(p);
25340 }
25341 }
25342 }
25343#endif
25344 }
25345}
25346
Bram Moolenaar53744302015-07-17 17:38:22 +020025347/* reset v:option_new, v:option_old and v:option_type */
25348 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025349reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025350{
25351 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25352 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25353 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25354}
25355
25356
Bram Moolenaar071d4272004-06-13 20:20:40 +000025357#endif /* FEAT_EVAL */
25358
Bram Moolenaar071d4272004-06-13 20:20:40 +000025359
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025360#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025361
25362#ifdef WIN3264
25363/*
25364 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25365 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025366static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25367static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25368static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025369
25370/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025371 * Get the short path (8.3) for the filename in "fnamep".
25372 * Only works for a valid file name.
25373 * When the path gets longer "fnamep" is changed and the allocated buffer
25374 * is put in "bufp".
25375 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25376 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025377 */
25378 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025379get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025380{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025381 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025382 char_u *newbuf;
25383
25384 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025385 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025386 if (l > len - 1)
25387 {
25388 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025389 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025390 newbuf = vim_strnsave(*fnamep, l);
25391 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025392 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025393
25394 vim_free(*bufp);
25395 *fnamep = *bufp = newbuf;
25396
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025397 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025398 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025399 }
25400
25401 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025402 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025403}
25404
25405/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025406 * Get the short path (8.3) for the filename in "fname". The converted
25407 * path is returned in "bufp".
25408 *
25409 * Some of the directories specified in "fname" may not exist. This function
25410 * will shorten the existing directories at the beginning of the path and then
25411 * append the remaining non-existing path.
25412 *
25413 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025414 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025415 * bufp - Pointer to an allocated buffer for the filename.
25416 * fnamelen - Length of the filename pointed to by fname
25417 *
25418 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025419 */
25420 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025421shortpath_for_invalid_fname(
25422 char_u **fname,
25423 char_u **bufp,
25424 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025425{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025426 char_u *short_fname, *save_fname, *pbuf_unused;
25427 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025428 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025429 int old_len, len;
25430 int new_len, sfx_len;
25431 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025432
25433 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025434 old_len = *fnamelen;
25435 save_fname = vim_strnsave(*fname, old_len);
25436 pbuf_unused = NULL;
25437 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025438
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025439 endp = save_fname + old_len - 1; /* Find the end of the copy */
25440 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025441
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025442 /*
25443 * Try shortening the supplied path till it succeeds by removing one
25444 * directory at a time from the tail of the path.
25445 */
25446 len = 0;
25447 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025448 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025449 /* go back one path-separator */
25450 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25451 --endp;
25452 if (endp <= save_fname)
25453 break; /* processed the complete path */
25454
25455 /*
25456 * Replace the path separator with a NUL and try to shorten the
25457 * resulting path.
25458 */
25459 ch = *endp;
25460 *endp = 0;
25461 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025462 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025463 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25464 {
25465 retval = FAIL;
25466 goto theend;
25467 }
25468 *endp = ch; /* preserve the string */
25469
25470 if (len > 0)
25471 break; /* successfully shortened the path */
25472
25473 /* failed to shorten the path. Skip the path separator */
25474 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025475 }
25476
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025477 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025478 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025479 /*
25480 * Succeeded in shortening the path. Now concatenate the shortened
25481 * path with the remaining path at the tail.
25482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025483
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025484 /* Compute the length of the new path. */
25485 sfx_len = (int)(save_endp - endp) + 1;
25486 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025487
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025488 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025489 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025490 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025491 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025492 /* There is not enough space in the currently allocated string,
25493 * copy it to a buffer big enough. */
25494 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025495 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025496 {
25497 retval = FAIL;
25498 goto theend;
25499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025500 }
25501 else
25502 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025503 /* Transfer short_fname to the main buffer (it's big enough),
25504 * unless get_short_pathname() did its work in-place. */
25505 *fname = *bufp = save_fname;
25506 if (short_fname != save_fname)
25507 vim_strncpy(save_fname, short_fname, len);
25508 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025509 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025510
25511 /* concat the not-shortened part of the path */
25512 vim_strncpy(*fname + len, endp, sfx_len);
25513 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025514 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025515
25516theend:
25517 vim_free(pbuf_unused);
25518 vim_free(save_fname);
25519
25520 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025521}
25522
25523/*
25524 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025525 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025526 */
25527 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025528shortpath_for_partial(
25529 char_u **fnamep,
25530 char_u **bufp,
25531 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025532{
25533 int sepcount, len, tflen;
25534 char_u *p;
25535 char_u *pbuf, *tfname;
25536 int hasTilde;
25537
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025538 /* Count up the path separators from the RHS.. so we know which part
25539 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025540 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025541 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025542 if (vim_ispathsep(*p))
25543 ++sepcount;
25544
25545 /* Need full path first (use expand_env() to remove a "~/") */
25546 hasTilde = (**fnamep == '~');
25547 if (hasTilde)
25548 pbuf = tfname = expand_env_save(*fnamep);
25549 else
25550 pbuf = tfname = FullName_save(*fnamep, FALSE);
25551
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025552 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025553
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025554 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25555 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025556
25557 if (len == 0)
25558 {
25559 /* Don't have a valid filename, so shorten the rest of the
25560 * path if we can. This CAN give us invalid 8.3 filenames, but
25561 * there's not a lot of point in guessing what it might be.
25562 */
25563 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025564 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25565 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025566 }
25567
25568 /* Count the paths backward to find the beginning of the desired string. */
25569 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025570 {
25571#ifdef FEAT_MBYTE
25572 if (has_mbyte)
25573 p -= mb_head_off(tfname, p);
25574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025575 if (vim_ispathsep(*p))
25576 {
25577 if (sepcount == 0 || (hasTilde && sepcount == 1))
25578 break;
25579 else
25580 sepcount --;
25581 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025583 if (hasTilde)
25584 {
25585 --p;
25586 if (p >= tfname)
25587 *p = '~';
25588 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025589 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025590 }
25591 else
25592 ++p;
25593
25594 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25595 vim_free(*bufp);
25596 *fnamelen = (int)STRLEN(p);
25597 *bufp = pbuf;
25598 *fnamep = p;
25599
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025600 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025601}
25602#endif /* WIN3264 */
25603
25604/*
25605 * Adjust a filename, according to a string of modifiers.
25606 * *fnamep must be NUL terminated when called. When returning, the length is
25607 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025608 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025609 * When there is an error, *fnamep is set to NULL.
25610 */
25611 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025612modify_fname(
25613 char_u *src, /* string with modifiers */
25614 int *usedlen, /* characters after src that are used */
25615 char_u **fnamep, /* file name so far */
25616 char_u **bufp, /* buffer for allocated file name or NULL */
25617 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025618{
25619 int valid = 0;
25620 char_u *tail;
25621 char_u *s, *p, *pbuf;
25622 char_u dirname[MAXPATHL];
25623 int c;
25624 int has_fullname = 0;
25625#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025626 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025627 int has_shortname = 0;
25628#endif
25629
25630repeat:
25631 /* ":p" - full path/file_name */
25632 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25633 {
25634 has_fullname = 1;
25635
25636 valid |= VALID_PATH;
25637 *usedlen += 2;
25638
25639 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25640 if ((*fnamep)[0] == '~'
25641#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25642 && ((*fnamep)[1] == '/'
25643# ifdef BACKSLASH_IN_FILENAME
25644 || (*fnamep)[1] == '\\'
25645# endif
25646 || (*fnamep)[1] == NUL)
25647
25648#endif
25649 )
25650 {
25651 *fnamep = expand_env_save(*fnamep);
25652 vim_free(*bufp); /* free any allocated file name */
25653 *bufp = *fnamep;
25654 if (*fnamep == NULL)
25655 return -1;
25656 }
25657
25658 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025659 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025660 {
25661 if (vim_ispathsep(*p)
25662 && p[1] == '.'
25663 && (p[2] == NUL
25664 || vim_ispathsep(p[2])
25665 || (p[2] == '.'
25666 && (p[3] == NUL || vim_ispathsep(p[3])))))
25667 break;
25668 }
25669
25670 /* FullName_save() is slow, don't use it when not needed. */
25671 if (*p != NUL || !vim_isAbsName(*fnamep))
25672 {
25673 *fnamep = FullName_save(*fnamep, *p != NUL);
25674 vim_free(*bufp); /* free any allocated file name */
25675 *bufp = *fnamep;
25676 if (*fnamep == NULL)
25677 return -1;
25678 }
25679
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025680#ifdef WIN3264
25681# if _WIN32_WINNT >= 0x0500
25682 if (vim_strchr(*fnamep, '~') != NULL)
25683 {
25684 /* Expand 8.3 filename to full path. Needed to make sure the same
25685 * file does not have two different names.
25686 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25687 p = alloc(_MAX_PATH + 1);
25688 if (p != NULL)
25689 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025690 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025691 {
25692 vim_free(*bufp);
25693 *bufp = *fnamep = p;
25694 }
25695 else
25696 vim_free(p);
25697 }
25698 }
25699# endif
25700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025701 /* Append a path separator to a directory. */
25702 if (mch_isdir(*fnamep))
25703 {
25704 /* Make room for one or two extra characters. */
25705 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25706 vim_free(*bufp); /* free any allocated file name */
25707 *bufp = *fnamep;
25708 if (*fnamep == NULL)
25709 return -1;
25710 add_pathsep(*fnamep);
25711 }
25712 }
25713
25714 /* ":." - path relative to the current directory */
25715 /* ":~" - path relative to the home directory */
25716 /* ":8" - shortname path - postponed till after */
25717 while (src[*usedlen] == ':'
25718 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25719 {
25720 *usedlen += 2;
25721 if (c == '8')
25722 {
25723#ifdef WIN3264
25724 has_shortname = 1; /* Postpone this. */
25725#endif
25726 continue;
25727 }
25728 pbuf = NULL;
25729 /* Need full path first (use expand_env() to remove a "~/") */
25730 if (!has_fullname)
25731 {
25732 if (c == '.' && **fnamep == '~')
25733 p = pbuf = expand_env_save(*fnamep);
25734 else
25735 p = pbuf = FullName_save(*fnamep, FALSE);
25736 }
25737 else
25738 p = *fnamep;
25739
25740 has_fullname = 0;
25741
25742 if (p != NULL)
25743 {
25744 if (c == '.')
25745 {
25746 mch_dirname(dirname, MAXPATHL);
25747 s = shorten_fname(p, dirname);
25748 if (s != NULL)
25749 {
25750 *fnamep = s;
25751 if (pbuf != NULL)
25752 {
25753 vim_free(*bufp); /* free any allocated file name */
25754 *bufp = pbuf;
25755 pbuf = NULL;
25756 }
25757 }
25758 }
25759 else
25760 {
25761 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25762 /* Only replace it when it starts with '~' */
25763 if (*dirname == '~')
25764 {
25765 s = vim_strsave(dirname);
25766 if (s != NULL)
25767 {
25768 *fnamep = s;
25769 vim_free(*bufp);
25770 *bufp = s;
25771 }
25772 }
25773 }
25774 vim_free(pbuf);
25775 }
25776 }
25777
25778 tail = gettail(*fnamep);
25779 *fnamelen = (int)STRLEN(*fnamep);
25780
25781 /* ":h" - head, remove "/file_name", can be repeated */
25782 /* Don't remove the first "/" or "c:\" */
25783 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25784 {
25785 valid |= VALID_HEAD;
25786 *usedlen += 2;
25787 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025788 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025789 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025790 *fnamelen = (int)(tail - *fnamep);
25791#ifdef VMS
25792 if (*fnamelen > 0)
25793 *fnamelen += 1; /* the path separator is part of the path */
25794#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025795 if (*fnamelen == 0)
25796 {
25797 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25798 p = vim_strsave((char_u *)".");
25799 if (p == NULL)
25800 return -1;
25801 vim_free(*bufp);
25802 *bufp = *fnamep = tail = p;
25803 *fnamelen = 1;
25804 }
25805 else
25806 {
25807 while (tail > s && !after_pathsep(s, tail))
25808 mb_ptr_back(*fnamep, tail);
25809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025810 }
25811
25812 /* ":8" - shortname */
25813 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25814 {
25815 *usedlen += 2;
25816#ifdef WIN3264
25817 has_shortname = 1;
25818#endif
25819 }
25820
25821#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025822 /*
25823 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025824 */
25825 if (has_shortname)
25826 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025827 /* Copy the string if it is shortened by :h and when it wasn't copied
25828 * yet, because we are going to change it in place. Avoids changing
25829 * the buffer name for "%:8". */
25830 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025831 {
25832 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025833 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025834 return -1;
25835 vim_free(*bufp);
25836 *bufp = *fnamep = p;
25837 }
25838
25839 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025840 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025841 if (!has_fullname && !vim_isAbsName(*fnamep))
25842 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025843 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025844 return -1;
25845 }
25846 else
25847 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025848 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025849
Bram Moolenaardc935552011-08-17 15:23:23 +020025850 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025851 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025852 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025853 return -1;
25854
25855 if (l == 0)
25856 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025857 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025858 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025859 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025860 return -1;
25861 }
25862 *fnamelen = l;
25863 }
25864 }
25865#endif /* WIN3264 */
25866
25867 /* ":t" - tail, just the basename */
25868 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25869 {
25870 *usedlen += 2;
25871 *fnamelen -= (int)(tail - *fnamep);
25872 *fnamep = tail;
25873 }
25874
25875 /* ":e" - extension, can be repeated */
25876 /* ":r" - root, without extension, can be repeated */
25877 while (src[*usedlen] == ':'
25878 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25879 {
25880 /* find a '.' in the tail:
25881 * - for second :e: before the current fname
25882 * - otherwise: The last '.'
25883 */
25884 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25885 s = *fnamep - 2;
25886 else
25887 s = *fnamep + *fnamelen - 1;
25888 for ( ; s > tail; --s)
25889 if (s[0] == '.')
25890 break;
25891 if (src[*usedlen + 1] == 'e') /* :e */
25892 {
25893 if (s > tail)
25894 {
25895 *fnamelen += (int)(*fnamep - (s + 1));
25896 *fnamep = s + 1;
25897#ifdef VMS
25898 /* cut version from the extension */
25899 s = *fnamep + *fnamelen - 1;
25900 for ( ; s > *fnamep; --s)
25901 if (s[0] == ';')
25902 break;
25903 if (s > *fnamep)
25904 *fnamelen = s - *fnamep;
25905#endif
25906 }
25907 else if (*fnamep <= tail)
25908 *fnamelen = 0;
25909 }
25910 else /* :r */
25911 {
25912 if (s > tail) /* remove one extension */
25913 *fnamelen = (int)(s - *fnamep);
25914 }
25915 *usedlen += 2;
25916 }
25917
25918 /* ":s?pat?foo?" - substitute */
25919 /* ":gs?pat?foo?" - global substitute */
25920 if (src[*usedlen] == ':'
25921 && (src[*usedlen + 1] == 's'
25922 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25923 {
25924 char_u *str;
25925 char_u *pat;
25926 char_u *sub;
25927 int sep;
25928 char_u *flags;
25929 int didit = FALSE;
25930
25931 flags = (char_u *)"";
25932 s = src + *usedlen + 2;
25933 if (src[*usedlen + 1] == 'g')
25934 {
25935 flags = (char_u *)"g";
25936 ++s;
25937 }
25938
25939 sep = *s++;
25940 if (sep)
25941 {
25942 /* find end of pattern */
25943 p = vim_strchr(s, sep);
25944 if (p != NULL)
25945 {
25946 pat = vim_strnsave(s, (int)(p - s));
25947 if (pat != NULL)
25948 {
25949 s = p + 1;
25950 /* find end of substitution */
25951 p = vim_strchr(s, sep);
25952 if (p != NULL)
25953 {
25954 sub = vim_strnsave(s, (int)(p - s));
25955 str = vim_strnsave(*fnamep, *fnamelen);
25956 if (sub != NULL && str != NULL)
25957 {
25958 *usedlen = (int)(p + 1 - src);
25959 s = do_string_sub(str, pat, sub, flags);
25960 if (s != NULL)
25961 {
25962 *fnamep = s;
25963 *fnamelen = (int)STRLEN(s);
25964 vim_free(*bufp);
25965 *bufp = s;
25966 didit = TRUE;
25967 }
25968 }
25969 vim_free(sub);
25970 vim_free(str);
25971 }
25972 vim_free(pat);
25973 }
25974 }
25975 /* after using ":s", repeat all the modifiers */
25976 if (didit)
25977 goto repeat;
25978 }
25979 }
25980
Bram Moolenaar26df0922014-02-23 23:39:13 +010025981 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25982 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010025983 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010025984 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010025985 if (c != NUL)
25986 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010025987 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010025988 if (c != NUL)
25989 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010025990 if (p == NULL)
25991 return -1;
25992 vim_free(*bufp);
25993 *bufp = *fnamep = p;
25994 *fnamelen = (int)STRLEN(p);
25995 *usedlen += 2;
25996 }
25997
Bram Moolenaar071d4272004-06-13 20:20:40 +000025998 return valid;
25999}
26000
26001/*
26002 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26003 * "flags" can be "g" to do a global substitute.
26004 * Returns an allocated string, NULL for error.
26005 */
26006 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026007do_string_sub(
26008 char_u *str,
26009 char_u *pat,
26010 char_u *sub,
26011 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026012{
26013 int sublen;
26014 regmatch_T regmatch;
26015 int i;
26016 int do_all;
26017 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026018 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026019 garray_T ga;
26020 char_u *ret;
26021 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026022 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026023
26024 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26025 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026026 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026027
26028 ga_init2(&ga, 1, 200);
26029
26030 do_all = (flags[0] == 'g');
26031
26032 regmatch.rm_ic = p_ic;
26033 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26034 if (regmatch.regprog != NULL)
26035 {
26036 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026037 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026038 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26039 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026040 /* Skip empty match except for first match. */
26041 if (regmatch.startp[0] == regmatch.endp[0])
26042 {
26043 if (zero_width == regmatch.startp[0])
26044 {
26045 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026046 i = MB_PTR2LEN(tail);
26047 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26048 (size_t)i);
26049 ga.ga_len += i;
26050 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026051 continue;
26052 }
26053 zero_width = regmatch.startp[0];
26054 }
26055
Bram Moolenaar071d4272004-06-13 20:20:40 +000026056 /*
26057 * Get some space for a temporary buffer to do the substitution
26058 * into. It will contain:
26059 * - The text up to where the match is.
26060 * - The substituted text.
26061 * - The text after the match.
26062 */
26063 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026064 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026065 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26066 {
26067 ga_clear(&ga);
26068 break;
26069 }
26070
26071 /* copy the text up to where the match is */
26072 i = (int)(regmatch.startp[0] - tail);
26073 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26074 /* add the substituted text */
26075 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26076 + ga.ga_len + i, TRUE, TRUE, FALSE);
26077 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026078 tail = regmatch.endp[0];
26079 if (*tail == NUL)
26080 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026081 if (!do_all)
26082 break;
26083 }
26084
26085 if (ga.ga_data != NULL)
26086 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26087
Bram Moolenaar473de612013-06-08 18:19:48 +020026088 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026089 }
26090
26091 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26092 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026093 if (p_cpo == empty_option)
26094 p_cpo = save_cpo;
26095 else
26096 /* Darn, evaluating {sub} expression changed the value. */
26097 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026098
26099 return ret;
26100}
26101
26102#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */