blob: 85f209221ab6b1186aaa0b9ad7cc05cf54a01057 [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 Moolenaar33570922005-01-25 22:26:29 +000037 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
38 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000039 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
40 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
41 * HI2DI() converts a hashitem pointer to a dictitem pointer.
42 */
Bram Moolenaar33570922005-01-25 22:26:29 +000043static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000044#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000045#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000047
48/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000049 * Structure returned by get_lval() and used by set_var_lval().
50 * For a plain name:
51 * "name" points to the variable name.
52 * "exp_name" is NULL.
53 * "tv" is NULL
54 * For a magic braces name:
55 * "name" points to the expanded variable name.
56 * "exp_name" is non-NULL, to be freed later.
57 * "tv" is NULL
58 * For an index in a list:
59 * "name" points to the (expanded) variable name.
60 * "exp_name" NULL or non-NULL, to be freed later.
61 * "tv" points to the (first) list item value
62 * "li" points to the (first) list item
63 * "range", "n1", "n2" and "empty2" indicate what items are used.
64 * For an existing Dict item:
65 * "name" points to the (expanded) variable name.
66 * "exp_name" NULL or non-NULL, to be freed later.
67 * "tv" points to the dict item value
68 * "newkey" is NULL
69 * For a non-existing Dict item:
70 * "name" points to the (expanded) variable name.
71 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000072 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000073 * "newkey" is the key for the new item.
74 */
75typedef struct lval_S
76{
77 char_u *ll_name; /* start of variable name (can be NULL) */
78 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000080 isn't NULL it's the Dict to which to add
81 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 listitem_T *ll_li; /* The list item or NULL. */
83 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000084 int ll_range; /* TRUE when a [i:j] range was used */
85 long ll_n1; /* First index for list */
86 long ll_n2; /* Second index for list range */
87 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 dict_T *ll_dict; /* The Dictionary or NULL */
89 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000090 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000091} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000099static char *e_listreq = N_("E714: List required");
100static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000102static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
103static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
104static char *e_funcdict = N_("E717: Dictionary entry already exists");
105static char *e_funcref = N_("E718: Funcref required");
106static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
107static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000108static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000109static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200110#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200111static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200112#endif
Bram Moolenaarab1fa392016-03-15 19:33:34 +0100113static char *e_dict_both = N_("E924: can't have both a \"self\" dict and a partial: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000114
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100115#define NAMESPACE_CHAR (char_u *)"abglstvw"
116
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200117static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000118#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119
120/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000121 * Old Vim variables such as "v:version" are also available without the "v:".
122 * Also in functions. We need a special hashtable for them.
123 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000124static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000125
126/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000127 * When recursively copying lists and dicts we need to remember which ones we
128 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000129 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000130 */
131static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000132#define COPYID_INC 2
133#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000134
Bram Moolenaar8502c702014-06-17 12:51:16 +0200135/* Abort conversion to string after a recursion error. */
136static int did_echo_string_emsg = FALSE;
137
Bram Moolenaard9fba312005-06-26 22:34:35 +0000138/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000139 * Array to hold the hashtab with variables local to each sourced script.
140 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000142typedef struct
143{
144 dictitem_T sv_var;
145 dict_T sv_dict;
146} scriptvar_T;
147
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200148static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
149#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
150#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
152static int echo_attr = 0; /* attributes used for ":echo" */
153
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000154/* Values for trans_function_name() argument: */
155#define TFN_INT 1 /* internal function name OK */
156#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100157#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
158
159/* Values for get_lval() flags argument: */
160#define GLV_QUIET TFN_QUIET /* no error messages */
161#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000162
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163/*
164 * Structure to hold info for a user function.
165 */
166typedef struct ufunc ufunc_T;
167
168struct ufunc
169{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000170 int uf_varargs; /* variable nr of arguments */
171 int uf_flags;
172 int uf_calls; /* nr of active calls */
173 garray_T uf_args; /* arguments */
174 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000175#ifdef FEAT_PROFILE
176 int uf_profiling; /* TRUE when func is being profiled */
177 /* profiling the function as a whole */
178 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000179 proftime_T uf_tm_total; /* time spent in function + children */
180 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000181 proftime_T uf_tm_children; /* time spent in children this call */
182 /* profiling the function per line */
183 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000184 proftime_T *uf_tml_total; /* time spent in a line + children */
185 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000186 proftime_T uf_tml_start; /* start time for current line */
187 proftime_T uf_tml_children; /* time spent in children for this line */
188 proftime_T uf_tml_wait; /* start wait time for current line */
189 int uf_tml_idx; /* index of line being timed; -1 if none */
190 int uf_tml_execed; /* line being timed was executed */
191#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000192 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 int uf_refcount; /* for numbered function: reference count */
195 char_u uf_name[1]; /* name of function (actually longer); can
196 start with <SNR>123_ (<SNR> is K_SPECIAL
197 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198};
199
200/* function flags */
201#define FC_ABORT 1 /* abort function on error */
202#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000203#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
205/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000206 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000208static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000210/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000211static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
212
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000213/* list heads for garbage collection */
214static dict_T *first_dict = NULL; /* list of all dicts */
215static list_T *first_list = NULL; /* list of all lists */
216
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000217/* From user function to hashitem and back. */
218static ufunc_T dumuf;
219#define UF2HIKEY(fp) ((fp)->uf_name)
220#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
221#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
222
223#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
224#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225
Bram Moolenaar33570922005-01-25 22:26:29 +0000226#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
227#define VAR_SHORT_LEN 20 /* short variable name length */
228#define FIXVAR_CNT 12 /* number of fixed variables */
229
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000231typedef struct funccall_S funccall_T;
232
233struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234{
235 ufunc_T *func; /* function being called */
236 int linenr; /* next line to be executed */
237 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000238 struct /* fixed variables for arguments */
239 {
240 dictitem_T var; /* variable (without room for name) */
241 char_u room[VAR_SHORT_LEN]; /* room for the name */
242 } fixvar[FIXVAR_CNT];
243 dict_T l_vars; /* l: local function variables */
244 dictitem_T l_vars_var; /* variable for l: scope */
245 dict_T l_avars; /* a: argument variables */
246 dictitem_T l_avars_var; /* variable for a: scope */
247 list_T l_varlist; /* list for a:000 */
248 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
249 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 linenr_T breakpoint; /* next line with breakpoint or zero */
251 int dbg_tick; /* debug_tick when breakpoint was set */
252 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000253#ifdef FEAT_PROFILE
254 proftime_T prof_child; /* time spent in a child */
255#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000256 funccall_T *caller; /* calling function or NULL */
257};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258
259/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000260 * Info used by a ":for" loop.
261 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000262typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000263{
264 int fi_semicolon; /* TRUE if ending in '; var]' */
265 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266 listwatch_T fi_lw; /* keep an eye on the item used. */
267 list_T *fi_list; /* list being used */
268} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000270/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000271 * Struct used by trans_function_name()
272 */
273typedef struct
274{
Bram Moolenaar33570922005-01-25 22:26:29 +0000275 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000276 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 dictitem_T *fd_di; /* Dictionary item used */
278} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000279
Bram Moolenaara7043832005-01-21 11:56:39 +0000280
281/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000282 * Array to hold the value of v: variables.
283 * The value is in a dictitem, so that it can also be used in the v: scope.
284 * The reason to use this table anyway is for very quick access to the
285 * variables with the VV_ defines.
286 */
287#include "version.h"
288
289/* values for vv_flags: */
290#define VV_COMPAT 1 /* compatible, also used without "v:" */
291#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000292#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000293
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000294#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000295
296static struct vimvar
297{
298 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000299 dictitem_T vv_di; /* value and name for key */
300 char vv_filler[16]; /* space for LONGEST name below!!! */
301 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
302} vimvars[VV_LEN] =
303{
304 /*
305 * The order here must match the VV_ defines in vim.h!
306 * Initializing a union does not work, leave tv.vval empty to get zero's.
307 */
308 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
309 {VV_NAME("count1", VAR_NUMBER), VV_RO},
310 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
311 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
312 {VV_NAME("warningmsg", VAR_STRING), 0},
313 {VV_NAME("statusmsg", VAR_STRING), 0},
314 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
315 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
316 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
318 {VV_NAME("termresponse", VAR_STRING), VV_RO},
319 {VV_NAME("fname", VAR_STRING), VV_RO},
320 {VV_NAME("lang", VAR_STRING), VV_RO},
321 {VV_NAME("lc_time", VAR_STRING), VV_RO},
322 {VV_NAME("ctype", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
324 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
325 {VV_NAME("fname_in", VAR_STRING), VV_RO},
326 {VV_NAME("fname_out", VAR_STRING), VV_RO},
327 {VV_NAME("fname_new", VAR_STRING), VV_RO},
328 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
329 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
330 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
333 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("progname", VAR_STRING), VV_RO},
335 {VV_NAME("servername", VAR_STRING), VV_RO},
336 {VV_NAME("dying", VAR_NUMBER), VV_RO},
337 {VV_NAME("exception", VAR_STRING), VV_RO},
338 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
339 {VV_NAME("register", VAR_STRING), VV_RO},
340 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
341 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000342 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
343 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000344 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000345 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
346 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000347 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000352 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000353 {VV_NAME("swapname", VAR_STRING), VV_RO},
354 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000355 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200356 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000357 {VV_NAME("mouse_win", VAR_NUMBER), 0},
358 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
359 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000360 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000361 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100362 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000363 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200364 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200365 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200366 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200367 {VV_NAME("option_new", VAR_STRING), VV_RO},
368 {VV_NAME("option_old", VAR_STRING), VV_RO},
369 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100370 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100371 {VV_NAME("false", VAR_SPECIAL), VV_RO},
372 {VV_NAME("true", VAR_SPECIAL), VV_RO},
373 {VV_NAME("null", VAR_SPECIAL), VV_RO},
374 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000375};
376
377/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000378#define vv_type vv_di.di_tv.v_type
379#define vv_nr vv_di.di_tv.vval.v_number
380#define vv_float vv_di.di_tv.vval.v_float
381#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000382#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200383#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000384#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000385
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200386static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000387#define vimvarht vimvardict.dv_hashtab
388
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100389static void prepare_vimvar(int idx, typval_T *save_tv);
390static void restore_vimvar(int idx, typval_T *save_tv);
391static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
392static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
393static char_u *skip_var_one(char_u *arg);
394static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
395static void list_glob_vars(int *first);
396static void list_buf_vars(int *first);
397static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000398#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100399static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000400#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100401static void list_vim_vars(int *first);
402static void list_script_vars(int *first);
403static void list_func_vars(int *first);
404static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
405static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
406static int check_changedtick(char_u *arg);
407static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
408static void clear_lval(lval_T *lp);
409static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
410static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
411static void list_fix_watch(list_T *l, listitem_T *item);
412static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
413static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
414static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
415static void item_lock(typval_T *tv, int deep, int lock);
416static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000417
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100418static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
419static int eval1(char_u **arg, typval_T *rettv, int evaluate);
420static int eval2(char_u **arg, typval_T *rettv, int evaluate);
421static int eval3(char_u **arg, typval_T *rettv, int evaluate);
422static int eval4(char_u **arg, typval_T *rettv, int evaluate);
423static int eval5(char_u **arg, typval_T *rettv, int evaluate);
424static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
425static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000426
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100427static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
428static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
429static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
430static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
431static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
432static long list_len(list_T *l);
433static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
434static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
435static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
436static long list_find_nr(list_T *l, long idx, int *errorp);
437static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100438static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
439static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
440static list_T *list_copy(list_T *orig, int deep, int copyID);
441static char_u *list2string(typval_T *tv, int copyID);
442static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
443static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
444static int free_unref_items(int copyID);
445static dictitem_T *dictitem_copy(dictitem_T *org);
446static void dictitem_remove(dict_T *dict, dictitem_T *item);
447static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
448static long dict_len(dict_T *d);
449static char_u *dict2string(typval_T *tv, int copyID);
450static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
451static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
452static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
453static char_u *string_quote(char_u *str, int function);
454static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
455static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100456static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
457static 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 +0100458static void emsg_funcname(char *ermsg, char_u *name);
459static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000460
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000461#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100462static void f_abs(typval_T *argvars, typval_T *rettv);
463static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100465static void f_add(typval_T *argvars, typval_T *rettv);
466static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
467static void f_and(typval_T *argvars, typval_T *rettv);
468static void f_append(typval_T *argvars, typval_T *rettv);
469static void f_argc(typval_T *argvars, typval_T *rettv);
470static void f_argidx(typval_T *argvars, typval_T *rettv);
471static void f_arglistid(typval_T *argvars, typval_T *rettv);
472static void f_argv(typval_T *argvars, typval_T *rettv);
473static void f_assert_equal(typval_T *argvars, typval_T *rettv);
474static void f_assert_exception(typval_T *argvars, typval_T *rettv);
475static void f_assert_fails(typval_T *argvars, typval_T *rettv);
476static void f_assert_false(typval_T *argvars, typval_T *rettv);
477static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000478#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100479static void f_asin(typval_T *argvars, typval_T *rettv);
480static void f_atan(typval_T *argvars, typval_T *rettv);
481static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000482#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100483static void f_browse(typval_T *argvars, typval_T *rettv);
484static void f_browsedir(typval_T *argvars, typval_T *rettv);
485static void f_bufexists(typval_T *argvars, typval_T *rettv);
486static void f_buflisted(typval_T *argvars, typval_T *rettv);
487static void f_bufloaded(typval_T *argvars, typval_T *rettv);
488static void f_bufname(typval_T *argvars, typval_T *rettv);
489static void f_bufnr(typval_T *argvars, typval_T *rettv);
490static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
491static void f_byte2line(typval_T *argvars, typval_T *rettv);
492static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
493static void f_byteidx(typval_T *argvars, typval_T *rettv);
494static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
495static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000496#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100497static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000498#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100499#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100500static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100501static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
502static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100503static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100504static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100505static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100506static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
507static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100508static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100509static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100510static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
511static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100512static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100513static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100514#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100515static void f_changenr(typval_T *argvars, typval_T *rettv);
516static void f_char2nr(typval_T *argvars, typval_T *rettv);
517static void f_cindent(typval_T *argvars, typval_T *rettv);
518static void f_clearmatches(typval_T *argvars, typval_T *rettv);
519static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000520#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100521static void f_complete(typval_T *argvars, typval_T *rettv);
522static void f_complete_add(typval_T *argvars, typval_T *rettv);
523static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000524#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100525static void f_confirm(typval_T *argvars, typval_T *rettv);
526static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000527#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100528static void f_cos(typval_T *argvars, typval_T *rettv);
529static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000530#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100531static void f_count(typval_T *argvars, typval_T *rettv);
532static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
533static void f_cursor(typval_T *argsvars, typval_T *rettv);
534static void f_deepcopy(typval_T *argvars, typval_T *rettv);
535static void f_delete(typval_T *argvars, typval_T *rettv);
536static void f_did_filetype(typval_T *argvars, typval_T *rettv);
537static void f_diff_filler(typval_T *argvars, typval_T *rettv);
538static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100539static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100540static void f_empty(typval_T *argvars, typval_T *rettv);
541static void f_escape(typval_T *argvars, typval_T *rettv);
542static void f_eval(typval_T *argvars, typval_T *rettv);
543static void f_eventhandler(typval_T *argvars, typval_T *rettv);
544static void f_executable(typval_T *argvars, typval_T *rettv);
545static void f_exepath(typval_T *argvars, typval_T *rettv);
546static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200547#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100548static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200549#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100550static void f_expand(typval_T *argvars, typval_T *rettv);
551static void f_extend(typval_T *argvars, typval_T *rettv);
552static void f_feedkeys(typval_T *argvars, typval_T *rettv);
553static void f_filereadable(typval_T *argvars, typval_T *rettv);
554static void f_filewritable(typval_T *argvars, typval_T *rettv);
555static void f_filter(typval_T *argvars, typval_T *rettv);
556static void f_finddir(typval_T *argvars, typval_T *rettv);
557static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000558#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100559static void f_float2nr(typval_T *argvars, typval_T *rettv);
560static void f_floor(typval_T *argvars, typval_T *rettv);
561static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000562#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100563static void f_fnameescape(typval_T *argvars, typval_T *rettv);
564static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
565static void f_foldclosed(typval_T *argvars, typval_T *rettv);
566static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
567static void f_foldlevel(typval_T *argvars, typval_T *rettv);
568static void f_foldtext(typval_T *argvars, typval_T *rettv);
569static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
570static void f_foreground(typval_T *argvars, typval_T *rettv);
571static void f_function(typval_T *argvars, typval_T *rettv);
572static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
573static void f_get(typval_T *argvars, typval_T *rettv);
574static void f_getbufline(typval_T *argvars, typval_T *rettv);
575static void f_getbufvar(typval_T *argvars, typval_T *rettv);
576static void f_getchar(typval_T *argvars, typval_T *rettv);
577static void f_getcharmod(typval_T *argvars, typval_T *rettv);
578static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
579static void f_getcmdline(typval_T *argvars, typval_T *rettv);
580static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
581static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
582static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
583static void f_getcwd(typval_T *argvars, typval_T *rettv);
584static void f_getfontname(typval_T *argvars, typval_T *rettv);
585static void f_getfperm(typval_T *argvars, typval_T *rettv);
586static void f_getfsize(typval_T *argvars, typval_T *rettv);
587static void f_getftime(typval_T *argvars, typval_T *rettv);
588static void f_getftype(typval_T *argvars, typval_T *rettv);
589static void f_getline(typval_T *argvars, typval_T *rettv);
590static void f_getmatches(typval_T *argvars, typval_T *rettv);
591static void f_getpid(typval_T *argvars, typval_T *rettv);
592static void f_getcurpos(typval_T *argvars, typval_T *rettv);
593static void f_getpos(typval_T *argvars, typval_T *rettv);
594static void f_getqflist(typval_T *argvars, typval_T *rettv);
595static void f_getreg(typval_T *argvars, typval_T *rettv);
596static void f_getregtype(typval_T *argvars, typval_T *rettv);
597static void f_gettabvar(typval_T *argvars, typval_T *rettv);
598static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
599static void f_getwinposx(typval_T *argvars, typval_T *rettv);
600static void f_getwinposy(typval_T *argvars, typval_T *rettv);
601static void f_getwinvar(typval_T *argvars, typval_T *rettv);
602static void f_glob(typval_T *argvars, typval_T *rettv);
603static void f_globpath(typval_T *argvars, typval_T *rettv);
604static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
605static void f_has(typval_T *argvars, typval_T *rettv);
606static void f_has_key(typval_T *argvars, typval_T *rettv);
607static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
608static void f_hasmapto(typval_T *argvars, typval_T *rettv);
609static void f_histadd(typval_T *argvars, typval_T *rettv);
610static void f_histdel(typval_T *argvars, typval_T *rettv);
611static void f_histget(typval_T *argvars, typval_T *rettv);
612static void f_histnr(typval_T *argvars, typval_T *rettv);
613static void f_hlID(typval_T *argvars, typval_T *rettv);
614static void f_hlexists(typval_T *argvars, typval_T *rettv);
615static void f_hostname(typval_T *argvars, typval_T *rettv);
616static void f_iconv(typval_T *argvars, typval_T *rettv);
617static void f_indent(typval_T *argvars, typval_T *rettv);
618static void f_index(typval_T *argvars, typval_T *rettv);
619static void f_input(typval_T *argvars, typval_T *rettv);
620static void f_inputdialog(typval_T *argvars, typval_T *rettv);
621static void f_inputlist(typval_T *argvars, typval_T *rettv);
622static void f_inputrestore(typval_T *argvars, typval_T *rettv);
623static void f_inputsave(typval_T *argvars, typval_T *rettv);
624static void f_inputsecret(typval_T *argvars, typval_T *rettv);
625static void f_insert(typval_T *argvars, typval_T *rettv);
626static void f_invert(typval_T *argvars, typval_T *rettv);
627static void f_isdirectory(typval_T *argvars, typval_T *rettv);
628static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100629#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
630static void f_isnan(typval_T *argvars, typval_T *rettv);
631#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100632static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100633#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100634static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100635static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100636static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100637static void f_job_start(typval_T *argvars, typval_T *rettv);
638static void f_job_stop(typval_T *argvars, typval_T *rettv);
639static void f_job_status(typval_T *argvars, typval_T *rettv);
640#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100641static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100642static void f_js_decode(typval_T *argvars, typval_T *rettv);
643static void f_js_encode(typval_T *argvars, typval_T *rettv);
644static void f_json_decode(typval_T *argvars, typval_T *rettv);
645static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100646static void f_keys(typval_T *argvars, typval_T *rettv);
647static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
648static void f_len(typval_T *argvars, typval_T *rettv);
649static void f_libcall(typval_T *argvars, typval_T *rettv);
650static void f_libcallnr(typval_T *argvars, typval_T *rettv);
651static void f_line(typval_T *argvars, typval_T *rettv);
652static void f_line2byte(typval_T *argvars, typval_T *rettv);
653static void f_lispindent(typval_T *argvars, typval_T *rettv);
654static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000655#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100656static void f_log(typval_T *argvars, typval_T *rettv);
657static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000658#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200659#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100660static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200661#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100662static void f_map(typval_T *argvars, typval_T *rettv);
663static void f_maparg(typval_T *argvars, typval_T *rettv);
664static void f_mapcheck(typval_T *argvars, typval_T *rettv);
665static void f_match(typval_T *argvars, typval_T *rettv);
666static void f_matchadd(typval_T *argvars, typval_T *rettv);
667static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
668static void f_matcharg(typval_T *argvars, typval_T *rettv);
669static void f_matchdelete(typval_T *argvars, typval_T *rettv);
670static void f_matchend(typval_T *argvars, typval_T *rettv);
671static void f_matchlist(typval_T *argvars, typval_T *rettv);
672static void f_matchstr(typval_T *argvars, typval_T *rettv);
673static void f_max(typval_T *argvars, typval_T *rettv);
674static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000675#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100676static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000677#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100678static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100679#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100680static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100681#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100682static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
683static void f_nr2char(typval_T *argvars, typval_T *rettv);
684static void f_or(typval_T *argvars, typval_T *rettv);
685static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100686#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100687static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100688#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000689#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000691#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100692static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
693static void f_printf(typval_T *argvars, typval_T *rettv);
694static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200695#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100696static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200697#endif
698#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100699static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200700#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100701static void f_range(typval_T *argvars, typval_T *rettv);
702static void f_readfile(typval_T *argvars, typval_T *rettv);
703static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100704#ifdef FEAT_FLOAT
705static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
706#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100707static void f_reltimestr(typval_T *argvars, typval_T *rettv);
708static void f_remote_expr(typval_T *argvars, typval_T *rettv);
709static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
710static void f_remote_peek(typval_T *argvars, typval_T *rettv);
711static void f_remote_read(typval_T *argvars, typval_T *rettv);
712static void f_remote_send(typval_T *argvars, typval_T *rettv);
713static void f_remove(typval_T *argvars, typval_T *rettv);
714static void f_rename(typval_T *argvars, typval_T *rettv);
715static void f_repeat(typval_T *argvars, typval_T *rettv);
716static void f_resolve(typval_T *argvars, typval_T *rettv);
717static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000718#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100719static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000720#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100721static void f_screenattr(typval_T *argvars, typval_T *rettv);
722static void f_screenchar(typval_T *argvars, typval_T *rettv);
723static void f_screencol(typval_T *argvars, typval_T *rettv);
724static void f_screenrow(typval_T *argvars, typval_T *rettv);
725static void f_search(typval_T *argvars, typval_T *rettv);
726static void f_searchdecl(typval_T *argvars, typval_T *rettv);
727static void f_searchpair(typval_T *argvars, typval_T *rettv);
728static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
729static void f_searchpos(typval_T *argvars, typval_T *rettv);
730static void f_server2client(typval_T *argvars, typval_T *rettv);
731static void f_serverlist(typval_T *argvars, typval_T *rettv);
732static void f_setbufvar(typval_T *argvars, typval_T *rettv);
733static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
734static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100735static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100736static void f_setline(typval_T *argvars, typval_T *rettv);
737static void f_setloclist(typval_T *argvars, typval_T *rettv);
738static void f_setmatches(typval_T *argvars, typval_T *rettv);
739static void f_setpos(typval_T *argvars, typval_T *rettv);
740static void f_setqflist(typval_T *argvars, typval_T *rettv);
741static void f_setreg(typval_T *argvars, typval_T *rettv);
742static void f_settabvar(typval_T *argvars, typval_T *rettv);
743static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
744static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100745#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100746static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100747#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100748static void f_shellescape(typval_T *argvars, typval_T *rettv);
749static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
750static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000751#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100752static void f_sin(typval_T *argvars, typval_T *rettv);
753static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000754#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100755static void f_sort(typval_T *argvars, typval_T *rettv);
756static void f_soundfold(typval_T *argvars, typval_T *rettv);
757static void f_spellbadword(typval_T *argvars, typval_T *rettv);
758static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
759static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000760#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100761static void f_sqrt(typval_T *argvars, typval_T *rettv);
762static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000763#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100764static void f_str2nr(typval_T *argvars, typval_T *rettv);
765static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000766#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100767static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000768#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100769static void f_stridx(typval_T *argvars, typval_T *rettv);
770static void f_string(typval_T *argvars, typval_T *rettv);
771static void f_strlen(typval_T *argvars, typval_T *rettv);
772static void f_strpart(typval_T *argvars, typval_T *rettv);
773static void f_strridx(typval_T *argvars, typval_T *rettv);
774static void f_strtrans(typval_T *argvars, typval_T *rettv);
775static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
776static void f_strwidth(typval_T *argvars, typval_T *rettv);
777static void f_submatch(typval_T *argvars, typval_T *rettv);
778static void f_substitute(typval_T *argvars, typval_T *rettv);
779static void f_synID(typval_T *argvars, typval_T *rettv);
780static void f_synIDattr(typval_T *argvars, typval_T *rettv);
781static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
782static void f_synstack(typval_T *argvars, typval_T *rettv);
783static void f_synconcealed(typval_T *argvars, typval_T *rettv);
784static void f_system(typval_T *argvars, typval_T *rettv);
785static void f_systemlist(typval_T *argvars, typval_T *rettv);
786static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
787static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
788static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
789static void f_taglist(typval_T *argvars, typval_T *rettv);
790static void f_tagfiles(typval_T *argvars, typval_T *rettv);
791static void f_tempname(typval_T *argvars, typval_T *rettv);
792static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200793#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100794static void f_tan(typval_T *argvars, typval_T *rettv);
795static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200796#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100797#ifdef FEAT_TIMERS
798static void f_timer_start(typval_T *argvars, typval_T *rettv);
799static void f_timer_stop(typval_T *argvars, typval_T *rettv);
800#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100801static void f_tolower(typval_T *argvars, typval_T *rettv);
802static void f_toupper(typval_T *argvars, typval_T *rettv);
803static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000804#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100805static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000806#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100807static void f_type(typval_T *argvars, typval_T *rettv);
808static void f_undofile(typval_T *argvars, typval_T *rettv);
809static void f_undotree(typval_T *argvars, typval_T *rettv);
810static void f_uniq(typval_T *argvars, typval_T *rettv);
811static void f_values(typval_T *argvars, typval_T *rettv);
812static void f_virtcol(typval_T *argvars, typval_T *rettv);
813static void f_visualmode(typval_T *argvars, typval_T *rettv);
814static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100815static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100816static void f_win_getid(typval_T *argvars, typval_T *rettv);
817static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
818static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
819static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100820static void f_winbufnr(typval_T *argvars, typval_T *rettv);
821static void f_wincol(typval_T *argvars, typval_T *rettv);
822static void f_winheight(typval_T *argvars, typval_T *rettv);
823static void f_winline(typval_T *argvars, typval_T *rettv);
824static void f_winnr(typval_T *argvars, typval_T *rettv);
825static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
826static void f_winrestview(typval_T *argvars, typval_T *rettv);
827static void f_winsaveview(typval_T *argvars, typval_T *rettv);
828static void f_winwidth(typval_T *argvars, typval_T *rettv);
829static void f_writefile(typval_T *argvars, typval_T *rettv);
830static void f_wordcount(typval_T *argvars, typval_T *rettv);
831static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000832
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100833static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
834static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
835static int get_env_len(char_u **arg);
836static int get_id_len(char_u **arg);
837static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
838static 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 +0000839#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
840#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
841 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100842static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
843static int eval_isnamec(int c);
844static int eval_isnamec1(int c);
845static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
846static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100847static typval_T *alloc_string_tv(char_u *string);
848static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100849#ifdef FEAT_FLOAT
850static float_T get_tv_float(typval_T *varp);
851#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100852static linenr_T get_tv_lnum(typval_T *argvars);
853static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100854static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
855static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
856static hashtab_T *find_var_ht(char_u *name, char_u **varname);
857static funccall_T *get_funccal(void);
858static void vars_clear_ext(hashtab_T *ht, int free_val);
859static void delete_var(hashtab_T *ht, hashitem_T *hi);
860static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
861static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
862static void set_var(char_u *name, typval_T *varp, int copy);
863static int var_check_ro(int flags, char_u *name, int use_gettext);
864static int var_check_fixed(int flags, char_u *name, int use_gettext);
865static int var_check_func_name(char_u *name, int new_var);
866static int valid_varname(char_u *varname);
867static int tv_check_lock(int lock, char_u *name, int use_gettext);
868static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
869static char_u *find_option_end(char_u **arg, int *opt_flags);
870static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
871static int eval_fname_script(char_u *p);
872static int eval_fname_sid(char_u *p);
873static void list_func_head(ufunc_T *fp, int indent);
874static ufunc_T *find_func(char_u *name);
875static int function_exists(char_u *name);
876static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000877#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100878static void func_do_profile(ufunc_T *fp);
879static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
880static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000881static int
882# ifdef __BORLANDC__
883 _RTLENTRYF
884# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100885 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000886static int
887# ifdef __BORLANDC__
888 _RTLENTRYF
889# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100890 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000891#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100892static int script_autoload(char_u *name, int reload);
893static char_u *autoload_name(char_u *name);
894static void cat_func_name(char_u *buf, ufunc_T *fp);
895static void func_free(ufunc_T *fp);
896static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
897static int can_free_funccal(funccall_T *fc, int copyID) ;
898static void free_funccal(funccall_T *fc, int free_val);
899static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
900static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
901static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
902static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
903static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
904static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
905static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
906static int write_list(FILE *fd, list_T *list, int binary);
907static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000908
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200909
910#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100911static int compare_func_name(const void *s1, const void *s2);
912static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200913#endif
914
Bram Moolenaar33570922005-01-25 22:26:29 +0000915/*
916 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000917 */
918 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100919eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000920{
Bram Moolenaar33570922005-01-25 22:26:29 +0000921 int i;
922 struct vimvar *p;
923
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200924 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
925 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200926 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000927 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000928 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000929
930 for (i = 0; i < VV_LEN; ++i)
931 {
932 p = &vimvars[i];
933 STRCPY(p->vv_di.di_key, p->vv_name);
934 if (p->vv_flags & VV_RO)
935 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
936 else if (p->vv_flags & VV_RO_SBX)
937 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
938 else
939 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000940
941 /* add to v: scope dict, unless the value is not always available */
942 if (p->vv_type != VAR_UNKNOWN)
943 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000944 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000945 /* add to compat scope dict */
946 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000947 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100948 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
949
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000950 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100951 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200952 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100953 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100954
955 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
956 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
957 set_vim_var_nr(VV_NONE, VVAL_NONE);
958 set_vim_var_nr(VV_NULL, VVAL_NULL);
959
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200960 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200961
962#ifdef EBCDIC
963 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100964 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200965 */
966 sortFunctions();
967#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000968}
969
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000970#if defined(EXITFREE) || defined(PROTO)
971 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100972eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000973{
974 int i;
975 struct vimvar *p;
976
977 for (i = 0; i < VV_LEN; ++i)
978 {
979 p = &vimvars[i];
980 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000981 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000982 vim_free(p->vv_str);
983 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000984 }
985 else if (p->vv_di.di_tv.v_type == VAR_LIST)
986 {
987 list_unref(p->vv_list);
988 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000989 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000990 }
991 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000992 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000993 hash_clear(&compat_hashtab);
994
Bram Moolenaard9fba312005-06-26 22:34:35 +0000995 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100996# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200997 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100998# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000999
1000 /* global variables */
1001 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001002
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001003 /* autoloaded script names */
1004 ga_clear_strings(&ga_loaded);
1005
Bram Moolenaarcca74132013-09-25 21:00:28 +02001006 /* Script-local variables. First clear all the variables and in a second
1007 * loop free the scriptvar_T, because a variable in one script might hold
1008 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001009 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001010 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001011 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001012 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001013 ga_clear(&ga_scripts);
1014
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001015 /* unreferenced lists and dicts */
1016 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001017
1018 /* functions */
1019 free_all_functions();
1020 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001021}
1022#endif
1023
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 * Return the name of the executed function.
1026 */
1027 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001028func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001030 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031}
1032
1033/*
1034 * Return the address holding the next breakpoint line for a funccall cookie.
1035 */
1036 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001037func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038{
Bram Moolenaar33570922005-01-25 22:26:29 +00001039 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040}
1041
1042/*
1043 * Return the address holding the debug tick for a funccall cookie.
1044 */
1045 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001046func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047{
Bram Moolenaar33570922005-01-25 22:26:29 +00001048 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049}
1050
1051/*
1052 * Return the nesting level for a funccall cookie.
1053 */
1054 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001055func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056{
Bram Moolenaar33570922005-01-25 22:26:29 +00001057 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058}
1059
1060/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001061funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001063/* pointer to list of previously used funccal, still around because some
1064 * item in it is still being used. */
1065funccall_T *previous_funccal = NULL;
1066
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067/*
1068 * Return TRUE when a function was ended by a ":return" command.
1069 */
1070 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001071current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072{
1073 return current_funccal->returned;
1074}
1075
1076
1077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 * Set an internal variable to a string value. Creates the variable if it does
1079 * not already exist.
1080 */
1081 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001082set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001084 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001085 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086
1087 val = vim_strsave(value);
1088 if (val != NULL)
1089 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001090 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001091 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001093 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001094 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 }
1096 }
1097}
1098
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001099static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001100static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101static char_u *redir_endp = NULL;
1102static char_u *redir_varname = NULL;
1103
1104/*
1105 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001106 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107 * Returns OK if successfully completed the setup. FAIL otherwise.
1108 */
1109 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001110var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111{
1112 int save_emsg;
1113 int err;
1114 typval_T tv;
1115
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001116 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001117 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118 {
1119 EMSG(_(e_invarg));
1120 return FAIL;
1121 }
1122
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001123 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001124 redir_varname = vim_strsave(name);
1125 if (redir_varname == NULL)
1126 return FAIL;
1127
1128 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1129 if (redir_lval == NULL)
1130 {
1131 var_redir_stop();
1132 return FAIL;
1133 }
1134
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001135 /* The output is stored in growarray "redir_ga" until redirection ends. */
1136 ga_init2(&redir_ga, (int)sizeof(char), 500);
1137
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001139 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001140 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1142 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001143 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 if (redir_endp != NULL && *redir_endp != NUL)
1145 /* Trailing characters are present after the variable name */
1146 EMSG(_(e_trailing));
1147 else
1148 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001149 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 var_redir_stop();
1151 return FAIL;
1152 }
1153
1154 /* check if we can write to the variable: set it to or append an empty
1155 * string */
1156 save_emsg = did_emsg;
1157 did_emsg = FALSE;
1158 tv.v_type = VAR_STRING;
1159 tv.vval.v_string = (char_u *)"";
1160 if (append)
1161 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1162 else
1163 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001164 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001166 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167 if (err)
1168 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001169 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001170 var_redir_stop();
1171 return FAIL;
1172 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001173
1174 return OK;
1175}
1176
1177/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001178 * Append "value[value_len]" to the variable set by var_redir_start().
1179 * The actual appending is postponed until redirection ends, because the value
1180 * appended may in fact be the string we write to, changing it may cause freed
1181 * memory to be used:
1182 * :redir => foo
1183 * :let foo
1184 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001185 */
1186 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001187var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001188{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001189 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001190
1191 if (redir_lval == NULL)
1192 return;
1193
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001194 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001195 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001196 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001197 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001198
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001199 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001200 {
1201 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001202 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001203 }
1204 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001205 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001206}
1207
1208/*
1209 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001210 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001211 */
1212 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001213var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001214{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001215 typval_T tv;
1216
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001217 if (redir_lval != NULL)
1218 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001219 /* If there was no error: assign the text to the variable. */
1220 if (redir_endp != NULL)
1221 {
1222 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1223 tv.v_type = VAR_STRING;
1224 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001225 /* Call get_lval() again, if it's inside a Dict or List it may
1226 * have changed. */
1227 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001228 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001229 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1230 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1231 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001232 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001233
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001234 /* free the collected output */
1235 vim_free(redir_ga.ga_data);
1236 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001237
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001238 vim_free(redir_lval);
1239 redir_lval = NULL;
1240 }
1241 vim_free(redir_varname);
1242 redir_varname = NULL;
1243}
1244
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245# if defined(FEAT_MBYTE) || defined(PROTO)
1246 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001247eval_charconvert(
1248 char_u *enc_from,
1249 char_u *enc_to,
1250 char_u *fname_from,
1251 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252{
1253 int err = FALSE;
1254
1255 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1256 set_vim_var_string(VV_CC_TO, enc_to, -1);
1257 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1258 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1259 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1260 err = TRUE;
1261 set_vim_var_string(VV_CC_FROM, NULL, -1);
1262 set_vim_var_string(VV_CC_TO, NULL, -1);
1263 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1264 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1265
1266 if (err)
1267 return FAIL;
1268 return OK;
1269}
1270# endif
1271
1272# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1273 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001274eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275{
1276 int err = FALSE;
1277
1278 set_vim_var_string(VV_FNAME_IN, fname, -1);
1279 set_vim_var_string(VV_CMDARG, args, -1);
1280 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1281 err = TRUE;
1282 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1283 set_vim_var_string(VV_CMDARG, NULL, -1);
1284
1285 if (err)
1286 {
1287 mch_remove(fname);
1288 return FAIL;
1289 }
1290 return OK;
1291}
1292# endif
1293
1294# if defined(FEAT_DIFF) || defined(PROTO)
1295 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001296eval_diff(
1297 char_u *origfile,
1298 char_u *newfile,
1299 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300{
1301 int err = FALSE;
1302
1303 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1304 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1305 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1306 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1307 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1308 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1309 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1310}
1311
1312 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001313eval_patch(
1314 char_u *origfile,
1315 char_u *difffile,
1316 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317{
1318 int err;
1319
1320 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1321 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1322 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1323 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1324 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1325 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1326 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1327}
1328# endif
1329
1330/*
1331 * Top level evaluation function, returning a boolean.
1332 * Sets "error" to TRUE if there was an error.
1333 * Return TRUE or FALSE.
1334 */
1335 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001336eval_to_bool(
1337 char_u *arg,
1338 int *error,
1339 char_u **nextcmd,
1340 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341{
Bram Moolenaar33570922005-01-25 22:26:29 +00001342 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 int retval = FALSE;
1344
1345 if (skip)
1346 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001347 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 else
1350 {
1351 *error = FALSE;
1352 if (!skip)
1353 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001354 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001355 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 }
1357 }
1358 if (skip)
1359 --emsg_skip;
1360
1361 return retval;
1362}
1363
1364/*
1365 * Top level evaluation function, returning a string. If "skip" is TRUE,
1366 * only parsing to "nextcmd" is done, without reporting errors. Return
1367 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1368 */
1369 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001370eval_to_string_skip(
1371 char_u *arg,
1372 char_u **nextcmd,
1373 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374{
Bram Moolenaar33570922005-01-25 22:26:29 +00001375 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 char_u *retval;
1377
1378 if (skip)
1379 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001380 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 retval = NULL;
1382 else
1383 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001384 retval = vim_strsave(get_tv_string(&tv));
1385 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 }
1387 if (skip)
1388 --emsg_skip;
1389
1390 return retval;
1391}
1392
1393/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001394 * Skip over an expression at "*pp".
1395 * Return FAIL for an error, OK otherwise.
1396 */
1397 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001398skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001399{
Bram Moolenaar33570922005-01-25 22:26:29 +00001400 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001401
1402 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001403 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001404}
1405
1406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001408 * When "convert" is TRUE convert a List into a sequence of lines and convert
1409 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 * Return pointer to allocated memory, or NULL for failure.
1411 */
1412 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001413eval_to_string(
1414 char_u *arg,
1415 char_u **nextcmd,
1416 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417{
Bram Moolenaar33570922005-01-25 22:26:29 +00001418 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001420 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001421#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001422 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001425 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 retval = NULL;
1427 else
1428 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001429 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001430 {
1431 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001432 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001433 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001434 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001435 if (tv.vval.v_list->lv_len > 0)
1436 ga_append(&ga, NL);
1437 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001438 ga_append(&ga, NUL);
1439 retval = (char_u *)ga.ga_data;
1440 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001441#ifdef FEAT_FLOAT
1442 else if (convert && tv.v_type == VAR_FLOAT)
1443 {
1444 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1445 retval = vim_strsave(numbuf);
1446 }
1447#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001448 else
1449 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001450 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 }
1452
1453 return retval;
1454}
1455
1456/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001457 * Call eval_to_string() without using current local variables and using
1458 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 */
1460 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001461eval_to_string_safe(
1462 char_u *arg,
1463 char_u **nextcmd,
1464 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465{
1466 char_u *retval;
1467 void *save_funccalp;
1468
1469 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001470 if (use_sandbox)
1471 ++sandbox;
1472 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001473 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001474 if (use_sandbox)
1475 --sandbox;
1476 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 restore_funccal(save_funccalp);
1478 return retval;
1479}
1480
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481/*
1482 * Top level evaluation function, returning a number.
1483 * Evaluates "expr" silently.
1484 * Returns -1 for an error.
1485 */
1486 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001487eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488{
Bram Moolenaar33570922005-01-25 22:26:29 +00001489 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001491 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492
1493 ++emsg_off;
1494
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001495 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 retval = -1;
1497 else
1498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001499 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001500 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 }
1502 --emsg_off;
1503
1504 return retval;
1505}
1506
Bram Moolenaara40058a2005-07-11 22:42:07 +00001507/*
1508 * Prepare v: variable "idx" to be used.
1509 * Save the current typeval in "save_tv".
1510 * When not used yet add the variable to the v: hashtable.
1511 */
1512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001513prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001514{
1515 *save_tv = vimvars[idx].vv_tv;
1516 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1517 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1518}
1519
1520/*
1521 * Restore v: variable "idx" to typeval "save_tv".
1522 * When no longer defined, remove the variable from the v: hashtable.
1523 */
1524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001525restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001526{
1527 hashitem_T *hi;
1528
Bram Moolenaara40058a2005-07-11 22:42:07 +00001529 vimvars[idx].vv_tv = *save_tv;
1530 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1531 {
1532 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1533 if (HASHITEM_EMPTY(hi))
1534 EMSG2(_(e_intern2), "restore_vimvar()");
1535 else
1536 hash_remove(&vimvarht, hi);
1537 }
1538}
1539
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001540#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001541/*
1542 * Evaluate an expression to a list with suggestions.
1543 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001544 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001545 */
1546 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001547eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001548{
1549 typval_T save_val;
1550 typval_T rettv;
1551 list_T *list = NULL;
1552 char_u *p = skipwhite(expr);
1553
1554 /* Set "v:val" to the bad word. */
1555 prepare_vimvar(VV_VAL, &save_val);
1556 vimvars[VV_VAL].vv_type = VAR_STRING;
1557 vimvars[VV_VAL].vv_str = badword;
1558 if (p_verbose == 0)
1559 ++emsg_off;
1560
1561 if (eval1(&p, &rettv, TRUE) == OK)
1562 {
1563 if (rettv.v_type != VAR_LIST)
1564 clear_tv(&rettv);
1565 else
1566 list = rettv.vval.v_list;
1567 }
1568
1569 if (p_verbose == 0)
1570 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001571 restore_vimvar(VV_VAL, &save_val);
1572
1573 return list;
1574}
1575
1576/*
1577 * "list" is supposed to contain two items: a word and a number. Return the
1578 * word in "pp" and the number as the return value.
1579 * Return -1 if anything isn't right.
1580 * Used to get the good word and score from the eval_spell_expr() result.
1581 */
1582 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001583get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001584{
1585 listitem_T *li;
1586
1587 li = list->lv_first;
1588 if (li == NULL)
1589 return -1;
1590 *pp = get_tv_string(&li->li_tv);
1591
1592 li = li->li_next;
1593 if (li == NULL)
1594 return -1;
1595 return get_tv_number(&li->li_tv);
1596}
1597#endif
1598
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001599/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001600 * Top level evaluation function.
1601 * Returns an allocated typval_T with the result.
1602 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001603 */
1604 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001605eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001606{
1607 typval_T *tv;
1608
1609 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001610 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001611 {
1612 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001613 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001614 }
1615
1616 return tv;
1617}
1618
1619
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001621 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001622 * Uses argv[argc] for the function arguments. Only Number and String
1623 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001624 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001626 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001627call_vim_function(
1628 char_u *func,
1629 int argc,
1630 char_u **argv,
1631 int safe, /* use the sandbox */
1632 int str_arg_only, /* all arguments are strings */
1633 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634{
Bram Moolenaar33570922005-01-25 22:26:29 +00001635 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 long n;
1637 int len;
1638 int i;
1639 int doesrange;
1640 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001641 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001643 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001645 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
1647 for (i = 0; i < argc; i++)
1648 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001649 /* Pass a NULL or empty argument as an empty string */
1650 if (argv[i] == NULL || *argv[i] == NUL)
1651 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001652 argvars[i].v_type = VAR_STRING;
1653 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001654 continue;
1655 }
1656
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001657 if (str_arg_only)
1658 len = 0;
1659 else
1660 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001661 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 if (len != 0 && len == (int)STRLEN(argv[i]))
1663 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001664 argvars[i].v_type = VAR_NUMBER;
1665 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 }
1667 else
1668 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001669 argvars[i].v_type = VAR_STRING;
1670 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 }
1672 }
1673
1674 if (safe)
1675 {
1676 save_funccalp = save_funccal();
1677 ++sandbox;
1678 }
1679
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001680 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1681 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001683 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 if (safe)
1685 {
1686 --sandbox;
1687 restore_funccal(save_funccalp);
1688 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 vim_free(argvars);
1690
1691 if (ret == FAIL)
1692 clear_tv(rettv);
1693
1694 return ret;
1695}
1696
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001697/*
1698 * Call vimL function "func" and return the result as a number.
1699 * Returns -1 when calling the function fails.
1700 * Uses argv[argc] for the function arguments.
1701 */
1702 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001703call_func_retnr(
1704 char_u *func,
1705 int argc,
1706 char_u **argv,
1707 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001708{
1709 typval_T rettv;
1710 long retval;
1711
1712 /* All arguments are passed as strings, no conversion to number. */
1713 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1714 return -1;
1715
1716 retval = get_tv_number_chk(&rettv, NULL);
1717 clear_tv(&rettv);
1718 return retval;
1719}
1720
1721#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1722 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1723
Bram Moolenaar4f688582007-07-24 12:34:30 +00001724# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001725/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001726 * Call vimL function "func" and return the result as a string.
1727 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001728 * Uses argv[argc] for the function arguments.
1729 */
1730 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001731call_func_retstr(
1732 char_u *func,
1733 int argc,
1734 char_u **argv,
1735 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001736{
1737 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001738 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001739
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001740 /* All arguments are passed as strings, no conversion to number. */
1741 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001742 return NULL;
1743
1744 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001745 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 return retval;
1747}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001748# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001749
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001750/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001751 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001752 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001753 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001754 */
1755 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001756call_func_retlist(
1757 char_u *func,
1758 int argc,
1759 char_u **argv,
1760 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001761{
1762 typval_T rettv;
1763
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001764 /* All arguments are passed as strings, no conversion to number. */
1765 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001766 return NULL;
1767
1768 if (rettv.v_type != VAR_LIST)
1769 {
1770 clear_tv(&rettv);
1771 return NULL;
1772 }
1773
1774 return rettv.vval.v_list;
1775}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776#endif
1777
1778/*
1779 * Save the current function call pointer, and set it to NULL.
1780 * Used when executing autocommands and for ":source".
1781 */
1782 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001783save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001785 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 current_funccal = NULL;
1788 return (void *)fc;
1789}
1790
1791 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001792restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001794 funccall_T *fc = (funccall_T *)vfc;
1795
1796 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797}
1798
Bram Moolenaar05159a02005-02-26 23:04:13 +00001799#if defined(FEAT_PROFILE) || defined(PROTO)
1800/*
1801 * Prepare profiling for entering a child or something else that is not
1802 * counted for the script/function itself.
1803 * Should always be called in pair with prof_child_exit().
1804 */
1805 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001806prof_child_enter(
1807 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001808{
1809 funccall_T *fc = current_funccal;
1810
1811 if (fc != NULL && fc->func->uf_profiling)
1812 profile_start(&fc->prof_child);
1813 script_prof_save(tm);
1814}
1815
1816/*
1817 * Take care of time spent in a child.
1818 * Should always be called after prof_child_enter().
1819 */
1820 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001821prof_child_exit(
1822 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001823{
1824 funccall_T *fc = current_funccal;
1825
1826 if (fc != NULL && fc->func->uf_profiling)
1827 {
1828 profile_end(&fc->prof_child);
1829 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1830 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1831 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1832 }
1833 script_prof_restore(tm);
1834}
1835#endif
1836
1837
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838#ifdef FEAT_FOLDING
1839/*
1840 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1841 * it in "*cp". Doesn't give error messages.
1842 */
1843 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001844eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845{
Bram Moolenaar33570922005-01-25 22:26:29 +00001846 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 int retval;
1848 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001849 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1850 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851
1852 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001853 if (use_sandbox)
1854 ++sandbox;
1855 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 retval = 0;
1859 else
1860 {
1861 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001862 if (tv.v_type == VAR_NUMBER)
1863 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001864 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 retval = 0;
1866 else
1867 {
1868 /* If the result is a string, check if there is a non-digit before
1869 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001870 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 if (!VIM_ISDIGIT(*s) && *s != '-')
1872 *cp = *s++;
1873 retval = atol((char *)s);
1874 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 }
1877 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001878 if (use_sandbox)
1879 --sandbox;
1880 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881
1882 return retval;
1883}
1884#endif
1885
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001887 * ":let" list all variable values
1888 * ":let var1 var2" list variable values
1889 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001890 * ":let var += expr" assignment command.
1891 * ":let var -= expr" assignment command.
1892 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 */
1895 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001896ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897{
1898 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001900 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 int var_count = 0;
1903 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001904 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001905 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001906 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907
Bram Moolenaardb552d602006-03-23 22:59:57 +00001908 argend = skip_var_list(arg, &var_count, &semicolon);
1909 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001910 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001911 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1912 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001913 expr = skipwhite(argend);
1914 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1915 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001917 /*
1918 * ":let" without "=": list variables
1919 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001920 if (*arg == '[')
1921 EMSG(_(e_invarg));
1922 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001924 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001926 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001927 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001928 list_glob_vars(&first);
1929 list_buf_vars(&first);
1930 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001931#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001932 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001933#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001934 list_script_vars(&first);
1935 list_func_vars(&first);
1936 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 eap->nextcmd = check_nextcmd(arg);
1939 }
1940 else
1941 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001942 op[0] = '=';
1943 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001944 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001945 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001946 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1947 op[0] = *expr; /* +=, -= or .= */
1948 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001949 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001950 else
1951 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001952
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 if (eap->skip)
1954 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 if (eap->skip)
1957 {
1958 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 --emsg_skip;
1961 }
1962 else if (i != FAIL)
1963 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001965 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001966 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 }
1968 }
1969}
1970
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001971/*
1972 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1973 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001974 * When "nextchars" is not NULL it points to a string with characters that
1975 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1976 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001977 * Returns OK or FAIL;
1978 */
1979 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001980ex_let_vars(
1981 char_u *arg_start,
1982 typval_T *tv,
1983 int copy, /* copy values from "tv", don't move */
1984 int semicolon, /* from skip_var_list() */
1985 int var_count, /* from skip_var_list() */
1986 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001987{
1988 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001989 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001991 listitem_T *item;
1992 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001993
1994 if (*arg != '[')
1995 {
1996 /*
1997 * ":let var = expr" or ":for var in list"
1998 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001999 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002000 return FAIL;
2001 return OK;
2002 }
2003
2004 /*
2005 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2006 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002007 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 {
2009 EMSG(_(e_listreq));
2010 return FAIL;
2011 }
2012
2013 i = list_len(l);
2014 if (semicolon == 0 && var_count < i)
2015 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002016 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002017 return FAIL;
2018 }
2019 if (var_count - semicolon > i)
2020 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002021 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002022 return FAIL;
2023 }
2024
2025 item = l->lv_first;
2026 while (*arg != ']')
2027 {
2028 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002029 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002030 item = item->li_next;
2031 if (arg == NULL)
2032 return FAIL;
2033
2034 arg = skipwhite(arg);
2035 if (*arg == ';')
2036 {
2037 /* Put the rest of the list (may be empty) in the var after ';'.
2038 * Create a new list for this. */
2039 l = list_alloc();
2040 if (l == NULL)
2041 return FAIL;
2042 while (item != NULL)
2043 {
2044 list_append_tv(l, &item->li_tv);
2045 item = item->li_next;
2046 }
2047
2048 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002049 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002050 ltv.vval.v_list = l;
2051 l->lv_refcount = 1;
2052
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002053 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2054 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002055 clear_tv(&ltv);
2056 if (arg == NULL)
2057 return FAIL;
2058 break;
2059 }
2060 else if (*arg != ',' && *arg != ']')
2061 {
2062 EMSG2(_(e_intern2), "ex_let_vars()");
2063 return FAIL;
2064 }
2065 }
2066
2067 return OK;
2068}
2069
2070/*
2071 * Skip over assignable variable "var" or list of variables "[var, var]".
2072 * Used for ":let varvar = expr" and ":for varvar in expr".
2073 * For "[var, var]" increment "*var_count" for each variable.
2074 * for "[var, var; var]" set "semicolon".
2075 * Return NULL for an error.
2076 */
2077 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002078skip_var_list(
2079 char_u *arg,
2080 int *var_count,
2081 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002082{
2083 char_u *p, *s;
2084
2085 if (*arg == '[')
2086 {
2087 /* "[var, var]": find the matching ']'. */
2088 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002089 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002090 {
2091 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2092 s = skip_var_one(p);
2093 if (s == p)
2094 {
2095 EMSG2(_(e_invarg2), p);
2096 return NULL;
2097 }
2098 ++*var_count;
2099
2100 p = skipwhite(s);
2101 if (*p == ']')
2102 break;
2103 else if (*p == ';')
2104 {
2105 if (*semicolon == 1)
2106 {
2107 EMSG(_("Double ; in list of variables"));
2108 return NULL;
2109 }
2110 *semicolon = 1;
2111 }
2112 else if (*p != ',')
2113 {
2114 EMSG2(_(e_invarg2), p);
2115 return NULL;
2116 }
2117 }
2118 return p + 1;
2119 }
2120 else
2121 return skip_var_one(arg);
2122}
2123
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002124/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002125 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002126 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002127 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002128 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002129skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002130{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002131 if (*arg == '@' && arg[1] != NUL)
2132 return arg + 2;
2133 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2134 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002135}
2136
Bram Moolenaara7043832005-01-21 11:56:39 +00002137/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002138 * List variables for hashtab "ht" with prefix "prefix".
2139 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002140 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002142list_hashtable_vars(
2143 hashtab_T *ht,
2144 char_u *prefix,
2145 int empty,
2146 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002147{
Bram Moolenaar33570922005-01-25 22:26:29 +00002148 hashitem_T *hi;
2149 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002150 int todo;
2151
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002152 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002153 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2154 {
2155 if (!HASHITEM_EMPTY(hi))
2156 {
2157 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002158 di = HI2DI(hi);
2159 if (empty || di->di_tv.v_type != VAR_STRING
2160 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002162 }
2163 }
2164}
2165
2166/*
2167 * List global variables.
2168 */
2169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002170list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002171{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002173}
2174
2175/*
2176 * List buffer variables.
2177 */
2178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002179list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002180{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002181 char_u numbuf[NUMBUFLEN];
2182
Bram Moolenaar429fa852013-04-15 12:27:36 +02002183 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002185
2186 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002187 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2188 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002189}
2190
2191/*
2192 * List window variables.
2193 */
2194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002195list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002196{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002197 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002199}
2200
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002201#ifdef FEAT_WINDOWS
2202/*
2203 * List tab page variables.
2204 */
2205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002206list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002207{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002208 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002209 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002210}
2211#endif
2212
Bram Moolenaara7043832005-01-21 11:56:39 +00002213/*
2214 * List Vim variables.
2215 */
2216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002217list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002219 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220}
2221
2222/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002223 * List script-local variables, if there is a script.
2224 */
2225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002226list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002227{
2228 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002229 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2230 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002231}
2232
2233/*
2234 * List function variables, if there is a function.
2235 */
2236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002237list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002238{
2239 if (current_funccal != NULL)
2240 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002241 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002242}
2243
2244/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 * List variables in "arg".
2246 */
2247 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002248list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249{
2250 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002251 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 char_u *name_start;
2254 char_u *arg_subsc;
2255 char_u *tofree;
2256 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257
2258 while (!ends_excmd(*arg) && !got_int)
2259 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002262 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2264 {
2265 emsg_severe = TRUE;
2266 EMSG(_(e_trailing));
2267 break;
2268 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 /* get_name_len() takes care of expanding curly braces */
2273 name_start = name = arg;
2274 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2275 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002277 /* This is mainly to keep test 49 working: when expanding
2278 * curly braces fails overrule the exception error message. */
2279 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002281 emsg_severe = TRUE;
2282 EMSG2(_(e_invarg2), arg);
2283 break;
2284 }
2285 error = TRUE;
2286 }
2287 else
2288 {
2289 if (tofree != NULL)
2290 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002291 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 else
2294 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 /* handle d.key, l[idx], f(expr) */
2296 arg_subsc = arg;
2297 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002298 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002299 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002300 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002301 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002302 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002304 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002305 case 'g': list_glob_vars(first); break;
2306 case 'b': list_buf_vars(first); break;
2307 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002308#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002309 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002310#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002311 case 'v': list_vim_vars(first); break;
2312 case 's': list_script_vars(first); break;
2313 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002314 default:
2315 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002316 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002317 }
2318 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002319 {
2320 char_u numbuf[NUMBUFLEN];
2321 char_u *tf;
2322 int c;
2323 char_u *s;
2324
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002325 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002326 c = *arg;
2327 *arg = NUL;
2328 list_one_var_a((char_u *)"",
2329 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002330 tv.v_type,
2331 s == NULL ? (char_u *)"" : s,
2332 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002333 *arg = c;
2334 vim_free(tf);
2335 }
2336 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002337 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002338 }
2339 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002340
2341 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002343
2344 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345 }
2346
2347 return arg;
2348}
2349
2350/*
2351 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2352 * Returns a pointer to the char just after the var name.
2353 * Returns NULL if there is an error.
2354 */
2355 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002356ex_let_one(
2357 char_u *arg, /* points to variable name */
2358 typval_T *tv, /* value to assign to variable */
2359 int copy, /* copy value from "tv" */
2360 char_u *endchars, /* valid chars after variable name or NULL */
2361 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002362{
2363 int c1;
2364 char_u *name;
2365 char_u *p;
2366 char_u *arg_end = NULL;
2367 int len;
2368 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002369 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002370
2371 /*
2372 * ":let $VAR = expr": Set environment variable.
2373 */
2374 if (*arg == '$')
2375 {
2376 /* Find the end of the name. */
2377 ++arg;
2378 name = arg;
2379 len = get_env_len(&arg);
2380 if (len == 0)
2381 EMSG2(_(e_invarg2), name - 1);
2382 else
2383 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002384 if (op != NULL && (*op == '+' || *op == '-'))
2385 EMSG2(_(e_letwrong), op);
2386 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002387 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002388 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002389 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 {
2391 c1 = name[len];
2392 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002393 p = get_tv_string_chk(tv);
2394 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002395 {
2396 int mustfree = FALSE;
2397 char_u *s = vim_getenv(name, &mustfree);
2398
2399 if (s != NULL)
2400 {
2401 p = tofree = concat_str(s, p);
2402 if (mustfree)
2403 vim_free(s);
2404 }
2405 }
2406 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002407 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002408 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002409 if (STRICMP(name, "HOME") == 0)
2410 init_homedir();
2411 else if (didset_vim && STRICMP(name, "VIM") == 0)
2412 didset_vim = FALSE;
2413 else if (didset_vimruntime
2414 && STRICMP(name, "VIMRUNTIME") == 0)
2415 didset_vimruntime = FALSE;
2416 arg_end = arg;
2417 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002418 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002420 }
2421 }
2422 }
2423
2424 /*
2425 * ":let &option = expr": Set option value.
2426 * ":let &l:option = expr": Set local option value.
2427 * ":let &g:option = expr": Set global option value.
2428 */
2429 else if (*arg == '&')
2430 {
2431 /* Find the end of the name. */
2432 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002433 if (p == NULL || (endchars != NULL
2434 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002435 EMSG(_(e_letunexp));
2436 else
2437 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 long n;
2439 int opt_type;
2440 long numval;
2441 char_u *stringval = NULL;
2442 char_u *s;
2443
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002444 c1 = *p;
2445 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446
2447 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002448 s = get_tv_string_chk(tv); /* != NULL if number or string */
2449 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 {
2451 opt_type = get_option_value(arg, &numval,
2452 &stringval, opt_flags);
2453 if ((opt_type == 1 && *op == '.')
2454 || (opt_type == 0 && *op != '.'))
2455 EMSG2(_(e_letwrong), op);
2456 else
2457 {
2458 if (opt_type == 1) /* number */
2459 {
2460 if (*op == '+')
2461 n = numval + n;
2462 else
2463 n = numval - n;
2464 }
2465 else if (opt_type == 0 && stringval != NULL) /* string */
2466 {
2467 s = concat_str(stringval, s);
2468 vim_free(stringval);
2469 stringval = s;
2470 }
2471 }
2472 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002473 if (s != NULL)
2474 {
2475 set_option_value(arg, n, s, opt_flags);
2476 arg_end = p;
2477 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002480 }
2481 }
2482
2483 /*
2484 * ":let @r = expr": Set register contents.
2485 */
2486 else if (*arg == '@')
2487 {
2488 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002489 if (op != NULL && (*op == '+' || *op == '-'))
2490 EMSG2(_(e_letwrong), op);
2491 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002492 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002493 EMSG(_(e_letunexp));
2494 else
2495 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002496 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002497 char_u *s;
2498
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002499 p = get_tv_string_chk(tv);
2500 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002501 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002502 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002503 if (s != NULL)
2504 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002505 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002506 vim_free(s);
2507 }
2508 }
2509 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002510 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002511 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002512 arg_end = arg + 1;
2513 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002514 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 }
2516 }
2517
2518 /*
2519 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002522 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002524 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002526 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002528 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2530 EMSG(_(e_letunexp));
2531 else
2532 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002533 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 arg_end = p;
2535 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002536 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002538 }
2539
2540 else
2541 EMSG2(_(e_invarg2), arg);
2542
2543 return arg_end;
2544}
2545
2546/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002547 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2548 */
2549 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002550check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002551{
2552 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2553 {
2554 EMSG2(_(e_readonlyvar), arg);
2555 return TRUE;
2556 }
2557 return FALSE;
2558}
2559
2560/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 * Get an lval: variable, Dict item or List item that can be assigned a value
2562 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2563 * "name.key", "name.key[expr]" etc.
2564 * Indexing only works if "name" is an existing List or Dictionary.
2565 * "name" points to the start of the name.
2566 * If "rettv" is not NULL it points to the value to be assigned.
2567 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2568 * wrong; must end in space or cmd separator.
2569 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002570 * flags:
2571 * GLV_QUIET: do not give error messages
2572 * GLV_NO_AUTOLOAD: do not use script autoloading
2573 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002575 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002577 */
2578 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002579get_lval(
2580 char_u *name,
2581 typval_T *rettv,
2582 lval_T *lp,
2583 int unlet,
2584 int skip,
2585 int flags, /* GLV_ values */
2586 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002587{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002588 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 char_u *expr_start, *expr_end;
2590 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002591 dictitem_T *v;
2592 typval_T var1;
2593 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002595 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002596 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002597 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002598 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002599 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002600
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002602 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603
2604 if (skip)
2605 {
2606 /* When skipping just find the end of the name. */
2607 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002608 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 }
2610
2611 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002612 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 if (expr_start != NULL)
2614 {
2615 /* Don't expand the name when we already know there is an error. */
2616 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2617 && *p != '[' && *p != '.')
2618 {
2619 EMSG(_(e_trailing));
2620 return NULL;
2621 }
2622
2623 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2624 if (lp->ll_exp_name == NULL)
2625 {
2626 /* Report an invalid expression in braces, unless the
2627 * expression evaluation has been cancelled due to an
2628 * aborting error, an interrupt, or an exception. */
2629 if (!aborting() && !quiet)
2630 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002631 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 EMSG2(_(e_invarg2), name);
2633 return NULL;
2634 }
2635 }
2636 lp->ll_name = lp->ll_exp_name;
2637 }
2638 else
2639 lp->ll_name = name;
2640
2641 /* Without [idx] or .key we are done. */
2642 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2643 return p;
2644
2645 cc = *p;
2646 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002647 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (v == NULL && !quiet)
2649 EMSG2(_(e_undefvar), lp->ll_name);
2650 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002651 if (v == NULL)
2652 return NULL;
2653
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 /*
2655 * Loop until no more [idx] or .key is following.
2656 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002657 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002659 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2661 && !(lp->ll_tv->v_type == VAR_DICT
2662 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (!quiet)
2665 EMSG(_("E689: Can only index a List or Dictionary"));
2666 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002667 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002669 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 if (!quiet)
2671 EMSG(_("E708: [:] must come last"));
2672 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002673 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002674
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 len = -1;
2676 if (*p == '.')
2677 {
2678 key = p + 1;
2679 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2680 ;
2681 if (len == 0)
2682 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 if (!quiet)
2684 EMSG(_(e_emptykey));
2685 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 }
2687 p = key + len;
2688 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002689 else
2690 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 if (*p == ':')
2694 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002695 else
2696 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 empty1 = FALSE;
2698 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002700 if (get_tv_string_chk(&var1) == NULL)
2701 {
2702 /* not a number or string */
2703 clear_tv(&var1);
2704 return NULL;
2705 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 }
2707
2708 /* Optionally get the second index [ :expr]. */
2709 if (*p == ':')
2710 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002714 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002715 if (!empty1)
2716 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002718 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (rettv != NULL && (rettv->v_type != VAR_LIST
2720 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 if (!quiet)
2723 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 if (!empty1)
2725 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 }
2728 p = skipwhite(p + 1);
2729 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 else
2732 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2735 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 if (!empty1)
2737 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002740 if (get_tv_string_chk(&var2) == NULL)
2741 {
2742 /* not a number or string */
2743 if (!empty1)
2744 clear_tv(&var1);
2745 clear_tv(&var2);
2746 return NULL;
2747 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002750 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002753
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002755 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 if (!quiet)
2757 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 if (!empty1)
2759 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 }
2764
2765 /* Skip to past ']'. */
2766 ++p;
2767 }
2768
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 {
2771 if (len == -1)
2772 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002774 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 if (*key == NUL)
2776 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 if (!quiet)
2778 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 }
2782 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002783 lp->ll_list = NULL;
2784 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002785 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002786
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002787 /* When assigning to a scope dictionary check that a function and
2788 * variable name is valid (only variable name unless it is l: or
2789 * g: dictionary). Disallow overwriting a builtin function. */
2790 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002791 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002792 int prevval;
2793 int wrong;
2794
2795 if (len != -1)
2796 {
2797 prevval = key[len];
2798 key[len] = NUL;
2799 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002800 else
2801 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002802 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2803 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002804 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002805 || !valid_varname(key);
2806 if (len != -1)
2807 key[len] = prevval;
2808 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002809 return NULL;
2810 }
2811
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002814 /* Can't add "v:" variable. */
2815 if (lp->ll_dict == &vimvardict)
2816 {
2817 EMSG2(_(e_illvar), name);
2818 return NULL;
2819 }
2820
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002821 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002825 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 if (len == -1)
2827 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002829 }
2830 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 if (len == -1)
2835 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 p = NULL;
2838 break;
2839 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002840 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002841 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002842 return NULL;
2843
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 if (len == -1)
2845 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 }
2848 else
2849 {
2850 /*
2851 * Get the number and item for the only or first index of the List.
2852 */
2853 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 else
2856 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002857 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 clear_tv(&var1);
2859 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002860 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 lp->ll_list = lp->ll_tv->vval.v_list;
2862 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2863 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002865 if (lp->ll_n1 < 0)
2866 {
2867 lp->ll_n1 = 0;
2868 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2869 }
2870 }
2871 if (lp->ll_li == NULL)
2872 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002875 if (!quiet)
2876 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 }
2879
2880 /*
2881 * May need to find the item or absolute index for the second
2882 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 * When no index given: "lp->ll_empty2" is TRUE.
2884 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002885 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002888 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002891 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002893 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002894 {
2895 if (!quiet)
2896 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002898 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002900 }
2901
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2903 if (lp->ll_n1 < 0)
2904 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2905 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002906 {
2907 if (!quiet)
2908 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002910 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002911 }
2912
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002914 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002915 }
2916
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 return p;
2918}
2919
2920/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002921 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 */
2923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002924clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925{
2926 vim_free(lp->ll_exp_name);
2927 vim_free(lp->ll_newkey);
2928}
2929
2930/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002931 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002933 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 */
2935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002936set_var_lval(
2937 lval_T *lp,
2938 char_u *endp,
2939 typval_T *rettv,
2940 int copy,
2941 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942{
2943 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002944 listitem_T *ri;
2945 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946
2947 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002948 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002950 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 cc = *endp;
2952 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002953 if (op != NULL && *op != '=')
2954 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002955 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002956
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002957 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002958 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002959 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002960 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002961 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002962 if ((di == NULL
2963 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2964 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2965 FALSE)))
2966 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002967 set_var(lp->ll_name, &tv, FALSE);
2968 clear_tv(&tv);
2969 }
2970 }
2971 else
2972 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002973 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002974 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002975 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002976 else if (tv_check_lock(lp->ll_newkey == NULL
2977 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002978 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002979 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 else if (lp->ll_range)
2981 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002982 listitem_T *ll_li = lp->ll_li;
2983 int ll_n1 = lp->ll_n1;
2984
2985 /*
2986 * Check whether any of the list items is locked
2987 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002988 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002989 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002990 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002991 return;
2992 ri = ri->li_next;
2993 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2994 break;
2995 ll_li = ll_li->li_next;
2996 ++ll_n1;
2997 }
2998
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002999 /*
3000 * Assign the List values to the list items.
3001 */
3002 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003003 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003004 if (op != NULL && *op != '=')
3005 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3006 else
3007 {
3008 clear_tv(&lp->ll_li->li_tv);
3009 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3010 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003011 ri = ri->li_next;
3012 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3013 break;
3014 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003015 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003016 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003017 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003018 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003019 ri = NULL;
3020 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003021 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003022 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003023 lp->ll_li = lp->ll_li->li_next;
3024 ++lp->ll_n1;
3025 }
3026 if (ri != NULL)
3027 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003028 else if (lp->ll_empty2
3029 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003030 : lp->ll_n1 != lp->ll_n2)
3031 EMSG(_("E711: List value has not enough items"));
3032 }
3033 else
3034 {
3035 /*
3036 * Assign to a List or Dictionary item.
3037 */
3038 if (lp->ll_newkey != NULL)
3039 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003040 if (op != NULL && *op != '=')
3041 {
3042 EMSG2(_(e_letwrong), op);
3043 return;
3044 }
3045
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003046 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003047 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003048 if (di == NULL)
3049 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003050 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3051 {
3052 vim_free(di);
3053 return;
3054 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003055 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003056 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003057 else if (op != NULL && *op != '=')
3058 {
3059 tv_op(lp->ll_tv, rettv, op);
3060 return;
3061 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003062 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003063 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003064
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003065 /*
3066 * Assign the value to the variable or list item.
3067 */
3068 if (copy)
3069 copy_tv(rettv, lp->ll_tv);
3070 else
3071 {
3072 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003073 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003074 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003075 }
3076 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003077}
3078
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003079/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003080 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3081 * Returns OK or FAIL.
3082 */
3083 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003084tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003085{
3086 long n;
3087 char_u numbuf[NUMBUFLEN];
3088 char_u *s;
3089
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003090 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3091 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3092 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003093 {
3094 switch (tv1->v_type)
3095 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003096 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003097 case VAR_DICT:
3098 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003099 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003100 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003101 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003102 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003103 break;
3104
3105 case VAR_LIST:
3106 if (*op != '+' || tv2->v_type != VAR_LIST)
3107 break;
3108 /* List += List */
3109 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3110 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3111 return OK;
3112
3113 case VAR_NUMBER:
3114 case VAR_STRING:
3115 if (tv2->v_type == VAR_LIST)
3116 break;
3117 if (*op == '+' || *op == '-')
3118 {
3119 /* nr += nr or nr -= nr*/
3120 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003121#ifdef FEAT_FLOAT
3122 if (tv2->v_type == VAR_FLOAT)
3123 {
3124 float_T f = n;
3125
3126 if (*op == '+')
3127 f += tv2->vval.v_float;
3128 else
3129 f -= tv2->vval.v_float;
3130 clear_tv(tv1);
3131 tv1->v_type = VAR_FLOAT;
3132 tv1->vval.v_float = f;
3133 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003134 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003135#endif
3136 {
3137 if (*op == '+')
3138 n += get_tv_number(tv2);
3139 else
3140 n -= get_tv_number(tv2);
3141 clear_tv(tv1);
3142 tv1->v_type = VAR_NUMBER;
3143 tv1->vval.v_number = n;
3144 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003145 }
3146 else
3147 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003148 if (tv2->v_type == VAR_FLOAT)
3149 break;
3150
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003151 /* str .= str */
3152 s = get_tv_string(tv1);
3153 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3154 clear_tv(tv1);
3155 tv1->v_type = VAR_STRING;
3156 tv1->vval.v_string = s;
3157 }
3158 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003159
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003160 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003161#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003162 {
3163 float_T f;
3164
3165 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3166 && tv2->v_type != VAR_NUMBER
3167 && tv2->v_type != VAR_STRING))
3168 break;
3169 if (tv2->v_type == VAR_FLOAT)
3170 f = tv2->vval.v_float;
3171 else
3172 f = get_tv_number(tv2);
3173 if (*op == '+')
3174 tv1->vval.v_float += f;
3175 else
3176 tv1->vval.v_float -= f;
3177 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003178#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003179 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003180 }
3181 }
3182
3183 EMSG2(_(e_letwrong), op);
3184 return FAIL;
3185}
3186
3187/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188 * Add a watcher to a list.
3189 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003190 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003191list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003192{
3193 lw->lw_next = l->lv_watch;
3194 l->lv_watch = lw;
3195}
3196
3197/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003198 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199 * No warning when it isn't found...
3200 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003201 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003202list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003203{
Bram Moolenaar33570922005-01-25 22:26:29 +00003204 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003205
3206 lwp = &l->lv_watch;
3207 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3208 {
3209 if (lw == lwrem)
3210 {
3211 *lwp = lw->lw_next;
3212 break;
3213 }
3214 lwp = &lw->lw_next;
3215 }
3216}
3217
3218/*
3219 * Just before removing an item from a list: advance watchers to the next
3220 * item.
3221 */
3222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003223list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003224{
Bram Moolenaar33570922005-01-25 22:26:29 +00003225 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003226
3227 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3228 if (lw->lw_item == item)
3229 lw->lw_item = item->li_next;
3230}
3231
3232/*
3233 * Evaluate the expression used in a ":for var in expr" command.
3234 * "arg" points to "var".
3235 * Set "*errp" to TRUE for an error, FALSE otherwise;
3236 * Return a pointer that holds the info. Null when there is an error.
3237 */
3238 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003239eval_for_line(
3240 char_u *arg,
3241 int *errp,
3242 char_u **nextcmdp,
3243 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244{
Bram Moolenaar33570922005-01-25 22:26:29 +00003245 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003247 typval_T tv;
3248 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003249
3250 *errp = TRUE; /* default: there is an error */
3251
Bram Moolenaar33570922005-01-25 22:26:29 +00003252 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253 if (fi == NULL)
3254 return NULL;
3255
3256 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3257 if (expr == NULL)
3258 return fi;
3259
3260 expr = skipwhite(expr);
3261 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3262 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003263 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003264 return fi;
3265 }
3266
3267 if (skip)
3268 ++emsg_skip;
3269 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3270 {
3271 *errp = FALSE;
3272 if (!skip)
3273 {
3274 l = tv.vval.v_list;
3275 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003276 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003277 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003278 clear_tv(&tv);
3279 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280 else
3281 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003282 /* No need to increment the refcount, it's already set for the
3283 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003284 fi->fi_list = l;
3285 list_add_watch(l, &fi->fi_lw);
3286 fi->fi_lw.lw_item = l->lv_first;
3287 }
3288 }
3289 }
3290 if (skip)
3291 --emsg_skip;
3292
3293 return fi;
3294}
3295
3296/*
3297 * Use the first item in a ":for" list. Advance to the next.
3298 * Assign the values to the variable (list). "arg" points to the first one.
3299 * Return TRUE when a valid item was found, FALSE when at end of list or
3300 * something wrong.
3301 */
3302 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003303next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003304{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003305 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003306 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003307 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003308
3309 item = fi->fi_lw.lw_item;
3310 if (item == NULL)
3311 result = FALSE;
3312 else
3313 {
3314 fi->fi_lw.lw_item = item->li_next;
3315 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3316 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3317 }
3318 return result;
3319}
3320
3321/*
3322 * Free the structure used to store info used by ":for".
3323 */
3324 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003325free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003326{
Bram Moolenaar33570922005-01-25 22:26:29 +00003327 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003328
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003329 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003330 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003331 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003332 list_unref(fi->fi_list);
3333 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003334 vim_free(fi);
3335}
3336
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3338
3339 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003340set_context_for_expression(
3341 expand_T *xp,
3342 char_u *arg,
3343 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344{
3345 int got_eq = FALSE;
3346 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003347 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003349 if (cmdidx == CMD_let)
3350 {
3351 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003352 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003353 {
3354 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003355 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003356 {
3357 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003358 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003359 if (vim_iswhite(*p))
3360 break;
3361 }
3362 return;
3363 }
3364 }
3365 else
3366 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3367 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 while ((xp->xp_pattern = vim_strpbrk(arg,
3369 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3370 {
3371 c = *xp->xp_pattern;
3372 if (c == '&')
3373 {
3374 c = xp->xp_pattern[1];
3375 if (c == '&')
3376 {
3377 ++xp->xp_pattern;
3378 xp->xp_context = cmdidx != CMD_let || got_eq
3379 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3380 }
3381 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003382 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003384 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3385 xp->xp_pattern += 2;
3386
3387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 }
3389 else if (c == '$')
3390 {
3391 /* environment variable */
3392 xp->xp_context = EXPAND_ENV_VARS;
3393 }
3394 else if (c == '=')
3395 {
3396 got_eq = TRUE;
3397 xp->xp_context = EXPAND_EXPRESSION;
3398 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003399 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 && xp->xp_context == EXPAND_FUNCTIONS
3401 && vim_strchr(xp->xp_pattern, '(') == NULL)
3402 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003403 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 break;
3405 }
3406 else if (cmdidx != CMD_let || got_eq)
3407 {
3408 if (c == '"') /* string */
3409 {
3410 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3411 if (c == '\\' && xp->xp_pattern[1] != NUL)
3412 ++xp->xp_pattern;
3413 xp->xp_context = EXPAND_NOTHING;
3414 }
3415 else if (c == '\'') /* literal string */
3416 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003417 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3419 /* skip */ ;
3420 xp->xp_context = EXPAND_NOTHING;
3421 }
3422 else if (c == '|')
3423 {
3424 if (xp->xp_pattern[1] == '|')
3425 {
3426 ++xp->xp_pattern;
3427 xp->xp_context = EXPAND_EXPRESSION;
3428 }
3429 else
3430 xp->xp_context = EXPAND_COMMANDS;
3431 }
3432 else
3433 xp->xp_context = EXPAND_EXPRESSION;
3434 }
3435 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003436 /* Doesn't look like something valid, expand as an expression
3437 * anyway. */
3438 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 arg = xp->xp_pattern;
3440 if (*arg != NUL)
3441 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3442 /* skip */ ;
3443 }
3444 xp->xp_pattern = arg;
3445}
3446
3447#endif /* FEAT_CMDL_COMPL */
3448
3449/*
3450 * ":1,25call func(arg1, arg2)" function call.
3451 */
3452 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003453ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454{
3455 char_u *arg = eap->arg;
3456 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003458 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003460 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 linenr_T lnum;
3462 int doesrange;
3463 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003464 funcdict_T fudi;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003465 partial_T *partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003467 if (eap->skip)
3468 {
3469 /* trans_function_name() doesn't work well when skipping, use eval0()
3470 * instead to skip to any following command, e.g. for:
3471 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003472 ++emsg_skip;
3473 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3474 clear_tv(&rettv);
3475 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003476 return;
3477 }
3478
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003479 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003480 if (fudi.fd_newkey != NULL)
3481 {
3482 /* Still need to give an error message for missing key. */
3483 EMSG2(_(e_dictkey), fudi.fd_newkey);
3484 vim_free(fudi.fd_newkey);
3485 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003486 if (tofree == NULL)
3487 return;
3488
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003489 /* Increase refcount on dictionary, it could get deleted when evaluating
3490 * the arguments. */
3491 if (fudi.fd_dict != NULL)
3492 ++fudi.fd_dict->dv_refcount;
3493
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003494 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003495 len = (int)STRLEN(tofree);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003496 name = deref_func_name(tofree, &len, &partial, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497
Bram Moolenaar532c7802005-01-27 14:44:31 +00003498 /* Skip white space to allow ":call func ()". Not good, but required for
3499 * backward compatibility. */
3500 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003501 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502
3503 if (*startarg != '(')
3504 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003505 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 goto end;
3507 }
3508
3509 /*
3510 * When skipping, evaluate the function once, to find the end of the
3511 * arguments.
3512 * When the function takes a range, this is discovered after the first
3513 * call, and the loop is broken.
3514 */
3515 if (eap->skip)
3516 {
3517 ++emsg_skip;
3518 lnum = eap->line2; /* do it once, also with an invalid range */
3519 }
3520 else
3521 lnum = eap->line1;
3522 for ( ; lnum <= eap->line2; ++lnum)
3523 {
3524 if (!eap->skip && eap->addr_count > 0)
3525 {
3526 curwin->w_cursor.lnum = lnum;
3527 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003528#ifdef FEAT_VIRTUALEDIT
3529 curwin->w_cursor.coladd = 0;
3530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 }
3532 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003533 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003534 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003535 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 {
3537 failed = TRUE;
3538 break;
3539 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003540
3541 /* Handle a function returning a Funcref, Dictionary or List. */
3542 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3543 {
3544 failed = TRUE;
3545 break;
3546 }
3547
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003548 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 if (doesrange || eap->skip)
3550 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003551
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003553 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003554 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003555 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 if (aborting())
3557 break;
3558 }
3559 if (eap->skip)
3560 --emsg_skip;
3561
3562 if (!failed)
3563 {
3564 /* Check for trailing illegal characters and a following command. */
3565 if (!ends_excmd(*arg))
3566 {
3567 emsg_severe = TRUE;
3568 EMSG(_(e_trailing));
3569 }
3570 else
3571 eap->nextcmd = check_nextcmd(arg);
3572 }
3573
3574end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003575 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003576 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577}
3578
3579/*
3580 * ":unlet[!] var1 ... " command.
3581 */
3582 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003583ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003585 ex_unletlock(eap, eap->arg, 0);
3586}
3587
3588/*
3589 * ":lockvar" and ":unlockvar" commands
3590 */
3591 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003592ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003593{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003595 int deep = 2;
3596
3597 if (eap->forceit)
3598 deep = -1;
3599 else if (vim_isdigit(*arg))
3600 {
3601 deep = getdigits(&arg);
3602 arg = skipwhite(arg);
3603 }
3604
3605 ex_unletlock(eap, arg, deep);
3606}
3607
3608/*
3609 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3610 */
3611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003612ex_unletlock(
3613 exarg_T *eap,
3614 char_u *argstart,
3615 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003616{
3617 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003620 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621
3622 do
3623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003624 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003625 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003626 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003627 if (lv.ll_name == NULL)
3628 error = TRUE; /* error but continue parsing */
3629 if (name_end == NULL || (!vim_iswhite(*name_end)
3630 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003632 if (name_end != NULL)
3633 {
3634 emsg_severe = TRUE;
3635 EMSG(_(e_trailing));
3636 }
3637 if (!(eap->skip || error))
3638 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 break;
3640 }
3641
3642 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003643 {
3644 if (eap->cmdidx == CMD_unlet)
3645 {
3646 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3647 error = TRUE;
3648 }
3649 else
3650 {
3651 if (do_lock_var(&lv, name_end, deep,
3652 eap->cmdidx == CMD_lockvar) == FAIL)
3653 error = TRUE;
3654 }
3655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003657 if (!eap->skip)
3658 clear_lval(&lv);
3659
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 arg = skipwhite(name_end);
3661 } while (!ends_excmd(*arg));
3662
3663 eap->nextcmd = check_nextcmd(arg);
3664}
3665
Bram Moolenaar8c711452005-01-14 21:53:12 +00003666 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003667do_unlet_var(
3668 lval_T *lp,
3669 char_u *name_end,
3670 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003671{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672 int ret = OK;
3673 int cc;
3674
3675 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003676 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003677 cc = *name_end;
3678 *name_end = NUL;
3679
3680 /* Normal name or expanded name. */
3681 if (check_changedtick(lp->ll_name))
3682 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003683 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003684 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003685 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003686 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003687 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003688 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003689 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003690 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003691 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003692 else if (lp->ll_range)
3693 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003694 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003695 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003696 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003697
3698 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3699 {
3700 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003701 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003702 return FAIL;
3703 ll_li = li;
3704 ++ll_n1;
3705 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003706
3707 /* Delete a range of List items. */
3708 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3709 {
3710 li = lp->ll_li->li_next;
3711 listitem_remove(lp->ll_list, lp->ll_li);
3712 lp->ll_li = li;
3713 ++lp->ll_n1;
3714 }
3715 }
3716 else
3717 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003718 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003719 /* unlet a List item. */
3720 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003721 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003722 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003723 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003724 }
3725
3726 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003727}
3728
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729/*
3730 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003731 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 */
3733 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003734do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735{
Bram Moolenaar33570922005-01-25 22:26:29 +00003736 hashtab_T *ht;
3737 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003738 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003739 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003740 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741
Bram Moolenaar33570922005-01-25 22:26:29 +00003742 ht = find_var_ht(name, &varname);
3743 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003745 if (ht == &globvarht)
3746 d = &globvardict;
3747 else if (current_funccal != NULL
3748 && ht == &current_funccal->l_vars.dv_hashtab)
3749 d = &current_funccal->l_vars;
3750 else if (ht == &compat_hashtab)
3751 d = &vimvardict;
3752 else
3753 {
3754 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3755 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3756 }
3757 if (d == NULL)
3758 {
3759 EMSG2(_(e_intern2), "do_unlet()");
3760 return FAIL;
3761 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003762 hi = hash_find(ht, varname);
3763 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003764 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003765 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003766 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003767 || var_check_ro(di->di_flags, name, FALSE)
3768 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003769 return FAIL;
3770
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003771 delete_var(ht, hi);
3772 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003775 if (forceit)
3776 return OK;
3777 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 return FAIL;
3779}
3780
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003781/*
3782 * Lock or unlock variable indicated by "lp".
3783 * "deep" is the levels to go (-1 for unlimited);
3784 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3785 */
3786 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003787do_lock_var(
3788 lval_T *lp,
3789 char_u *name_end,
3790 int deep,
3791 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003792{
3793 int ret = OK;
3794 int cc;
3795 dictitem_T *di;
3796
3797 if (deep == 0) /* nothing to do */
3798 return OK;
3799
3800 if (lp->ll_tv == NULL)
3801 {
3802 cc = *name_end;
3803 *name_end = NUL;
3804
3805 /* Normal name or expanded name. */
3806 if (check_changedtick(lp->ll_name))
3807 ret = FAIL;
3808 else
3809 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003810 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003811 if (di == NULL)
3812 ret = FAIL;
3813 else
3814 {
3815 if (lock)
3816 di->di_flags |= DI_FLAGS_LOCK;
3817 else
3818 di->di_flags &= ~DI_FLAGS_LOCK;
3819 item_lock(&di->di_tv, deep, lock);
3820 }
3821 }
3822 *name_end = cc;
3823 }
3824 else if (lp->ll_range)
3825 {
3826 listitem_T *li = lp->ll_li;
3827
3828 /* (un)lock a range of List items. */
3829 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3830 {
3831 item_lock(&li->li_tv, deep, lock);
3832 li = li->li_next;
3833 ++lp->ll_n1;
3834 }
3835 }
3836 else if (lp->ll_list != NULL)
3837 /* (un)lock a List item. */
3838 item_lock(&lp->ll_li->li_tv, deep, lock);
3839 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003840 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003841 item_lock(&lp->ll_di->di_tv, deep, lock);
3842
3843 return ret;
3844}
3845
3846/*
3847 * Lock or unlock an item. "deep" is nr of levels to go.
3848 */
3849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003850item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003851{
3852 static int recurse = 0;
3853 list_T *l;
3854 listitem_T *li;
3855 dict_T *d;
3856 hashitem_T *hi;
3857 int todo;
3858
3859 if (recurse >= DICT_MAXNEST)
3860 {
3861 EMSG(_("E743: variable nested too deep for (un)lock"));
3862 return;
3863 }
3864 if (deep == 0)
3865 return;
3866 ++recurse;
3867
3868 /* lock/unlock the item itself */
3869 if (lock)
3870 tv->v_lock |= VAR_LOCKED;
3871 else
3872 tv->v_lock &= ~VAR_LOCKED;
3873
3874 switch (tv->v_type)
3875 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003876 case VAR_UNKNOWN:
3877 case VAR_NUMBER:
3878 case VAR_STRING:
3879 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003880 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003881 case VAR_FLOAT:
3882 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003883 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003884 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003885 break;
3886
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003887 case VAR_LIST:
3888 if ((l = tv->vval.v_list) != NULL)
3889 {
3890 if (lock)
3891 l->lv_lock |= VAR_LOCKED;
3892 else
3893 l->lv_lock &= ~VAR_LOCKED;
3894 if (deep < 0 || deep > 1)
3895 /* recursive: lock/unlock the items the List contains */
3896 for (li = l->lv_first; li != NULL; li = li->li_next)
3897 item_lock(&li->li_tv, deep - 1, lock);
3898 }
3899 break;
3900 case VAR_DICT:
3901 if ((d = tv->vval.v_dict) != NULL)
3902 {
3903 if (lock)
3904 d->dv_lock |= VAR_LOCKED;
3905 else
3906 d->dv_lock &= ~VAR_LOCKED;
3907 if (deep < 0 || deep > 1)
3908 {
3909 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003910 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003911 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3912 {
3913 if (!HASHITEM_EMPTY(hi))
3914 {
3915 --todo;
3916 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3917 }
3918 }
3919 }
3920 }
3921 }
3922 --recurse;
3923}
3924
Bram Moolenaara40058a2005-07-11 22:42:07 +00003925/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003926 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3927 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003928 */
3929 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003930tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003931{
3932 return (tv->v_lock & VAR_LOCKED)
3933 || (tv->v_type == VAR_LIST
3934 && tv->vval.v_list != NULL
3935 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3936 || (tv->v_type == VAR_DICT
3937 && tv->vval.v_dict != NULL
3938 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3939}
3940
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3942/*
3943 * Delete all "menutrans_" variables.
3944 */
3945 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003946del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947{
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003949 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950
Bram Moolenaar33570922005-01-25 22:26:29 +00003951 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003952 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003953 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003954 {
3955 if (!HASHITEM_EMPTY(hi))
3956 {
3957 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003958 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3959 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003960 }
3961 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963}
3964#endif
3965
3966#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3967
3968/*
3969 * Local string buffer for the next two functions to store a variable name
3970 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3971 * get_user_var_name().
3972 */
3973
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003974static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975
3976static char_u *varnamebuf = NULL;
3977static int varnamebuflen = 0;
3978
3979/*
3980 * Function to concatenate a prefix and a variable name.
3981 */
3982 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003983cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984{
3985 int len;
3986
3987 len = (int)STRLEN(name) + 3;
3988 if (len > varnamebuflen)
3989 {
3990 vim_free(varnamebuf);
3991 len += 10; /* some additional space */
3992 varnamebuf = alloc(len);
3993 if (varnamebuf == NULL)
3994 {
3995 varnamebuflen = 0;
3996 return NULL;
3997 }
3998 varnamebuflen = len;
3999 }
4000 *varnamebuf = prefix;
4001 varnamebuf[1] = ':';
4002 STRCPY(varnamebuf + 2, name);
4003 return varnamebuf;
4004}
4005
4006/*
4007 * Function given to ExpandGeneric() to obtain the list of user defined
4008 * (global/buffer/window/built-in) variable names.
4009 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004011get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004013 static long_u gdone;
4014 static long_u bdone;
4015 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004016#ifdef FEAT_WINDOWS
4017 static long_u tdone;
4018#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004019 static int vidx;
4020 static hashitem_T *hi;
4021 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022
4023 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004024 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004025 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004026#ifdef FEAT_WINDOWS
4027 tdone = 0;
4028#endif
4029 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004030
4031 /* Global variables */
4032 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004034 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004035 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004036 else
4037 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004038 while (HASHITEM_EMPTY(hi))
4039 ++hi;
4040 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4041 return cat_prefix_varname('g', hi->hi_key);
4042 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004044
4045 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004046 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004047 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004049 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004050 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004051 else
4052 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004053 while (HASHITEM_EMPTY(hi))
4054 ++hi;
4055 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004057 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004059 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 return (char_u *)"b:changedtick";
4061 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004062
4063 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004064 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004065 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004067 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004068 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004069 else
4070 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004071 while (HASHITEM_EMPTY(hi))
4072 ++hi;
4073 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004075
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004076#ifdef FEAT_WINDOWS
4077 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004078 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004079 if (tdone < ht->ht_used)
4080 {
4081 if (tdone++ == 0)
4082 hi = ht->ht_array;
4083 else
4084 ++hi;
4085 while (HASHITEM_EMPTY(hi))
4086 ++hi;
4087 return cat_prefix_varname('t', hi->hi_key);
4088 }
4089#endif
4090
Bram Moolenaar33570922005-01-25 22:26:29 +00004091 /* v: variables */
4092 if (vidx < VV_LEN)
4093 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094
4095 vim_free(varnamebuf);
4096 varnamebuf = NULL;
4097 varnamebuflen = 0;
4098 return NULL;
4099}
4100
4101#endif /* FEAT_CMDL_COMPL */
4102
4103/*
4104 * types for expressions.
4105 */
4106typedef enum
4107{
4108 TYPE_UNKNOWN = 0
4109 , TYPE_EQUAL /* == */
4110 , TYPE_NEQUAL /* != */
4111 , TYPE_GREATER /* > */
4112 , TYPE_GEQUAL /* >= */
4113 , TYPE_SMALLER /* < */
4114 , TYPE_SEQUAL /* <= */
4115 , TYPE_MATCH /* =~ */
4116 , TYPE_NOMATCH /* !~ */
4117} exptype_T;
4118
4119/*
4120 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4123 */
4124
4125/*
4126 * Handle zero level expression.
4127 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004128 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004129 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 * Return OK or FAIL.
4131 */
4132 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004133eval0(
4134 char_u *arg,
4135 typval_T *rettv,
4136 char_u **nextcmd,
4137 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138{
4139 int ret;
4140 char_u *p;
4141
4142 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 if (ret == FAIL || !ends_excmd(*p))
4145 {
4146 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004147 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 /*
4149 * Report the invalid expression unless the expression evaluation has
4150 * been cancelled due to an aborting error, an interrupt, or an
4151 * exception.
4152 */
4153 if (!aborting())
4154 EMSG2(_(e_invexpr2), arg);
4155 ret = FAIL;
4156 }
4157 if (nextcmd != NULL)
4158 *nextcmd = check_nextcmd(p);
4159
4160 return ret;
4161}
4162
4163/*
4164 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004165 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 *
4167 * "arg" must point to the first non-white of the expression.
4168 * "arg" is advanced to the next non-white after the recognized expression.
4169 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004170 * Note: "rettv.v_lock" is not set.
4171 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 * Return OK or FAIL.
4173 */
4174 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004175eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176{
4177 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004178 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179
4180 /*
4181 * Get the first variable.
4182 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004183 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 return FAIL;
4185
4186 if ((*arg)[0] == '?')
4187 {
4188 result = FALSE;
4189 if (evaluate)
4190 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004191 int error = FALSE;
4192
4193 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004195 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004196 if (error)
4197 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 }
4199
4200 /*
4201 * Get the second variable.
4202 */
4203 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004204 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 return FAIL;
4206
4207 /*
4208 * Check for the ":".
4209 */
4210 if ((*arg)[0] != ':')
4211 {
4212 EMSG(_("E109: Missing ':' after '?'"));
4213 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004214 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 return FAIL;
4216 }
4217
4218 /*
4219 * Get the third variable.
4220 */
4221 *arg = skipwhite(*arg + 1);
4222 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4223 {
4224 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004225 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 return FAIL;
4227 }
4228 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004229 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 }
4231
4232 return OK;
4233}
4234
4235/*
4236 * Handle first level expression:
4237 * expr2 || expr2 || expr2 logical OR
4238 *
4239 * "arg" must point to the first non-white of the expression.
4240 * "arg" is advanced to the next non-white after the recognized expression.
4241 *
4242 * Return OK or FAIL.
4243 */
4244 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004245eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246{
Bram Moolenaar33570922005-01-25 22:26:29 +00004247 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 long result;
4249 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004250 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
4252 /*
4253 * Get the first variable.
4254 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004255 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 return FAIL;
4257
4258 /*
4259 * Repeat until there is no following "||".
4260 */
4261 first = TRUE;
4262 result = FALSE;
4263 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4264 {
4265 if (evaluate && first)
4266 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004267 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004269 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004270 if (error)
4271 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 first = FALSE;
4273 }
4274
4275 /*
4276 * Get the second variable.
4277 */
4278 *arg = skipwhite(*arg + 2);
4279 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4280 return FAIL;
4281
4282 /*
4283 * Compute the result.
4284 */
4285 if (evaluate && !result)
4286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004287 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004289 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004290 if (error)
4291 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 }
4293 if (evaluate)
4294 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004295 rettv->v_type = VAR_NUMBER;
4296 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 }
4298 }
4299
4300 return OK;
4301}
4302
4303/*
4304 * Handle second level expression:
4305 * expr3 && expr3 && expr3 logical AND
4306 *
4307 * "arg" must point to the first non-white of the expression.
4308 * "arg" is advanced to the next non-white after the recognized expression.
4309 *
4310 * Return OK or FAIL.
4311 */
4312 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004313eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314{
Bram Moolenaar33570922005-01-25 22:26:29 +00004315 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 long result;
4317 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004318 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319
4320 /*
4321 * Get the first variable.
4322 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004323 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 return FAIL;
4325
4326 /*
4327 * Repeat until there is no following "&&".
4328 */
4329 first = TRUE;
4330 result = TRUE;
4331 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4332 {
4333 if (evaluate && first)
4334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004335 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004337 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004338 if (error)
4339 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 first = FALSE;
4341 }
4342
4343 /*
4344 * Get the second variable.
4345 */
4346 *arg = skipwhite(*arg + 2);
4347 if (eval4(arg, &var2, evaluate && result) == FAIL)
4348 return FAIL;
4349
4350 /*
4351 * Compute the result.
4352 */
4353 if (evaluate && result)
4354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004355 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004357 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004358 if (error)
4359 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
4361 if (evaluate)
4362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004363 rettv->v_type = VAR_NUMBER;
4364 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366 }
4367
4368 return OK;
4369}
4370
4371/*
4372 * Handle third level expression:
4373 * var1 == var2
4374 * var1 =~ var2
4375 * var1 != var2
4376 * var1 !~ var2
4377 * var1 > var2
4378 * var1 >= var2
4379 * var1 < var2
4380 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004381 * var1 is var2
4382 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 *
4384 * "arg" must point to the first non-white of the expression.
4385 * "arg" is advanced to the next non-white after the recognized expression.
4386 *
4387 * Return OK or FAIL.
4388 */
4389 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004390eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391{
Bram Moolenaar33570922005-01-25 22:26:29 +00004392 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 char_u *p;
4394 int i;
4395 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004396 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 int len = 2;
4398 long n1, n2;
4399 char_u *s1, *s2;
4400 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4401 regmatch_T regmatch;
4402 int ic;
4403 char_u *save_cpo;
4404
4405 /*
4406 * Get the first variable.
4407 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004408 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 return FAIL;
4410
4411 p = *arg;
4412 switch (p[0])
4413 {
4414 case '=': if (p[1] == '=')
4415 type = TYPE_EQUAL;
4416 else if (p[1] == '~')
4417 type = TYPE_MATCH;
4418 break;
4419 case '!': if (p[1] == '=')
4420 type = TYPE_NEQUAL;
4421 else if (p[1] == '~')
4422 type = TYPE_NOMATCH;
4423 break;
4424 case '>': if (p[1] != '=')
4425 {
4426 type = TYPE_GREATER;
4427 len = 1;
4428 }
4429 else
4430 type = TYPE_GEQUAL;
4431 break;
4432 case '<': if (p[1] != '=')
4433 {
4434 type = TYPE_SMALLER;
4435 len = 1;
4436 }
4437 else
4438 type = TYPE_SEQUAL;
4439 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004440 case 'i': if (p[1] == 's')
4441 {
4442 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4443 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004444 i = p[len];
4445 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004446 {
4447 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4448 type_is = TRUE;
4449 }
4450 }
4451 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 }
4453
4454 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004455 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 */
4457 if (type != TYPE_UNKNOWN)
4458 {
4459 /* extra question mark appended: ignore case */
4460 if (p[len] == '?')
4461 {
4462 ic = TRUE;
4463 ++len;
4464 }
4465 /* extra '#' appended: match case */
4466 else if (p[len] == '#')
4467 {
4468 ic = FALSE;
4469 ++len;
4470 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004471 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 else
4473 ic = p_ic;
4474
4475 /*
4476 * Get the second variable.
4477 */
4478 *arg = skipwhite(p + len);
4479 if (eval5(arg, &var2, evaluate) == FAIL)
4480 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004481 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 return FAIL;
4483 }
4484
4485 if (evaluate)
4486 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004487 if (type_is && rettv->v_type != var2.v_type)
4488 {
4489 /* For "is" a different type always means FALSE, for "notis"
4490 * it means TRUE. */
4491 n1 = (type == TYPE_NEQUAL);
4492 }
4493 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4494 {
4495 if (type_is)
4496 {
4497 n1 = (rettv->v_type == var2.v_type
4498 && rettv->vval.v_list == var2.vval.v_list);
4499 if (type == TYPE_NEQUAL)
4500 n1 = !n1;
4501 }
4502 else if (rettv->v_type != var2.v_type
4503 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4504 {
4505 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004506 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004507 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004508 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004509 clear_tv(rettv);
4510 clear_tv(&var2);
4511 return FAIL;
4512 }
4513 else
4514 {
4515 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004516 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4517 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004518 if (type == TYPE_NEQUAL)
4519 n1 = !n1;
4520 }
4521 }
4522
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004523 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4524 {
4525 if (type_is)
4526 {
4527 n1 = (rettv->v_type == var2.v_type
4528 && rettv->vval.v_dict == var2.vval.v_dict);
4529 if (type == TYPE_NEQUAL)
4530 n1 = !n1;
4531 }
4532 else if (rettv->v_type != var2.v_type
4533 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4534 {
4535 if (rettv->v_type != var2.v_type)
4536 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4537 else
4538 EMSG(_("E736: Invalid operation for Dictionary"));
4539 clear_tv(rettv);
4540 clear_tv(&var2);
4541 return FAIL;
4542 }
4543 else
4544 {
4545 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004546 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4547 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004548 if (type == TYPE_NEQUAL)
4549 n1 = !n1;
4550 }
4551 }
4552
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004553 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4554 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004555 {
4556 if (rettv->v_type != var2.v_type
4557 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4558 {
4559 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004560 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004561 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004562 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004563 clear_tv(rettv);
4564 clear_tv(&var2);
4565 return FAIL;
4566 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004567 else if (rettv->v_type == VAR_PARTIAL)
4568 {
4569 /* Partials are only equal when identical. */
4570 n1 = rettv->vval.v_partial != NULL
4571 && rettv->vval.v_partial == var2.vval.v_partial;
4572 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004573 else
4574 {
4575 /* Compare two Funcrefs for being equal or unequal. */
4576 if (rettv->vval.v_string == NULL
4577 || var2.vval.v_string == NULL)
4578 n1 = FALSE;
4579 else
4580 n1 = STRCMP(rettv->vval.v_string,
4581 var2.vval.v_string) == 0;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004582 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004583 if (type == TYPE_NEQUAL)
4584 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004585 }
4586
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004587#ifdef FEAT_FLOAT
4588 /*
4589 * If one of the two variables is a float, compare as a float.
4590 * When using "=~" or "!~", always compare as string.
4591 */
4592 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4593 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4594 {
4595 float_T f1, f2;
4596
4597 if (rettv->v_type == VAR_FLOAT)
4598 f1 = rettv->vval.v_float;
4599 else
4600 f1 = get_tv_number(rettv);
4601 if (var2.v_type == VAR_FLOAT)
4602 f2 = var2.vval.v_float;
4603 else
4604 f2 = get_tv_number(&var2);
4605 n1 = FALSE;
4606 switch (type)
4607 {
4608 case TYPE_EQUAL: n1 = (f1 == f2); break;
4609 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4610 case TYPE_GREATER: n1 = (f1 > f2); break;
4611 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4612 case TYPE_SMALLER: n1 = (f1 < f2); break;
4613 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4614 case TYPE_UNKNOWN:
4615 case TYPE_MATCH:
4616 case TYPE_NOMATCH: break; /* avoid gcc warning */
4617 }
4618 }
4619#endif
4620
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 /*
4622 * If one of the two variables is a number, compare as a number.
4623 * When using "=~" or "!~", always compare as string.
4624 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004625 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4627 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004628 n1 = get_tv_number(rettv);
4629 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 switch (type)
4631 {
4632 case TYPE_EQUAL: n1 = (n1 == n2); break;
4633 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4634 case TYPE_GREATER: n1 = (n1 > n2); break;
4635 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4636 case TYPE_SMALLER: n1 = (n1 < n2); break;
4637 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4638 case TYPE_UNKNOWN:
4639 case TYPE_MATCH:
4640 case TYPE_NOMATCH: break; /* avoid gcc warning */
4641 }
4642 }
4643 else
4644 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004645 s1 = get_tv_string_buf(rettv, buf1);
4646 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4648 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4649 else
4650 i = 0;
4651 n1 = FALSE;
4652 switch (type)
4653 {
4654 case TYPE_EQUAL: n1 = (i == 0); break;
4655 case TYPE_NEQUAL: n1 = (i != 0); break;
4656 case TYPE_GREATER: n1 = (i > 0); break;
4657 case TYPE_GEQUAL: n1 = (i >= 0); break;
4658 case TYPE_SMALLER: n1 = (i < 0); break;
4659 case TYPE_SEQUAL: n1 = (i <= 0); break;
4660
4661 case TYPE_MATCH:
4662 case TYPE_NOMATCH:
4663 /* avoid 'l' flag in 'cpoptions' */
4664 save_cpo = p_cpo;
4665 p_cpo = (char_u *)"";
4666 regmatch.regprog = vim_regcomp(s2,
4667 RE_MAGIC + RE_STRING);
4668 regmatch.rm_ic = ic;
4669 if (regmatch.regprog != NULL)
4670 {
4671 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004672 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 if (type == TYPE_NOMATCH)
4674 n1 = !n1;
4675 }
4676 p_cpo = save_cpo;
4677 break;
4678
4679 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4680 }
4681 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004682 clear_tv(rettv);
4683 clear_tv(&var2);
4684 rettv->v_type = VAR_NUMBER;
4685 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 }
4687 }
4688
4689 return OK;
4690}
4691
4692/*
4693 * Handle fourth level expression:
4694 * + number addition
4695 * - number subtraction
4696 * . string concatenation
4697 *
4698 * "arg" must point to the first non-white of the expression.
4699 * "arg" is advanced to the next non-white after the recognized expression.
4700 *
4701 * Return OK or FAIL.
4702 */
4703 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004704eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705{
Bram Moolenaar33570922005-01-25 22:26:29 +00004706 typval_T var2;
4707 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 int op;
4709 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004710#ifdef FEAT_FLOAT
4711 float_T f1 = 0, f2 = 0;
4712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 char_u *s1, *s2;
4714 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4715 char_u *p;
4716
4717 /*
4718 * Get the first variable.
4719 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004720 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 return FAIL;
4722
4723 /*
4724 * Repeat computing, until no '+', '-' or '.' is following.
4725 */
4726 for (;;)
4727 {
4728 op = **arg;
4729 if (op != '+' && op != '-' && op != '.')
4730 break;
4731
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004732 if ((op != '+' || rettv->v_type != VAR_LIST)
4733#ifdef FEAT_FLOAT
4734 && (op == '.' || rettv->v_type != VAR_FLOAT)
4735#endif
4736 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004737 {
4738 /* For "list + ...", an illegal use of the first operand as
4739 * a number cannot be determined before evaluating the 2nd
4740 * operand: if this is also a list, all is ok.
4741 * For "something . ...", "something - ..." or "non-list + ...",
4742 * we know that the first operand needs to be a string or number
4743 * without evaluating the 2nd operand. So check before to avoid
4744 * side effects after an error. */
4745 if (evaluate && get_tv_string_chk(rettv) == NULL)
4746 {
4747 clear_tv(rettv);
4748 return FAIL;
4749 }
4750 }
4751
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 /*
4753 * Get the second variable.
4754 */
4755 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004756 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004758 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 return FAIL;
4760 }
4761
4762 if (evaluate)
4763 {
4764 /*
4765 * Compute the result.
4766 */
4767 if (op == '.')
4768 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004769 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4770 s2 = get_tv_string_buf_chk(&var2, buf2);
4771 if (s2 == NULL) /* type error ? */
4772 {
4773 clear_tv(rettv);
4774 clear_tv(&var2);
4775 return FAIL;
4776 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004777 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004778 clear_tv(rettv);
4779 rettv->v_type = VAR_STRING;
4780 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004782 else if (op == '+' && rettv->v_type == VAR_LIST
4783 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004784 {
4785 /* concatenate Lists */
4786 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4787 &var3) == FAIL)
4788 {
4789 clear_tv(rettv);
4790 clear_tv(&var2);
4791 return FAIL;
4792 }
4793 clear_tv(rettv);
4794 *rettv = var3;
4795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 else
4797 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004798 int error = FALSE;
4799
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004800#ifdef FEAT_FLOAT
4801 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004802 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004803 f1 = rettv->vval.v_float;
4804 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004805 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004806 else
4807#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004808 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004809 n1 = get_tv_number_chk(rettv, &error);
4810 if (error)
4811 {
4812 /* This can only happen for "list + non-list". For
4813 * "non-list + ..." or "something - ...", we returned
4814 * before evaluating the 2nd operand. */
4815 clear_tv(rettv);
4816 return FAIL;
4817 }
4818#ifdef FEAT_FLOAT
4819 if (var2.v_type == VAR_FLOAT)
4820 f1 = n1;
4821#endif
4822 }
4823#ifdef FEAT_FLOAT
4824 if (var2.v_type == VAR_FLOAT)
4825 {
4826 f2 = var2.vval.v_float;
4827 n2 = 0;
4828 }
4829 else
4830#endif
4831 {
4832 n2 = get_tv_number_chk(&var2, &error);
4833 if (error)
4834 {
4835 clear_tv(rettv);
4836 clear_tv(&var2);
4837 return FAIL;
4838 }
4839#ifdef FEAT_FLOAT
4840 if (rettv->v_type == VAR_FLOAT)
4841 f2 = n2;
4842#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004843 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004844 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004845
4846#ifdef FEAT_FLOAT
4847 /* If there is a float on either side the result is a float. */
4848 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4849 {
4850 if (op == '+')
4851 f1 = f1 + f2;
4852 else
4853 f1 = f1 - f2;
4854 rettv->v_type = VAR_FLOAT;
4855 rettv->vval.v_float = f1;
4856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004858#endif
4859 {
4860 if (op == '+')
4861 n1 = n1 + n2;
4862 else
4863 n1 = n1 - n2;
4864 rettv->v_type = VAR_NUMBER;
4865 rettv->vval.v_number = n1;
4866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004868 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 }
4870 }
4871 return OK;
4872}
4873
4874/*
4875 * Handle fifth level expression:
4876 * * number multiplication
4877 * / number division
4878 * % number modulo
4879 *
4880 * "arg" must point to the first non-white of the expression.
4881 * "arg" is advanced to the next non-white after the recognized expression.
4882 *
4883 * Return OK or FAIL.
4884 */
4885 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004886eval6(
4887 char_u **arg,
4888 typval_T *rettv,
4889 int evaluate,
4890 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891{
Bram Moolenaar33570922005-01-25 22:26:29 +00004892 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 int op;
4894 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004895#ifdef FEAT_FLOAT
4896 int use_float = FALSE;
4897 float_T f1 = 0, f2;
4898#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004899 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900
4901 /*
4902 * Get the first variable.
4903 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004904 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 return FAIL;
4906
4907 /*
4908 * Repeat computing, until no '*', '/' or '%' is following.
4909 */
4910 for (;;)
4911 {
4912 op = **arg;
4913 if (op != '*' && op != '/' && op != '%')
4914 break;
4915
4916 if (evaluate)
4917 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004918#ifdef FEAT_FLOAT
4919 if (rettv->v_type == VAR_FLOAT)
4920 {
4921 f1 = rettv->vval.v_float;
4922 use_float = TRUE;
4923 n1 = 0;
4924 }
4925 else
4926#endif
4927 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004928 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004929 if (error)
4930 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 }
4932 else
4933 n1 = 0;
4934
4935 /*
4936 * Get the second variable.
4937 */
4938 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004939 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 return FAIL;
4941
4942 if (evaluate)
4943 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004944#ifdef FEAT_FLOAT
4945 if (var2.v_type == VAR_FLOAT)
4946 {
4947 if (!use_float)
4948 {
4949 f1 = n1;
4950 use_float = TRUE;
4951 }
4952 f2 = var2.vval.v_float;
4953 n2 = 0;
4954 }
4955 else
4956#endif
4957 {
4958 n2 = get_tv_number_chk(&var2, &error);
4959 clear_tv(&var2);
4960 if (error)
4961 return FAIL;
4962#ifdef FEAT_FLOAT
4963 if (use_float)
4964 f2 = n2;
4965#endif
4966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967
4968 /*
4969 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004970 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004972#ifdef FEAT_FLOAT
4973 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004975 if (op == '*')
4976 f1 = f1 * f2;
4977 else if (op == '/')
4978 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004979# ifdef VMS
4980 /* VMS crashes on divide by zero, work around it */
4981 if (f2 == 0.0)
4982 {
4983 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004984 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004985 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004986 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004987 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004988 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004989 }
4990 else
4991 f1 = f1 / f2;
4992# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004993 /* We rely on the floating point library to handle divide
4994 * by zero to result in "inf" and not a crash. */
4995 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004996# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004999 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005000 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005001 return FAIL;
5002 }
5003 rettv->v_type = VAR_FLOAT;
5004 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
5006 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005009 if (op == '*')
5010 n1 = n1 * n2;
5011 else if (op == '/')
5012 {
5013 if (n2 == 0) /* give an error message? */
5014 {
5015 if (n1 == 0)
5016 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5017 else if (n1 < 0)
5018 n1 = -0x7fffffffL;
5019 else
5020 n1 = 0x7fffffffL;
5021 }
5022 else
5023 n1 = n1 / n2;
5024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005026 {
5027 if (n2 == 0) /* give an error message? */
5028 n1 = 0;
5029 else
5030 n1 = n1 % n2;
5031 }
5032 rettv->v_type = VAR_NUMBER;
5033 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 }
5036 }
5037
5038 return OK;
5039}
5040
5041/*
5042 * Handle sixth level expression:
5043 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005044 * "string" string constant
5045 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 * &option-name option value
5047 * @r register contents
5048 * identifier variable value
5049 * function() function call
5050 * $VAR environment variable
5051 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005052 * [expr, expr] List
5053 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 *
5055 * Also handle:
5056 * ! in front logical NOT
5057 * - in front unary minus
5058 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005059 * trailing [] subscript in String or List
5060 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 *
5062 * "arg" must point to the first non-white of the expression.
5063 * "arg" is advanced to the next non-white after the recognized expression.
5064 *
5065 * Return OK or FAIL.
5066 */
5067 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005068eval7(
5069 char_u **arg,
5070 typval_T *rettv,
5071 int evaluate,
5072 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 long n;
5075 int len;
5076 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 char_u *start_leader, *end_leader;
5078 int ret = OK;
5079 char_u *alias;
5080
5081 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005082 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005083 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005085 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086
5087 /*
5088 * Skip '!' and '-' characters. They are handled later.
5089 */
5090 start_leader = *arg;
5091 while (**arg == '!' || **arg == '-' || **arg == '+')
5092 *arg = skipwhite(*arg + 1);
5093 end_leader = *arg;
5094
5095 switch (**arg)
5096 {
5097 /*
5098 * Number constant.
5099 */
5100 case '0':
5101 case '1':
5102 case '2':
5103 case '3':
5104 case '4':
5105 case '5':
5106 case '6':
5107 case '7':
5108 case '8':
5109 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005110 {
5111#ifdef FEAT_FLOAT
5112 char_u *p = skipdigits(*arg + 1);
5113 int get_float = FALSE;
5114
5115 /* We accept a float when the format matches
5116 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005117 * strict to avoid backwards compatibility problems.
5118 * Don't look for a float after the "." operator, so that
5119 * ":let vers = 1.2.3" doesn't fail. */
5120 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005122 get_float = TRUE;
5123 p = skipdigits(p + 2);
5124 if (*p == 'e' || *p == 'E')
5125 {
5126 ++p;
5127 if (*p == '-' || *p == '+')
5128 ++p;
5129 if (!vim_isdigit(*p))
5130 get_float = FALSE;
5131 else
5132 p = skipdigits(p + 1);
5133 }
5134 if (ASCII_ISALPHA(*p) || *p == '.')
5135 get_float = FALSE;
5136 }
5137 if (get_float)
5138 {
5139 float_T f;
5140
5141 *arg += string2float(*arg, &f);
5142 if (evaluate)
5143 {
5144 rettv->v_type = VAR_FLOAT;
5145 rettv->vval.v_float = f;
5146 }
5147 }
5148 else
5149#endif
5150 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005151 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005152 *arg += len;
5153 if (evaluate)
5154 {
5155 rettv->v_type = VAR_NUMBER;
5156 rettv->vval.v_number = n;
5157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 }
5159 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161
5162 /*
5163 * String constant: "string".
5164 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005165 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 break;
5167
5168 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005169 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005171 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005172 break;
5173
5174 /*
5175 * List: [expr, expr]
5176 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005177 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 break;
5179
5180 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005181 * Dictionary: {key: val, key: val}
5182 */
5183 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5184 break;
5185
5186 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005187 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005189 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 break;
5191
5192 /*
5193 * Environment variable: $VAR.
5194 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005195 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 break;
5197
5198 /*
5199 * Register contents: @r.
5200 */
5201 case '@': ++*arg;
5202 if (evaluate)
5203 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005204 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005205 rettv->vval.v_string = get_reg_contents(**arg,
5206 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 }
5208 if (**arg != NUL)
5209 ++*arg;
5210 break;
5211
5212 /*
5213 * nested expression: (expression).
5214 */
5215 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005216 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 if (**arg == ')')
5218 ++*arg;
5219 else if (ret == OK)
5220 {
5221 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005222 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 ret = FAIL;
5224 }
5225 break;
5226
Bram Moolenaar8c711452005-01-14 21:53:12 +00005227 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 break;
5229 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005230
5231 if (ret == NOTDONE)
5232 {
5233 /*
5234 * Must be a variable or function name.
5235 * Can also be a curly-braces kind of name: {expr}.
5236 */
5237 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005238 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239 if (alias != NULL)
5240 s = alias;
5241
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005242 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005243 ret = FAIL;
5244 else
5245 {
5246 if (**arg == '(') /* recursive! */
5247 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005248 partial_T *partial;
5249
Bram Moolenaar8c711452005-01-14 21:53:12 +00005250 /* If "s" is the name of a variable of type VAR_FUNC
5251 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005252 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005253
5254 /* Invoke the function. */
5255 ret = get_func_tv(s, len, rettv, arg,
5256 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005257 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005258
5259 /* If evaluate is FALSE rettv->v_type was not set in
5260 * get_func_tv, but it's needed in handle_subscript() to parse
5261 * what follows. So set it here. */
5262 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5263 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005264 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005265 rettv->v_type = VAR_FUNC;
5266 }
5267
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 /* Stop the expression evaluation when immediately
5269 * aborting on error, or when an interrupt occurred or
5270 * an exception was thrown but not caught. */
5271 if (aborting())
5272 {
5273 if (ret == OK)
5274 clear_tv(rettv);
5275 ret = FAIL;
5276 }
5277 }
5278 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005279 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005280 else
5281 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005282 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005283 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 }
5285
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 *arg = skipwhite(*arg);
5287
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005288 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5289 * expr(expr). */
5290 if (ret == OK)
5291 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292
5293 /*
5294 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5295 */
5296 if (ret == OK && evaluate && end_leader > start_leader)
5297 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005298 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005299 int val = 0;
5300#ifdef FEAT_FLOAT
5301 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005302
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005303 if (rettv->v_type == VAR_FLOAT)
5304 f = rettv->vval.v_float;
5305 else
5306#endif
5307 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005308 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005310 clear_tv(rettv);
5311 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005313 else
5314 {
5315 while (end_leader > start_leader)
5316 {
5317 --end_leader;
5318 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005319 {
5320#ifdef FEAT_FLOAT
5321 if (rettv->v_type == VAR_FLOAT)
5322 f = !f;
5323 else
5324#endif
5325 val = !val;
5326 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005327 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005328 {
5329#ifdef FEAT_FLOAT
5330 if (rettv->v_type == VAR_FLOAT)
5331 f = -f;
5332 else
5333#endif
5334 val = -val;
5335 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005336 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005337#ifdef FEAT_FLOAT
5338 if (rettv->v_type == VAR_FLOAT)
5339 {
5340 clear_tv(rettv);
5341 rettv->vval.v_float = f;
5342 }
5343 else
5344#endif
5345 {
5346 clear_tv(rettv);
5347 rettv->v_type = VAR_NUMBER;
5348 rettv->vval.v_number = val;
5349 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 }
5352
5353 return ret;
5354}
5355
5356/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005357 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5358 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5360 */
5361 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005362eval_index(
5363 char_u **arg,
5364 typval_T *rettv,
5365 int evaluate,
5366 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367{
5368 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005369 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005371 long len = -1;
5372 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005374 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375
Bram Moolenaara03f2332016-02-06 18:09:59 +01005376 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005378 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005379 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005380 if (verbose)
5381 EMSG(_("E695: Cannot index a Funcref"));
5382 return FAIL;
5383 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005384#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005385 if (verbose)
5386 EMSG(_(e_float_as_string));
5387 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005388#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005389 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005390 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005391 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005392 if (verbose)
5393 EMSG(_("E909: Cannot index a special variable"));
5394 return FAIL;
5395 case VAR_UNKNOWN:
5396 if (evaluate)
5397 return FAIL;
5398 /* FALLTHROUGH */
5399
5400 case VAR_STRING:
5401 case VAR_NUMBER:
5402 case VAR_LIST:
5403 case VAR_DICT:
5404 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005405 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005407 init_tv(&var1);
5408 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005409 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005410 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005411 /*
5412 * dict.name
5413 */
5414 key = *arg + 1;
5415 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5416 ;
5417 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005418 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005419 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005420 }
5421 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005422 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005423 /*
5424 * something[idx]
5425 *
5426 * Get the (first) variable from inside the [].
5427 */
5428 *arg = skipwhite(*arg + 1);
5429 if (**arg == ':')
5430 empty1 = TRUE;
5431 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5432 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005433 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5434 {
5435 /* not a number or string */
5436 clear_tv(&var1);
5437 return FAIL;
5438 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005439
5440 /*
5441 * Get the second variable from inside the [:].
5442 */
5443 if (**arg == ':')
5444 {
5445 range = TRUE;
5446 *arg = skipwhite(*arg + 1);
5447 if (**arg == ']')
5448 empty2 = TRUE;
5449 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5450 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005451 if (!empty1)
5452 clear_tv(&var1);
5453 return FAIL;
5454 }
5455 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5456 {
5457 /* not a number or string */
5458 if (!empty1)
5459 clear_tv(&var1);
5460 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005461 return FAIL;
5462 }
5463 }
5464
5465 /* Check for the ']'. */
5466 if (**arg != ']')
5467 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005468 if (verbose)
5469 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005470 clear_tv(&var1);
5471 if (range)
5472 clear_tv(&var2);
5473 return FAIL;
5474 }
5475 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 }
5477
5478 if (evaluate)
5479 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005480 n1 = 0;
5481 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005483 n1 = get_tv_number(&var1);
5484 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005485 }
5486 if (range)
5487 {
5488 if (empty2)
5489 n2 = -1;
5490 else
5491 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005492 n2 = get_tv_number(&var2);
5493 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494 }
5495 }
5496
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005497 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005498 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005499 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005500 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005501 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005502 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005503 case VAR_SPECIAL:
5504 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005505 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005506 break; /* not evaluating, skipping over subscript */
5507
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005508 case VAR_NUMBER:
5509 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005510 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005511 len = (long)STRLEN(s);
5512 if (range)
5513 {
5514 /* The resulting variable is a substring. If the indexes
5515 * are out of range the result is empty. */
5516 if (n1 < 0)
5517 {
5518 n1 = len + n1;
5519 if (n1 < 0)
5520 n1 = 0;
5521 }
5522 if (n2 < 0)
5523 n2 = len + n2;
5524 else if (n2 >= len)
5525 n2 = len;
5526 if (n1 >= len || n2 < 0 || n1 > n2)
5527 s = NULL;
5528 else
5529 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5530 }
5531 else
5532 {
5533 /* The resulting variable is a string of a single
5534 * character. If the index is too big or negative the
5535 * result is empty. */
5536 if (n1 >= len || n1 < 0)
5537 s = NULL;
5538 else
5539 s = vim_strnsave(s + n1, 1);
5540 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005541 clear_tv(rettv);
5542 rettv->v_type = VAR_STRING;
5543 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005544 break;
5545
5546 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005547 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005548 if (n1 < 0)
5549 n1 = len + n1;
5550 if (!empty1 && (n1 < 0 || n1 >= len))
5551 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005552 /* For a range we allow invalid values and return an empty
5553 * list. A list index out of range is an error. */
5554 if (!range)
5555 {
5556 if (verbose)
5557 EMSGN(_(e_listidx), n1);
5558 return FAIL;
5559 }
5560 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005561 }
5562 if (range)
5563 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005564 list_T *l;
5565 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005566
5567 if (n2 < 0)
5568 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005569 else if (n2 >= len)
5570 n2 = len - 1;
5571 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005572 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005573 l = list_alloc();
5574 if (l == NULL)
5575 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005576 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005577 n1 <= n2; ++n1)
5578 {
5579 if (list_append_tv(l, &item->li_tv) == FAIL)
5580 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005581 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005582 return FAIL;
5583 }
5584 item = item->li_next;
5585 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005586 clear_tv(rettv);
5587 rettv->v_type = VAR_LIST;
5588 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005589 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005590 }
5591 else
5592 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005593 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005594 clear_tv(rettv);
5595 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005596 }
5597 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005598
5599 case VAR_DICT:
5600 if (range)
5601 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005602 if (verbose)
5603 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005604 if (len == -1)
5605 clear_tv(&var1);
5606 return FAIL;
5607 }
5608 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005609 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005610
5611 if (len == -1)
5612 {
5613 key = get_tv_string(&var1);
5614 if (*key == NUL)
5615 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005616 if (verbose)
5617 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005618 clear_tv(&var1);
5619 return FAIL;
5620 }
5621 }
5622
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005623 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005624
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005625 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005626 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005627 if (len == -1)
5628 clear_tv(&var1);
5629 if (item == NULL)
5630 return FAIL;
5631
5632 copy_tv(&item->di_tv, &var1);
5633 clear_tv(rettv);
5634 *rettv = var1;
5635 }
5636 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005637 }
5638 }
5639
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005640 return OK;
5641}
5642
5643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 * Get an option value.
5645 * "arg" points to the '&' or '+' before the option name.
5646 * "arg" is advanced to character after the option name.
5647 * Return OK or FAIL.
5648 */
5649 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005650get_option_tv(
5651 char_u **arg,
5652 typval_T *rettv, /* when NULL, only check if option exists */
5653 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654{
5655 char_u *option_end;
5656 long numval;
5657 char_u *stringval;
5658 int opt_type;
5659 int c;
5660 int working = (**arg == '+'); /* has("+option") */
5661 int ret = OK;
5662 int opt_flags;
5663
5664 /*
5665 * Isolate the option name and find its value.
5666 */
5667 option_end = find_option_end(arg, &opt_flags);
5668 if (option_end == NULL)
5669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005670 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 EMSG2(_("E112: Option name missing: %s"), *arg);
5672 return FAIL;
5673 }
5674
5675 if (!evaluate)
5676 {
5677 *arg = option_end;
5678 return OK;
5679 }
5680
5681 c = *option_end;
5682 *option_end = NUL;
5683 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005684 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685
5686 if (opt_type == -3) /* invalid name */
5687 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005688 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 EMSG2(_("E113: Unknown option: %s"), *arg);
5690 ret = FAIL;
5691 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005692 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 {
5694 if (opt_type == -2) /* hidden string option */
5695 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005696 rettv->v_type = VAR_STRING;
5697 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 }
5699 else if (opt_type == -1) /* hidden number option */
5700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005701 rettv->v_type = VAR_NUMBER;
5702 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 }
5704 else if (opt_type == 1) /* number option */
5705 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005706 rettv->v_type = VAR_NUMBER;
5707 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 }
5709 else /* string option */
5710 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005711 rettv->v_type = VAR_STRING;
5712 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 }
5714 }
5715 else if (working && (opt_type == -2 || opt_type == -1))
5716 ret = FAIL;
5717
5718 *option_end = c; /* put back for error messages */
5719 *arg = option_end;
5720
5721 return ret;
5722}
5723
5724/*
5725 * Allocate a variable for a string constant.
5726 * Return OK or FAIL.
5727 */
5728 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005729get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730{
5731 char_u *p;
5732 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 int extra = 0;
5734
5735 /*
5736 * Find the end of the string, skipping backslashed characters.
5737 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005738 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 {
5740 if (*p == '\\' && p[1] != NUL)
5741 {
5742 ++p;
5743 /* A "\<x>" form occupies at least 4 characters, and produces up
5744 * to 6 characters: reserve space for 2 extra */
5745 if (*p == '<')
5746 extra += 2;
5747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 }
5749
5750 if (*p != '"')
5751 {
5752 EMSG2(_("E114: Missing quote: %s"), *arg);
5753 return FAIL;
5754 }
5755
5756 /* If only parsing, set *arg and return here */
5757 if (!evaluate)
5758 {
5759 *arg = p + 1;
5760 return OK;
5761 }
5762
5763 /*
5764 * Copy the string into allocated memory, handling backslashed
5765 * characters.
5766 */
5767 name = alloc((unsigned)(p - *arg + extra));
5768 if (name == NULL)
5769 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005770 rettv->v_type = VAR_STRING;
5771 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772
Bram Moolenaar8c711452005-01-14 21:53:12 +00005773 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 {
5775 if (*p == '\\')
5776 {
5777 switch (*++p)
5778 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 case 'b': *name++ = BS; ++p; break;
5780 case 'e': *name++ = ESC; ++p; break;
5781 case 'f': *name++ = FF; ++p; break;
5782 case 'n': *name++ = NL; ++p; break;
5783 case 'r': *name++ = CAR; ++p; break;
5784 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785
5786 case 'X': /* hex: "\x1", "\x12" */
5787 case 'x':
5788 case 'u': /* Unicode: "\u0023" */
5789 case 'U':
5790 if (vim_isxdigit(p[1]))
5791 {
5792 int n, nr;
5793 int c = toupper(*p);
5794
5795 if (c == 'X')
5796 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005797 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005799 else
5800 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 nr = 0;
5802 while (--n >= 0 && vim_isxdigit(p[1]))
5803 {
5804 ++p;
5805 nr = (nr << 4) + hex2nr(*p);
5806 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005807 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808#ifdef FEAT_MBYTE
5809 /* For "\u" store the number according to
5810 * 'encoding'. */
5811 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 else
5814#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005815 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 break;
5818
5819 /* octal: "\1", "\12", "\123" */
5820 case '0':
5821 case '1':
5822 case '2':
5823 case '3':
5824 case '4':
5825 case '5':
5826 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005827 case '7': *name = *p++ - '0';
5828 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005830 *name = (*name << 3) + *p++ - '0';
5831 if (*p >= '0' && *p <= '7')
5832 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 break;
5836
5837 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 if (extra != 0)
5840 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 break;
5843 }
5844 /* FALLTHROUGH */
5845
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 break;
5848 }
5849 }
5850 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005854 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 *arg = p + 1;
5856
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 return OK;
5858}
5859
5860/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 * Return OK or FAIL.
5863 */
5864 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005865get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866{
5867 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005868 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005869 int reduce = 0;
5870
5871 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005872 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005873 */
5874 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5875 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005876 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005877 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005878 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005879 break;
5880 ++reduce;
5881 ++p;
5882 }
5883 }
5884
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005886 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005887 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888 return FAIL;
5889 }
5890
Bram Moolenaar8c711452005-01-14 21:53:12 +00005891 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005892 if (!evaluate)
5893 {
5894 *arg = p + 1;
5895 return OK;
5896 }
5897
5898 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005899 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005900 */
5901 str = alloc((unsigned)((p - *arg) - reduce));
5902 if (str == NULL)
5903 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005904 rettv->v_type = VAR_STRING;
5905 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005906
Bram Moolenaar8c711452005-01-14 21:53:12 +00005907 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005908 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005909 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005910 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005911 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005912 break;
5913 ++p;
5914 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005915 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005916 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005917 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005918 *arg = p + 1;
5919
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005920 return OK;
5921}
5922
5923/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005924 * Allocate a variable for a List and fill it from "*arg".
5925 * Return OK or FAIL.
5926 */
5927 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005928get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005929{
Bram Moolenaar33570922005-01-25 22:26:29 +00005930 list_T *l = NULL;
5931 typval_T tv;
5932 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933
5934 if (evaluate)
5935 {
5936 l = list_alloc();
5937 if (l == NULL)
5938 return FAIL;
5939 }
5940
5941 *arg = skipwhite(*arg + 1);
5942 while (**arg != ']' && **arg != NUL)
5943 {
5944 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5945 goto failret;
5946 if (evaluate)
5947 {
5948 item = listitem_alloc();
5949 if (item != NULL)
5950 {
5951 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005952 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953 list_append(l, item);
5954 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005955 else
5956 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957 }
5958
5959 if (**arg == ']')
5960 break;
5961 if (**arg != ',')
5962 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005963 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005964 goto failret;
5965 }
5966 *arg = skipwhite(*arg + 1);
5967 }
5968
5969 if (**arg != ']')
5970 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005971 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972failret:
5973 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005974 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975 return FAIL;
5976 }
5977
5978 *arg = skipwhite(*arg + 1);
5979 if (evaluate)
5980 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005981 rettv->v_type = VAR_LIST;
5982 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 ++l->lv_refcount;
5984 }
5985
5986 return OK;
5987}
5988
5989/*
5990 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005991 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005993 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005994list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005995{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005996 list_T *l;
5997
5998 l = (list_T *)alloc_clear(sizeof(list_T));
5999 if (l != NULL)
6000 {
6001 /* Prepend the list to the list of lists for garbage collection. */
6002 if (first_list != NULL)
6003 first_list->lv_used_prev = l;
6004 l->lv_used_prev = NULL;
6005 l->lv_used_next = first_list;
6006 first_list = l;
6007 }
6008 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009}
6010
6011/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006012 * Allocate an empty list for a return value.
6013 * Returns OK or FAIL.
6014 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006015 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006016rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006017{
6018 list_T *l = list_alloc();
6019
6020 if (l == NULL)
6021 return FAIL;
6022
6023 rettv->vval.v_list = l;
6024 rettv->v_type = VAR_LIST;
6025 ++l->lv_refcount;
6026 return OK;
6027}
6028
6029/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030 * Unreference a list: decrement the reference count and free it when it
6031 * becomes zero.
6032 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006033 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006034list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006036 if (l != NULL && --l->lv_refcount <= 0)
6037 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038}
6039
6040/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006041 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 * Ignores the reference count.
6043 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006044 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006045list_free(
6046 list_T *l,
6047 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006048{
Bram Moolenaar33570922005-01-25 22:26:29 +00006049 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006051 /* Remove the list from the list of lists for garbage collection. */
6052 if (l->lv_used_prev == NULL)
6053 first_list = l->lv_used_next;
6054 else
6055 l->lv_used_prev->lv_used_next = l->lv_used_next;
6056 if (l->lv_used_next != NULL)
6057 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6058
Bram Moolenaard9fba312005-06-26 22:34:35 +00006059 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006061 /* Remove the item before deleting it. */
6062 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006063 if (recurse || (item->li_tv.v_type != VAR_LIST
6064 && item->li_tv.v_type != VAR_DICT))
6065 clear_tv(&item->li_tv);
6066 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006067 }
6068 vim_free(l);
6069}
6070
6071/*
6072 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006073 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006075 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006076listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006077{
Bram Moolenaar33570922005-01-25 22:26:29 +00006078 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006079}
6080
6081/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006082 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006083 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006084 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006085listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006086{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006087 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088 vim_free(item);
6089}
6090
6091/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006092 * Remove a list item from a List and free it. Also clears the value.
6093 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006094 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006095listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006096{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006097 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006098 listitem_free(item);
6099}
6100
6101/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006102 * Get the number of items in a list.
6103 */
6104 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006105list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006106{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006107 if (l == NULL)
6108 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006109 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006110}
6111
6112/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006113 * Return TRUE when two lists have exactly the same values.
6114 */
6115 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006116list_equal(
6117 list_T *l1,
6118 list_T *l2,
6119 int ic, /* ignore case for strings */
6120 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006121{
Bram Moolenaar33570922005-01-25 22:26:29 +00006122 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006123
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006124 if (l1 == NULL || l2 == NULL)
6125 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006126 if (l1 == l2)
6127 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006128 if (list_len(l1) != list_len(l2))
6129 return FALSE;
6130
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006131 for (item1 = l1->lv_first, item2 = l2->lv_first;
6132 item1 != NULL && item2 != NULL;
6133 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006134 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006135 return FALSE;
6136 return item1 == NULL && item2 == NULL;
6137}
6138
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006139/*
6140 * Return the dictitem that an entry in a hashtable points to.
6141 */
6142 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006143dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006144{
6145 return HI2DI(hi);
6146}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006147
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006148/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006149 * Return TRUE when two dictionaries have exactly the same key/values.
6150 */
6151 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006152dict_equal(
6153 dict_T *d1,
6154 dict_T *d2,
6155 int ic, /* ignore case for strings */
6156 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006157{
Bram Moolenaar33570922005-01-25 22:26:29 +00006158 hashitem_T *hi;
6159 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006160 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006161
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006162 if (d1 == NULL || d2 == NULL)
6163 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006164 if (d1 == d2)
6165 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006166 if (dict_len(d1) != dict_len(d2))
6167 return FALSE;
6168
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006169 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006170 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006171 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006172 if (!HASHITEM_EMPTY(hi))
6173 {
6174 item2 = dict_find(d2, hi->hi_key, -1);
6175 if (item2 == NULL)
6176 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006177 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006178 return FALSE;
6179 --todo;
6180 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006181 }
6182 return TRUE;
6183}
6184
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006185static int tv_equal_recurse_limit;
6186
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006187/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006188 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006189 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006190 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006191 */
6192 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006193tv_equal(
6194 typval_T *tv1,
6195 typval_T *tv2,
6196 int ic, /* ignore case */
6197 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006198{
6199 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006200 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006201 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006202 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006203
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006204 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006205 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006206
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006207 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006208 * recursiveness to a limit. We guess they are equal then.
6209 * A fixed limit has the problem of still taking an awful long time.
6210 * Reduce the limit every time running into it. That should work fine for
6211 * deeply linked structures that are not recursively linked and catch
6212 * recursiveness quickly. */
6213 if (!recursive)
6214 tv_equal_recurse_limit = 1000;
6215 if (recursive_cnt >= tv_equal_recurse_limit)
6216 {
6217 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006218 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006219 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006220
6221 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006222 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006223 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006224 ++recursive_cnt;
6225 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6226 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006227 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006228
6229 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006230 ++recursive_cnt;
6231 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6232 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006233 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006234
6235 case VAR_FUNC:
6236 return (tv1->vval.v_string != NULL
6237 && tv2->vval.v_string != NULL
6238 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6239
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006240 case VAR_PARTIAL:
6241 return tv1->vval.v_partial != NULL
6242 && tv1->vval.v_partial == tv2->vval.v_partial;
6243
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006244 case VAR_NUMBER:
6245 return tv1->vval.v_number == tv2->vval.v_number;
6246
6247 case VAR_STRING:
6248 s1 = get_tv_string_buf(tv1, buf1);
6249 s2 = get_tv_string_buf(tv2, buf2);
6250 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006251
6252 case VAR_SPECIAL:
6253 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006254
6255 case VAR_FLOAT:
6256#ifdef FEAT_FLOAT
6257 return tv1->vval.v_float == tv2->vval.v_float;
6258#endif
6259 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006260#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006261 return tv1->vval.v_job == tv2->vval.v_job;
6262#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006263 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006264#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006265 return tv1->vval.v_channel == tv2->vval.v_channel;
6266#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006267 case VAR_UNKNOWN:
6268 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006269 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006270
Bram Moolenaara03f2332016-02-06 18:09:59 +01006271 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6272 * does not equal anything, not even itself. */
6273 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006274}
6275
6276/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006277 * Locate item with index "n" in list "l" and return it.
6278 * A negative index is counted from the end; -1 is the last item.
6279 * Returns NULL when "n" is out of range.
6280 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006281 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006282list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006283{
Bram Moolenaar33570922005-01-25 22:26:29 +00006284 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006285 long idx;
6286
6287 if (l == NULL)
6288 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006289
6290 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006291 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006292 n = l->lv_len + n;
6293
6294 /* Check for index out of range. */
6295 if (n < 0 || n >= l->lv_len)
6296 return NULL;
6297
6298 /* When there is a cached index may start search from there. */
6299 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006300 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006301 if (n < l->lv_idx / 2)
6302 {
6303 /* closest to the start of the list */
6304 item = l->lv_first;
6305 idx = 0;
6306 }
6307 else if (n > (l->lv_idx + l->lv_len) / 2)
6308 {
6309 /* closest to the end of the list */
6310 item = l->lv_last;
6311 idx = l->lv_len - 1;
6312 }
6313 else
6314 {
6315 /* closest to the cached index */
6316 item = l->lv_idx_item;
6317 idx = l->lv_idx;
6318 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006319 }
6320 else
6321 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006322 if (n < l->lv_len / 2)
6323 {
6324 /* closest to the start of the list */
6325 item = l->lv_first;
6326 idx = 0;
6327 }
6328 else
6329 {
6330 /* closest to the end of the list */
6331 item = l->lv_last;
6332 idx = l->lv_len - 1;
6333 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006334 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006335
6336 while (n > idx)
6337 {
6338 /* search forward */
6339 item = item->li_next;
6340 ++idx;
6341 }
6342 while (n < idx)
6343 {
6344 /* search backward */
6345 item = item->li_prev;
6346 --idx;
6347 }
6348
6349 /* cache the used index */
6350 l->lv_idx = idx;
6351 l->lv_idx_item = item;
6352
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006353 return item;
6354}
6355
6356/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006357 * Get list item "l[idx]" as a number.
6358 */
6359 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006360list_find_nr(
6361 list_T *l,
6362 long idx,
6363 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006364{
6365 listitem_T *li;
6366
6367 li = list_find(l, idx);
6368 if (li == NULL)
6369 {
6370 if (errorp != NULL)
6371 *errorp = TRUE;
6372 return -1L;
6373 }
6374 return get_tv_number_chk(&li->li_tv, errorp);
6375}
6376
6377/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006378 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6379 */
6380 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006381list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006382{
6383 listitem_T *li;
6384
6385 li = list_find(l, idx - 1);
6386 if (li == NULL)
6387 {
6388 EMSGN(_(e_listidx), idx);
6389 return NULL;
6390 }
6391 return get_tv_string(&li->li_tv);
6392}
6393
6394/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006395 * Locate "item" list "l" and return its index.
6396 * Returns -1 when "item" is not in the list.
6397 */
6398 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006399list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006400{
6401 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006402 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006403
6404 if (l == NULL)
6405 return -1;
6406 idx = 0;
6407 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6408 ++idx;
6409 if (li == NULL)
6410 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006411 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006412}
6413
6414/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006415 * Append item "item" to the end of list "l".
6416 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006417 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006418list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006419{
6420 if (l->lv_last == NULL)
6421 {
6422 /* empty list */
6423 l->lv_first = item;
6424 l->lv_last = item;
6425 item->li_prev = NULL;
6426 }
6427 else
6428 {
6429 l->lv_last->li_next = item;
6430 item->li_prev = l->lv_last;
6431 l->lv_last = item;
6432 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006433 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006434 item->li_next = NULL;
6435}
6436
6437/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006438 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006439 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006440 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006441 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006442list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006443{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006444 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006445
Bram Moolenaar05159a02005-02-26 23:04:13 +00006446 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006447 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006448 copy_tv(tv, &li->li_tv);
6449 list_append(l, li);
6450 return OK;
6451}
6452
6453/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006454 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006455 * Return FAIL when out of memory.
6456 */
6457 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006458list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006459{
6460 listitem_T *li = listitem_alloc();
6461
6462 if (li == NULL)
6463 return FAIL;
6464 li->li_tv.v_type = VAR_DICT;
6465 li->li_tv.v_lock = 0;
6466 li->li_tv.vval.v_dict = dict;
6467 list_append(list, li);
6468 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006469 return OK;
6470}
6471
6472/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006473 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006474 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006475 * Returns FAIL when out of memory.
6476 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006477 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006478list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006479{
6480 listitem_T *li = listitem_alloc();
6481
6482 if (li == NULL)
6483 return FAIL;
6484 list_append(l, li);
6485 li->li_tv.v_type = VAR_STRING;
6486 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006487 if (str == NULL)
6488 li->li_tv.vval.v_string = NULL;
6489 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006490 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006491 return FAIL;
6492 return OK;
6493}
6494
6495/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006496 * Append "n" to list "l".
6497 * Returns FAIL when out of memory.
6498 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006499 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006500list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006501{
6502 listitem_T *li;
6503
6504 li = listitem_alloc();
6505 if (li == NULL)
6506 return FAIL;
6507 li->li_tv.v_type = VAR_NUMBER;
6508 li->li_tv.v_lock = 0;
6509 li->li_tv.vval.v_number = n;
6510 list_append(l, li);
6511 return OK;
6512}
6513
6514/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006515 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006516 * If "item" is NULL append at the end.
6517 * Return FAIL when out of memory.
6518 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006519 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006520list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006521{
Bram Moolenaar33570922005-01-25 22:26:29 +00006522 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006523
6524 if (ni == NULL)
6525 return FAIL;
6526 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006527 list_insert(l, ni, item);
6528 return OK;
6529}
6530
6531 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006532list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006533{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006534 if (item == NULL)
6535 /* Append new item at end of list. */
6536 list_append(l, ni);
6537 else
6538 {
6539 /* Insert new item before existing item. */
6540 ni->li_prev = item->li_prev;
6541 ni->li_next = item;
6542 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006543 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006545 ++l->lv_idx;
6546 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006547 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006548 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006549 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006550 l->lv_idx_item = NULL;
6551 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006552 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006553 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006554 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555}
6556
6557/*
6558 * Extend "l1" with "l2".
6559 * If "bef" is NULL append at the end, otherwise insert before this item.
6560 * Returns FAIL when out of memory.
6561 */
6562 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006563list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006564{
Bram Moolenaar33570922005-01-25 22:26:29 +00006565 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006566 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006567
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006568 /* We also quit the loop when we have inserted the original item count of
6569 * the list, avoid a hang when we extend a list with itself. */
6570 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006571 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6572 return FAIL;
6573 return OK;
6574}
6575
6576/*
6577 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6578 * Return FAIL when out of memory.
6579 */
6580 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006581list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006582{
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006584
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006585 if (l1 == NULL || l2 == NULL)
6586 return FAIL;
6587
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006588 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006589 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006590 if (l == NULL)
6591 return FAIL;
6592 tv->v_type = VAR_LIST;
6593 tv->vval.v_list = l;
6594
6595 /* append all items from the second list */
6596 return list_extend(l, l2, NULL);
6597}
6598
6599/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006600 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006601 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006602 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006603 * Returns NULL when out of memory.
6604 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006605 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006606list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006607{
Bram Moolenaar33570922005-01-25 22:26:29 +00006608 list_T *copy;
6609 listitem_T *item;
6610 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006611
6612 if (orig == NULL)
6613 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006614
6615 copy = list_alloc();
6616 if (copy != NULL)
6617 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006618 if (copyID != 0)
6619 {
6620 /* Do this before adding the items, because one of the items may
6621 * refer back to this list. */
6622 orig->lv_copyID = copyID;
6623 orig->lv_copylist = copy;
6624 }
6625 for (item = orig->lv_first; item != NULL && !got_int;
6626 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006627 {
6628 ni = listitem_alloc();
6629 if (ni == NULL)
6630 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006631 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006632 {
6633 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6634 {
6635 vim_free(ni);
6636 break;
6637 }
6638 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006639 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006640 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006641 list_append(copy, ni);
6642 }
6643 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006644 if (item != NULL)
6645 {
6646 list_unref(copy);
6647 copy = NULL;
6648 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006649 }
6650
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006651 return copy;
6652}
6653
6654/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006655 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006656 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006657 * This used to be called list_remove, but that conflicts with a Sun header
6658 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006659 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006660 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006661vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006662{
Bram Moolenaar33570922005-01-25 22:26:29 +00006663 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006664
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006665 /* notify watchers */
6666 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006668 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006669 list_fix_watch(l, ip);
6670 if (ip == item2)
6671 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006672 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006673
6674 if (item2->li_next == NULL)
6675 l->lv_last = item->li_prev;
6676 else
6677 item2->li_next->li_prev = item->li_prev;
6678 if (item->li_prev == NULL)
6679 l->lv_first = item2->li_next;
6680 else
6681 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006682 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006683}
6684
6685/*
6686 * Return an allocated string with the string representation of a list.
6687 * May return NULL.
6688 */
6689 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006690list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006691{
6692 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006693
6694 if (tv->vval.v_list == NULL)
6695 return NULL;
6696 ga_init2(&ga, (int)sizeof(char), 80);
6697 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006698 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006699 {
6700 vim_free(ga.ga_data);
6701 return NULL;
6702 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006703 ga_append(&ga, ']');
6704 ga_append(&ga, NUL);
6705 return (char_u *)ga.ga_data;
6706}
6707
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006708typedef struct join_S {
6709 char_u *s;
6710 char_u *tofree;
6711} join_T;
6712
6713 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006714list_join_inner(
6715 garray_T *gap, /* to store the result in */
6716 list_T *l,
6717 char_u *sep,
6718 int echo_style,
6719 int copyID,
6720 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006721{
6722 int i;
6723 join_T *p;
6724 int len;
6725 int sumlen = 0;
6726 int first = TRUE;
6727 char_u *tofree;
6728 char_u numbuf[NUMBUFLEN];
6729 listitem_T *item;
6730 char_u *s;
6731
6732 /* Stringify each item in the list. */
6733 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6734 {
6735 if (echo_style)
6736 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6737 else
6738 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6739 if (s == NULL)
6740 return FAIL;
6741
6742 len = (int)STRLEN(s);
6743 sumlen += len;
6744
Bram Moolenaarcde88542015-08-11 19:14:00 +02006745 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006746 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6747 if (tofree != NULL || s != numbuf)
6748 {
6749 p->s = s;
6750 p->tofree = tofree;
6751 }
6752 else
6753 {
6754 p->s = vim_strnsave(s, len);
6755 p->tofree = p->s;
6756 }
6757
6758 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006759 if (did_echo_string_emsg) /* recursion error, bail out */
6760 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006761 }
6762
6763 /* Allocate result buffer with its total size, avoid re-allocation and
6764 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6765 if (join_gap->ga_len >= 2)
6766 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6767 if (ga_grow(gap, sumlen + 2) == FAIL)
6768 return FAIL;
6769
6770 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6771 {
6772 if (first)
6773 first = FALSE;
6774 else
6775 ga_concat(gap, sep);
6776 p = ((join_T *)join_gap->ga_data) + i;
6777
6778 if (p->s != NULL)
6779 ga_concat(gap, p->s);
6780 line_breakcheck();
6781 }
6782
6783 return OK;
6784}
6785
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006786/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006787 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006788 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006789 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006790 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006791 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006792list_join(
6793 garray_T *gap,
6794 list_T *l,
6795 char_u *sep,
6796 int echo_style,
6797 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006798{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006799 garray_T join_ga;
6800 int retval;
6801 join_T *p;
6802 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006803
Bram Moolenaard39a7512015-04-16 22:51:22 +02006804 if (l->lv_len < 1)
6805 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006806 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6807 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6808
6809 /* Dispose each item in join_ga. */
6810 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006811 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006812 p = (join_T *)join_ga.ga_data;
6813 for (i = 0; i < join_ga.ga_len; ++i)
6814 {
6815 vim_free(p->tofree);
6816 ++p;
6817 }
6818 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006819 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006820
6821 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006822}
6823
6824/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006825 * Return the next (unique) copy ID.
6826 * Used for serializing nested structures.
6827 */
6828 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006829get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006830{
6831 current_copyID += COPYID_INC;
6832 return current_copyID;
6833}
6834
6835/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006836 * Garbage collection for lists and dictionaries.
6837 *
6838 * We use reference counts to be able to free most items right away when they
6839 * are no longer used. But for composite items it's possible that it becomes
6840 * unused while the reference count is > 0: When there is a recursive
6841 * reference. Example:
6842 * :let l = [1, 2, 3]
6843 * :let d = {9: l}
6844 * :let l[1] = d
6845 *
6846 * Since this is quite unusual we handle this with garbage collection: every
6847 * once in a while find out which lists and dicts are not referenced from any
6848 * variable.
6849 *
6850 * Here is a good reference text about garbage collection (refers to Python
6851 * but it applies to all reference-counting mechanisms):
6852 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006853 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006854
6855/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006856 * Do garbage collection for lists and dicts.
6857 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006858 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006859 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006860garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006861{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006862 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006863 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006864 buf_T *buf;
6865 win_T *wp;
6866 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006867 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006868 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006869 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006870#ifdef FEAT_WINDOWS
6871 tabpage_T *tp;
6872#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006873
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006874 /* Only do this once. */
6875 want_garbage_collect = FALSE;
6876 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006877 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006878
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006879 /* We advance by two because we add one for items referenced through
6880 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006881 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006882
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006883 /*
6884 * 1. Go through all accessible variables and mark all lists and dicts
6885 * with copyID.
6886 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006887
6888 /* Don't free variables in the previous_funccal list unless they are only
6889 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006890 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006891 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6892 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006893 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6894 NULL);
6895 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6896 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006897 }
6898
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006899 /* script-local variables */
6900 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006901 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006902
6903 /* buffer-local variables */
6904 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006905 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6906 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006907
6908 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006909 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006910 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6911 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006912#ifdef FEAT_AUTOCMD
6913 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006914 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6915 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006916#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006917
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006918#ifdef FEAT_WINDOWS
6919 /* tabpage-local variables */
6920 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006921 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6922 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006923#endif
6924
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006925 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006926 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006927
6928 /* function-local variables */
6929 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6930 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006931 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6932 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006933 }
6934
Bram Moolenaard812df62008-11-09 12:46:09 +00006935 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006936 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006937
Bram Moolenaar1dced572012-04-05 16:54:08 +02006938#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006939 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006940#endif
6941
Bram Moolenaardb913952012-06-29 12:54:53 +02006942#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006943 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006944#endif
6945
6946#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006947 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006948#endif
6949
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006950#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006951 abort = abort || set_ref_in_channel(copyID);
6952#endif
6953
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006954 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006955 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006956 /*
6957 * 2. Free lists and dictionaries that are not referenced.
6958 */
6959 did_free = free_unref_items(copyID);
6960
6961 /*
6962 * 3. Check if any funccal can be freed now.
6963 */
6964 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006965 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006966 if (can_free_funccal(*pfc, copyID))
6967 {
6968 fc = *pfc;
6969 *pfc = fc->caller;
6970 free_funccal(fc, TRUE);
6971 did_free = TRUE;
6972 did_free_funccal = TRUE;
6973 }
6974 else
6975 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006976 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006977 if (did_free_funccal)
6978 /* When a funccal was freed some more items might be garbage
6979 * collected, so run again. */
6980 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006981 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006982 else if (p_verbose > 0)
6983 {
6984 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6985 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006986
6987 return did_free;
6988}
6989
6990/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006991 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006992 */
6993 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006994free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006995{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006996 dict_T *dd, *dd_next;
6997 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006998 int did_free = FALSE;
6999
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007001 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 */
7003 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007004 {
7005 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007006 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007007 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007008 /* Free the Dictionary and ordinary items it contains, but don't
7009 * recurse into Lists and Dictionaries, they will be in the list
7010 * of dicts or list of lists. */
7011 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007012 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007013 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007014 dd = dd_next;
7015 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016
7017 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007018 * Go through the list of lists and free items without the copyID.
7019 * But don't free a list that has a watcher (used in a for loop), these
7020 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021 */
7022 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007023 {
7024 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007025 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7026 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007027 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007028 /* Free the List and ordinary items it contains, but don't recurse
7029 * into Lists and Dictionaries, they will be in the list of dicts
7030 * or list of lists. */
7031 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007032 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007034 ll = ll_next;
7035 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007036
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007037 return did_free;
7038}
7039
7040/*
7041 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007042 * "list_stack" is used to add lists to be marked. Can be NULL.
7043 *
7044 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007045 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007046 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007047set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007048{
7049 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007050 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007051 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007052 hashtab_T *cur_ht;
7053 ht_stack_T *ht_stack = NULL;
7054 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007055
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007056 cur_ht = ht;
7057 for (;;)
7058 {
7059 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007060 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007061 /* Mark each item in the hashtab. If the item contains a hashtab
7062 * it is added to ht_stack, if it contains a list it is added to
7063 * list_stack. */
7064 todo = (int)cur_ht->ht_used;
7065 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7066 if (!HASHITEM_EMPTY(hi))
7067 {
7068 --todo;
7069 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7070 &ht_stack, list_stack);
7071 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007072 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007073
7074 if (ht_stack == NULL)
7075 break;
7076
7077 /* take an item from the stack */
7078 cur_ht = ht_stack->ht;
7079 tempitem = ht_stack;
7080 ht_stack = ht_stack->prev;
7081 free(tempitem);
7082 }
7083
7084 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007085}
7086
7087/*
7088 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007089 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7090 *
7091 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007092 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007093 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007094set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007095{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007096 listitem_T *li;
7097 int abort = FALSE;
7098 list_T *cur_l;
7099 list_stack_T *list_stack = NULL;
7100 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007101
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007102 cur_l = l;
7103 for (;;)
7104 {
7105 if (!abort)
7106 /* Mark each item in the list. If the item contains a hashtab
7107 * it is added to ht_stack, if it contains a list it is added to
7108 * list_stack. */
7109 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7110 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7111 ht_stack, &list_stack);
7112 if (list_stack == NULL)
7113 break;
7114
7115 /* take an item from the stack */
7116 cur_l = list_stack->list;
7117 tempitem = list_stack;
7118 list_stack = list_stack->prev;
7119 free(tempitem);
7120 }
7121
7122 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007123}
7124
7125/*
7126 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007127 * "list_stack" is used to add lists to be marked. Can be NULL.
7128 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7129 *
7130 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007131 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007132 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007133set_ref_in_item(
7134 typval_T *tv,
7135 int copyID,
7136 ht_stack_T **ht_stack,
7137 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007138{
7139 dict_T *dd;
7140 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007141 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007142
Bram Moolenaara03f2332016-02-06 18:09:59 +01007143 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007144 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007145 dd = tv->vval.v_dict;
7146 if (dd != NULL && dd->dv_copyID != copyID)
7147 {
7148 /* Didn't see this dict yet. */
7149 dd->dv_copyID = copyID;
7150 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007151 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007152 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7153 }
7154 else
7155 {
7156 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7157 if (newitem == NULL)
7158 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007159 else
7160 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007161 newitem->ht = &dd->dv_hashtab;
7162 newitem->prev = *ht_stack;
7163 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007164 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007165 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007166 }
7167 }
7168 else if (tv->v_type == VAR_LIST)
7169 {
7170 ll = tv->vval.v_list;
7171 if (ll != NULL && ll->lv_copyID != copyID)
7172 {
7173 /* Didn't see this list yet. */
7174 ll->lv_copyID = copyID;
7175 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007176 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007177 abort = set_ref_in_list(ll, copyID, ht_stack);
7178 }
7179 else
7180 {
7181 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007182 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007183 if (newitem == NULL)
7184 abort = TRUE;
7185 else
7186 {
7187 newitem->list = ll;
7188 newitem->prev = *list_stack;
7189 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007190 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007191 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007192 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007193 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007194 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007195}
7196
7197/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007198 * Allocate an empty header for a dictionary.
7199 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007200 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007201dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007202{
Bram Moolenaar33570922005-01-25 22:26:29 +00007203 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007204
Bram Moolenaar33570922005-01-25 22:26:29 +00007205 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007206 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007207 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007208 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007209 if (first_dict != NULL)
7210 first_dict->dv_used_prev = d;
7211 d->dv_used_next = first_dict;
7212 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007213 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007214
Bram Moolenaar33570922005-01-25 22:26:29 +00007215 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007216 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007217 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007218 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007219 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007220 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007221 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222}
7223
7224/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007225 * Allocate an empty dict for a return value.
7226 * Returns OK or FAIL.
7227 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007228 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007229rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007230{
7231 dict_T *d = dict_alloc();
7232
7233 if (d == NULL)
7234 return FAIL;
7235
7236 rettv->vval.v_dict = d;
7237 rettv->v_type = VAR_DICT;
7238 ++d->dv_refcount;
7239 return OK;
7240}
7241
7242
7243/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007244 * Unreference a Dictionary: decrement the reference count and free it when it
7245 * becomes zero.
7246 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007247 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007248dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007249{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007250 if (d != NULL && --d->dv_refcount <= 0)
7251 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252}
7253
7254/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007255 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007256 * Ignores the reference count.
7257 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007258 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007259dict_free(
7260 dict_T *d,
7261 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007262{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007263 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007264 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007265 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007267 /* Remove the dict from the list of dicts for garbage collection. */
7268 if (d->dv_used_prev == NULL)
7269 first_dict = d->dv_used_next;
7270 else
7271 d->dv_used_prev->dv_used_next = d->dv_used_next;
7272 if (d->dv_used_next != NULL)
7273 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7274
7275 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007276 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007277 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007278 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007279 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007280 if (!HASHITEM_EMPTY(hi))
7281 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007282 /* Remove the item before deleting it, just in case there is
7283 * something recursive causing trouble. */
7284 di = HI2DI(hi);
7285 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007286 if (recurse || (di->di_tv.v_type != VAR_LIST
7287 && di->di_tv.v_type != VAR_DICT))
7288 clear_tv(&di->di_tv);
7289 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007290 --todo;
7291 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007292 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007293 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007294 vim_free(d);
7295}
7296
7297/*
7298 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007299 * The "key" is copied to the new item.
7300 * Note that the value of the item "di_tv" still needs to be initialized!
7301 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007303 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007304dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007305{
Bram Moolenaar33570922005-01-25 22:26:29 +00007306 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007308 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007309 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007310 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007311 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007312 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007313 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007315}
7316
7317/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007318 * Make a copy of a Dictionary item.
7319 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007320 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007321dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007322{
Bram Moolenaar33570922005-01-25 22:26:29 +00007323 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007324
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007325 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7326 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007327 if (di != NULL)
7328 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007329 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007330 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007331 copy_tv(&org->di_tv, &di->di_tv);
7332 }
7333 return di;
7334}
7335
7336/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007337 * Remove item "item" from Dictionary "dict" and free it.
7338 */
7339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007340dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007341{
Bram Moolenaar33570922005-01-25 22:26:29 +00007342 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007343
Bram Moolenaar33570922005-01-25 22:26:29 +00007344 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007345 if (HASHITEM_EMPTY(hi))
7346 EMSG2(_(e_intern2), "dictitem_remove()");
7347 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007348 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349 dictitem_free(item);
7350}
7351
7352/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007353 * Free a dict item. Also clears the value.
7354 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007355 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007356dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007357{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007358 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007359 if (item->di_flags & DI_FLAGS_ALLOC)
7360 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007361}
7362
7363/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007364 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7365 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007366 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007367 * Returns NULL when out of memory.
7368 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007369 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007370dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007371{
Bram Moolenaar33570922005-01-25 22:26:29 +00007372 dict_T *copy;
7373 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007374 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007375 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007376
7377 if (orig == NULL)
7378 return NULL;
7379
7380 copy = dict_alloc();
7381 if (copy != NULL)
7382 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007383 if (copyID != 0)
7384 {
7385 orig->dv_copyID = copyID;
7386 orig->dv_copydict = copy;
7387 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007388 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007389 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007390 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007391 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007392 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007393 --todo;
7394
7395 di = dictitem_alloc(hi->hi_key);
7396 if (di == NULL)
7397 break;
7398 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007399 {
7400 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7401 copyID) == FAIL)
7402 {
7403 vim_free(di);
7404 break;
7405 }
7406 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007407 else
7408 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7409 if (dict_add(copy, di) == FAIL)
7410 {
7411 dictitem_free(di);
7412 break;
7413 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007414 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007415 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007416
Bram Moolenaare9a41262005-01-15 22:18:47 +00007417 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007418 if (todo > 0)
7419 {
7420 dict_unref(copy);
7421 copy = NULL;
7422 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007423 }
7424
7425 return copy;
7426}
7427
7428/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007429 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007430 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007431 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007432 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007433dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007434{
Bram Moolenaar33570922005-01-25 22:26:29 +00007435 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007436}
7437
Bram Moolenaar8c711452005-01-14 21:53:12 +00007438/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007439 * Add a number or string entry to dictionary "d".
7440 * When "str" is NULL use number "nr", otherwise use "str".
7441 * Returns FAIL when out of memory and when key already exists.
7442 */
7443 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007444dict_add_nr_str(
7445 dict_T *d,
7446 char *key,
7447 long nr,
7448 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007449{
7450 dictitem_T *item;
7451
7452 item = dictitem_alloc((char_u *)key);
7453 if (item == NULL)
7454 return FAIL;
7455 item->di_tv.v_lock = 0;
7456 if (str == NULL)
7457 {
7458 item->di_tv.v_type = VAR_NUMBER;
7459 item->di_tv.vval.v_number = nr;
7460 }
7461 else
7462 {
7463 item->di_tv.v_type = VAR_STRING;
7464 item->di_tv.vval.v_string = vim_strsave(str);
7465 }
7466 if (dict_add(d, item) == FAIL)
7467 {
7468 dictitem_free(item);
7469 return FAIL;
7470 }
7471 return OK;
7472}
7473
7474/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007475 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007476 * Returns FAIL when out of memory and when key already exists.
7477 */
7478 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007479dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007480{
7481 dictitem_T *item;
7482
7483 item = dictitem_alloc((char_u *)key);
7484 if (item == NULL)
7485 return FAIL;
7486 item->di_tv.v_lock = 0;
7487 item->di_tv.v_type = VAR_LIST;
7488 item->di_tv.vval.v_list = list;
7489 if (dict_add(d, item) == FAIL)
7490 {
7491 dictitem_free(item);
7492 return FAIL;
7493 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007494 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007495 return OK;
7496}
7497
7498/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007499 * Get the number of items in a Dictionary.
7500 */
7501 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007502dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007503{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007504 if (d == NULL)
7505 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007506 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007507}
7508
7509/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007510 * Find item "key[len]" in Dictionary "d".
7511 * If "len" is negative use strlen(key).
7512 * Returns NULL when not found.
7513 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007514 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007515dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007516{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007517#define AKEYLEN 200
7518 char_u buf[AKEYLEN];
7519 char_u *akey;
7520 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007521 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007523 if (len < 0)
7524 akey = key;
7525 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007526 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007527 tofree = akey = vim_strnsave(key, len);
7528 if (akey == NULL)
7529 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007530 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007531 else
7532 {
7533 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007534 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007535 akey = buf;
7536 }
7537
Bram Moolenaar33570922005-01-25 22:26:29 +00007538 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007539 vim_free(tofree);
7540 if (HASHITEM_EMPTY(hi))
7541 return NULL;
7542 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007543}
7544
7545/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007546 * Get a string item from a dictionary.
7547 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007548 * Returns NULL if the entry doesn't exist or out of memory.
7549 */
7550 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007551get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007552{
7553 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007554 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007555
7556 di = dict_find(d, key, -1);
7557 if (di == NULL)
7558 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007559 s = get_tv_string(&di->di_tv);
7560 if (save && s != NULL)
7561 s = vim_strsave(s);
7562 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007563}
7564
7565/*
7566 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007567 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007568 */
7569 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007570get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007571{
7572 dictitem_T *di;
7573
7574 di = dict_find(d, key, -1);
7575 if (di == NULL)
7576 return 0;
7577 return get_tv_number(&di->di_tv);
7578}
7579
7580/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581 * Return an allocated string with the string representation of a Dictionary.
7582 * May return NULL.
7583 */
7584 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007585dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007586{
7587 garray_T ga;
7588 int first = TRUE;
7589 char_u *tofree;
7590 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007591 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007592 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007593 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007594 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007595
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007596 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007597 return NULL;
7598 ga_init2(&ga, (int)sizeof(char), 80);
7599 ga_append(&ga, '{');
7600
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007601 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007602 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007603 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007604 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007605 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007606 --todo;
7607
7608 if (first)
7609 first = FALSE;
7610 else
7611 ga_concat(&ga, (char_u *)", ");
7612
7613 tofree = string_quote(hi->hi_key, FALSE);
7614 if (tofree != NULL)
7615 {
7616 ga_concat(&ga, tofree);
7617 vim_free(tofree);
7618 }
7619 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007620 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007621 if (s != NULL)
7622 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007623 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007624 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007625 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007626 line_breakcheck();
7627
Bram Moolenaar8c711452005-01-14 21:53:12 +00007628 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007629 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007630 if (todo > 0)
7631 {
7632 vim_free(ga.ga_data);
7633 return NULL;
7634 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007635
7636 ga_append(&ga, '}');
7637 ga_append(&ga, NUL);
7638 return (char_u *)ga.ga_data;
7639}
7640
7641/*
7642 * Allocate a variable for a Dictionary and fill it from "*arg".
7643 * Return OK or FAIL. Returns NOTDONE for {expr}.
7644 */
7645 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007646get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007647{
Bram Moolenaar33570922005-01-25 22:26:29 +00007648 dict_T *d = NULL;
7649 typval_T tvkey;
7650 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007651 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007652 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007653 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007654 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655
7656 /*
7657 * First check if it's not a curly-braces thing: {expr}.
7658 * Must do this without evaluating, otherwise a function may be called
7659 * twice. Unfortunately this means we need to call eval1() twice for the
7660 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007661 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007663 if (*start != '}')
7664 {
7665 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7666 return FAIL;
7667 if (*start == '}')
7668 return NOTDONE;
7669 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007670
7671 if (evaluate)
7672 {
7673 d = dict_alloc();
7674 if (d == NULL)
7675 return FAIL;
7676 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007677 tvkey.v_type = VAR_UNKNOWN;
7678 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007679
7680 *arg = skipwhite(*arg + 1);
7681 while (**arg != '}' && **arg != NUL)
7682 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007683 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007684 goto failret;
7685 if (**arg != ':')
7686 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007687 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007688 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007689 goto failret;
7690 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007691 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007693 key = get_tv_string_buf_chk(&tvkey, buf);
7694 if (key == NULL || *key == NUL)
7695 {
7696 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7697 if (key != NULL)
7698 EMSG(_(e_emptykey));
7699 clear_tv(&tvkey);
7700 goto failret;
7701 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007703
7704 *arg = skipwhite(*arg + 1);
7705 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7706 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007707 if (evaluate)
7708 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007709 goto failret;
7710 }
7711 if (evaluate)
7712 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007713 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007714 if (item != NULL)
7715 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007716 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007717 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007718 clear_tv(&tv);
7719 goto failret;
7720 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007721 item = dictitem_alloc(key);
7722 clear_tv(&tvkey);
7723 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007724 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007725 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007726 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007727 if (dict_add(d, item) == FAIL)
7728 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007729 }
7730 }
7731
7732 if (**arg == '}')
7733 break;
7734 if (**arg != ',')
7735 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007736 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007737 goto failret;
7738 }
7739 *arg = skipwhite(*arg + 1);
7740 }
7741
7742 if (**arg != '}')
7743 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007744 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007745failret:
7746 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007747 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007748 return FAIL;
7749 }
7750
7751 *arg = skipwhite(*arg + 1);
7752 if (evaluate)
7753 {
7754 rettv->v_type = VAR_DICT;
7755 rettv->vval.v_dict = d;
7756 ++d->dv_refcount;
7757 }
7758
7759 return OK;
7760}
7761
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007762#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007763#endif
7764
Bram Moolenaar17a13432016-01-24 14:22:10 +01007765 static char *
7766get_var_special_name(int nr)
7767{
7768 switch (nr)
7769 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007770 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007771 case VVAL_TRUE: return "v:true";
7772 case VVAL_NONE: return "v:none";
7773 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007774 }
7775 EMSG2(_(e_intern2), "get_var_special_name()");
7776 return "42";
7777}
7778
Bram Moolenaar8c711452005-01-14 21:53:12 +00007779/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007780 * Return a string with the string representation of a variable.
7781 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007782 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007783 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007784 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007785 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007786 */
7787 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007788echo_string(
7789 typval_T *tv,
7790 char_u **tofree,
7791 char_u *numbuf,
7792 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007793{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007794 static int recurse = 0;
7795 char_u *r = NULL;
7796
Bram Moolenaar33570922005-01-25 22:26:29 +00007797 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007798 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007799 if (!did_echo_string_emsg)
7800 {
7801 /* Only give this message once for a recursive call to avoid
7802 * flooding the user with errors. And stop iterating over lists
7803 * and dicts. */
7804 did_echo_string_emsg = TRUE;
7805 EMSG(_("E724: variable nested too deep for displaying"));
7806 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007807 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007808 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007809 }
7810 ++recurse;
7811
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007812 switch (tv->v_type)
7813 {
7814 case VAR_FUNC:
7815 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007816 r = tv->vval.v_string;
7817 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007818
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007819 case VAR_PARTIAL:
7820 *tofree = NULL;
7821 /* TODO: arguments */
7822 r = tv->vval.v_partial == NULL ? NULL : tv->vval.v_partial->pt_name;
7823 break;
7824
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007825 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007826 if (tv->vval.v_list == NULL)
7827 {
7828 *tofree = NULL;
7829 r = NULL;
7830 }
7831 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7832 {
7833 *tofree = NULL;
7834 r = (char_u *)"[...]";
7835 }
7836 else
7837 {
7838 tv->vval.v_list->lv_copyID = copyID;
7839 *tofree = list2string(tv, copyID);
7840 r = *tofree;
7841 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007842 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007843
Bram Moolenaar8c711452005-01-14 21:53:12 +00007844 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007845 if (tv->vval.v_dict == NULL)
7846 {
7847 *tofree = NULL;
7848 r = NULL;
7849 }
7850 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7851 {
7852 *tofree = NULL;
7853 r = (char_u *)"{...}";
7854 }
7855 else
7856 {
7857 tv->vval.v_dict->dv_copyID = copyID;
7858 *tofree = dict2string(tv, copyID);
7859 r = *tofree;
7860 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007861 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007862
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007863 case VAR_STRING:
7864 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007865 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007866 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007867 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007868 *tofree = NULL;
7869 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007870 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007871
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007872 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007873#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007874 *tofree = NULL;
7875 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7876 r = numbuf;
7877 break;
7878#endif
7879
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007880 case VAR_SPECIAL:
7881 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007882 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007883 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007884 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007885
Bram Moolenaar8502c702014-06-17 12:51:16 +02007886 if (--recurse == 0)
7887 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007888 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007889}
7890
7891/*
7892 * Return a string with the string representation of a variable.
7893 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7894 * "numbuf" is used for a number.
7895 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007896 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007897 */
7898 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007899tv2string(
7900 typval_T *tv,
7901 char_u **tofree,
7902 char_u *numbuf,
7903 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007904{
7905 switch (tv->v_type)
7906 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007907 case VAR_FUNC:
7908 *tofree = string_quote(tv->vval.v_string, TRUE);
7909 return *tofree;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007910 case VAR_PARTIAL:
7911 *tofree = string_quote(tv->vval.v_partial == NULL ? NULL
7912 : tv->vval.v_partial->pt_name, TRUE);
7913 return *tofree;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007914 case VAR_STRING:
7915 *tofree = string_quote(tv->vval.v_string, FALSE);
7916 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007917 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01007918#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007919 *tofree = NULL;
7920 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7921 return numbuf;
7922#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007923 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007924 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007925 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007926 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007927 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007928 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007929 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007930 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007931 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007932 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007933}
7934
7935/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007936 * Return string "str" in ' quotes, doubling ' characters.
7937 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007938 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007939 */
7940 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007941string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007942{
Bram Moolenaar33570922005-01-25 22:26:29 +00007943 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007944 char_u *p, *r, *s;
7945
Bram Moolenaar33570922005-01-25 22:26:29 +00007946 len = (function ? 13 : 3);
7947 if (str != NULL)
7948 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007949 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007950 for (p = str; *p != NUL; mb_ptr_adv(p))
7951 if (*p == '\'')
7952 ++len;
7953 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007954 s = r = alloc(len);
7955 if (r != NULL)
7956 {
7957 if (function)
7958 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007959 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007960 r += 10;
7961 }
7962 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007963 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007964 if (str != NULL)
7965 for (p = str; *p != NUL; )
7966 {
7967 if (*p == '\'')
7968 *r++ = '\'';
7969 MB_COPY_CHAR(p, r);
7970 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007971 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007972 if (function)
7973 *r++ = ')';
7974 *r++ = NUL;
7975 }
7976 return s;
7977}
7978
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007979#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007980/*
7981 * Convert the string "text" to a floating point number.
7982 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7983 * this always uses a decimal point.
7984 * Returns the length of the text that was consumed.
7985 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007986 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007987string2float(
7988 char_u *text,
7989 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007990{
7991 char *s = (char *)text;
7992 float_T f;
7993
7994 f = strtod(s, &s);
7995 *value = f;
7996 return (int)((char_u *)s - text);
7997}
7998#endif
7999
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 * Get the value of an environment variable.
8002 * "arg" is pointing to the '$'. It is advanced to after the name.
8003 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008004 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 */
8006 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008007get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008{
8009 char_u *string = NULL;
8010 int len;
8011 int cc;
8012 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008013 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014
8015 ++*arg;
8016 name = *arg;
8017 len = get_env_len(arg);
8018 if (evaluate)
8019 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008020 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008021 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008022
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008023 cc = name[len];
8024 name[len] = NUL;
8025 /* first try vim_getenv(), fast for normal environment vars */
8026 string = vim_getenv(name, &mustfree);
8027 if (string != NULL && *string != NUL)
8028 {
8029 if (!mustfree)
8030 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008032 else
8033 {
8034 if (mustfree)
8035 vim_free(string);
8036
8037 /* next try expanding things like $VIM and ${HOME} */
8038 string = expand_env_save(name - 1);
8039 if (string != NULL && *string == '$')
8040 {
8041 vim_free(string);
8042 string = NULL;
8043 }
8044 }
8045 name[len] = cc;
8046
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008047 rettv->v_type = VAR_STRING;
8048 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 }
8050
8051 return OK;
8052}
8053
8054/*
8055 * Array with names and number of arguments of all internal functions
8056 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8057 */
8058static struct fst
8059{
8060 char *f_name; /* function name */
8061 char f_min_argc; /* minimal number of arguments */
8062 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008063 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008064 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065} functions[] =
8066{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008067#ifdef FEAT_FLOAT
8068 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008069 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008070#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008071 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008072 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008073 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 {"append", 2, 2, f_append},
8075 {"argc", 0, 0, f_argc},
8076 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008077 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008078 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008079#ifdef FEAT_FLOAT
8080 {"asin", 1, 1, f_asin}, /* WJMc */
8081#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008082 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008083 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008084 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008085 {"assert_false", 1, 2, f_assert_false},
8086 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008087#ifdef FEAT_FLOAT
8088 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008089 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008092 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {"bufexists", 1, 1, f_bufexists},
8094 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8095 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8096 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8097 {"buflisted", 1, 1, f_buflisted},
8098 {"bufloaded", 1, 1, f_bufloaded},
8099 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008100 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"bufwinnr", 1, 1, f_bufwinnr},
8102 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008103 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008104 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008105 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008106#ifdef FEAT_FLOAT
8107 {"ceil", 1, 1, f_ceil},
8108#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008109#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008110 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008111 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8112 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008113 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008114 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008115 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008116 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008117 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008118 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008119 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008120 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8121 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008122 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008123 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008124#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008125 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008126 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008128 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008130#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008131 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008132 {"complete_add", 1, 1, f_complete_add},
8133 {"complete_check", 0, 0, f_complete_check},
8134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008136 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008137#ifdef FEAT_FLOAT
8138 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008139 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008140#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008141 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008143 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008144 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008145 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008147 {"diff_filler", 1, 1, f_diff_filler},
8148 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008149 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008150 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008152 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 {"eventhandler", 0, 0, f_eventhandler},
8154 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008155 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008157#ifdef FEAT_FLOAT
8158 {"exp", 1, 1, f_exp},
8159#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008160 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008161 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008162 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8164 {"filereadable", 1, 1, f_filereadable},
8165 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008166 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008167 {"finddir", 1, 3, f_finddir},
8168 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008169#ifdef FEAT_FLOAT
8170 {"float2nr", 1, 1, f_float2nr},
8171 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008172 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008173#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008174 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {"fnamemodify", 2, 2, f_fnamemodify},
8176 {"foldclosed", 1, 1, f_foldclosed},
8177 {"foldclosedend", 1, 1, f_foldclosedend},
8178 {"foldlevel", 1, 1, f_foldlevel},
8179 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008180 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008182 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008183 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008184 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008185 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008186 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 {"getchar", 0, 1, f_getchar},
8188 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008189 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {"getcmdline", 0, 0, f_getcmdline},
8191 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008192 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008193 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008194 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008195 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008196 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008197 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {"getfsize", 1, 1, f_getfsize},
8199 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008200 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008201 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008202 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008203 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008204 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008205 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008206 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008207 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008209 {"gettabvar", 2, 3, f_gettabvar},
8210 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 {"getwinposx", 0, 0, f_getwinposx},
8212 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008213 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008214 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008215 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008216 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008218 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008219 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008220 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8222 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8223 {"histadd", 2, 2, f_histadd},
8224 {"histdel", 1, 2, f_histdel},
8225 {"histget", 1, 2, f_histget},
8226 {"histnr", 1, 1, f_histnr},
8227 {"hlID", 1, 1, f_hlID},
8228 {"hlexists", 1, 1, f_hlexists},
8229 {"hostname", 0, 0, f_hostname},
8230 {"iconv", 3, 3, f_iconv},
8231 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008232 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008233 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008235 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 {"inputrestore", 0, 0, f_inputrestore},
8237 {"inputsave", 0, 0, f_inputsave},
8238 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008239 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008240 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008242 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008243#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8244 {"isnan", 1, 1, f_isnan},
8245#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008246 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008247#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008248 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008249 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008250 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008251 {"job_start", 1, 2, f_job_start},
8252 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008253 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008254#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008255 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008256 {"js_decode", 1, 1, f_js_decode},
8257 {"js_encode", 1, 1, f_js_encode},
8258 {"json_decode", 1, 1, f_json_decode},
8259 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008260 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008262 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {"libcall", 3, 3, f_libcall},
8264 {"libcallnr", 3, 3, f_libcallnr},
8265 {"line", 1, 1, f_line},
8266 {"line2byte", 1, 1, f_line2byte},
8267 {"lispindent", 1, 1, f_lispindent},
8268 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008269#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008270 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008271 {"log10", 1, 1, f_log10},
8272#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008273#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008274 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008275#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008276 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008277 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008278 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008279 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008280 {"matchadd", 2, 5, f_matchadd},
8281 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008282 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008283 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008284 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008285 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008286 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008287 {"max", 1, 1, f_max},
8288 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008289#ifdef vim_mkdir
8290 {"mkdir", 1, 3, f_mkdir},
8291#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008292 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008293#ifdef FEAT_MZSCHEME
8294 {"mzeval", 1, 1, f_mzeval},
8295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008297 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008298 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008299 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008300#ifdef FEAT_PERL
8301 {"perleval", 1, 1, f_perleval},
8302#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008303#ifdef FEAT_FLOAT
8304 {"pow", 2, 2, f_pow},
8305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008307 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008308 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008309#ifdef FEAT_PYTHON3
8310 {"py3eval", 1, 1, f_py3eval},
8311#endif
8312#ifdef FEAT_PYTHON
8313 {"pyeval", 1, 1, f_pyeval},
8314#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008315 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008316 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008317 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008318#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008319 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008320#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008321 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 {"remote_expr", 2, 3, f_remote_expr},
8323 {"remote_foreground", 1, 1, f_remote_foreground},
8324 {"remote_peek", 1, 2, f_remote_peek},
8325 {"remote_read", 1, 1, f_remote_read},
8326 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008327 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008329 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008331 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008332#ifdef FEAT_FLOAT
8333 {"round", 1, 1, f_round},
8334#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008335 {"screenattr", 2, 2, f_screenattr},
8336 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008337 {"screencol", 0, 0, f_screencol},
8338 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008339 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008340 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008341 {"searchpair", 3, 7, f_searchpair},
8342 {"searchpairpos", 3, 7, f_searchpairpos},
8343 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 {"server2client", 2, 2, f_server2client},
8345 {"serverlist", 0, 0, f_serverlist},
8346 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008347 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008349 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008351 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008352 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008353 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008354 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008356 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008357 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008359#ifdef FEAT_CRYPT
8360 {"sha256", 1, 1, f_sha256},
8361#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008362 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008363 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008365#ifdef FEAT_FLOAT
8366 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008367 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008368#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008369 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008370 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008371 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008372 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008373 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008374#ifdef FEAT_FLOAT
8375 {"sqrt", 1, 1, f_sqrt},
8376 {"str2float", 1, 1, f_str2float},
8377#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008378 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008379 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008380 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381#ifdef HAVE_STRFTIME
8382 {"strftime", 1, 2, f_strftime},
8383#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008384 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008385 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 {"strlen", 1, 1, f_strlen},
8387 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008388 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008390 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008391 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 {"substitute", 4, 4, f_substitute},
8393 {"synID", 3, 3, f_synID},
8394 {"synIDattr", 2, 3, f_synIDattr},
8395 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008396 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008397 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008398 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008399 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008400 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008401 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008402 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008403 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008404 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008405#ifdef FEAT_FLOAT
8406 {"tan", 1, 1, f_tan},
8407 {"tanh", 1, 1, f_tanh},
8408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008410 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008411#ifdef FEAT_TIMERS
8412 {"timer_start", 2, 3, f_timer_start},
8413 {"timer_stop", 1, 1, f_timer_stop},
8414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 {"tolower", 1, 1, f_tolower},
8416 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008417 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008418#ifdef FEAT_FLOAT
8419 {"trunc", 1, 1, f_trunc},
8420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008422 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008423 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008424 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008425 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 {"virtcol", 1, 1, f_virtcol},
8427 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008428 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008429 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008430 {"win_getid", 0, 2, f_win_getid},
8431 {"win_gotoid", 1, 1, f_win_gotoid},
8432 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8433 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 {"winbufnr", 1, 1, f_winbufnr},
8435 {"wincol", 0, 0, f_wincol},
8436 {"winheight", 1, 1, f_winheight},
8437 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008438 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008440 {"winrestview", 1, 1, f_winrestview},
8441 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008443 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008444 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008445 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446};
8447
8448#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8449
8450/*
8451 * Function given to ExpandGeneric() to obtain the list of internal
8452 * or user defined function names.
8453 */
8454 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008455get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456{
8457 static int intidx = -1;
8458 char_u *name;
8459
8460 if (idx == 0)
8461 intidx = -1;
8462 if (intidx < 0)
8463 {
8464 name = get_user_func_name(xp, idx);
8465 if (name != NULL)
8466 return name;
8467 }
8468 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8469 {
8470 STRCPY(IObuff, functions[intidx].f_name);
8471 STRCAT(IObuff, "(");
8472 if (functions[intidx].f_max_argc == 0)
8473 STRCAT(IObuff, ")");
8474 return IObuff;
8475 }
8476
8477 return NULL;
8478}
8479
8480/*
8481 * Function given to ExpandGeneric() to obtain the list of internal or
8482 * user defined variable or function names.
8483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008485get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486{
8487 static int intidx = -1;
8488 char_u *name;
8489
8490 if (idx == 0)
8491 intidx = -1;
8492 if (intidx < 0)
8493 {
8494 name = get_function_name(xp, idx);
8495 if (name != NULL)
8496 return name;
8497 }
8498 return get_user_var_name(xp, ++intidx);
8499}
8500
8501#endif /* FEAT_CMDL_COMPL */
8502
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008503#if defined(EBCDIC) || defined(PROTO)
8504/*
8505 * Compare struct fst by function name.
8506 */
8507 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008508compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008509{
8510 struct fst *p1 = (struct fst *)s1;
8511 struct fst *p2 = (struct fst *)s2;
8512
8513 return STRCMP(p1->f_name, p2->f_name);
8514}
8515
8516/*
8517 * Sort the function table by function name.
8518 * The sorting of the table above is ASCII dependant.
8519 * On machines using EBCDIC we have to sort it.
8520 */
8521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008522sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008523{
8524 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8525
8526 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8527}
8528#endif
8529
8530
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531/*
8532 * Find internal function in table above.
8533 * Return index, or -1 if not found
8534 */
8535 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008536find_internal_func(
8537 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538{
8539 int first = 0;
8540 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8541 int cmp;
8542 int x;
8543
8544 /*
8545 * Find the function name in the table. Binary search.
8546 */
8547 while (first <= last)
8548 {
8549 x = first + ((unsigned)(last - first) >> 1);
8550 cmp = STRCMP(name, functions[x].f_name);
8551 if (cmp < 0)
8552 last = x - 1;
8553 else if (cmp > 0)
8554 first = x + 1;
8555 else
8556 return x;
8557 }
8558 return -1;
8559}
8560
8561/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008562 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8563 * name it contains, otherwise return "name".
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008564 * If "name" is of type VAR_PARTIAL also return "partial"
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008565 */
8566 static char_u *
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008567deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008568{
Bram Moolenaar33570922005-01-25 22:26:29 +00008569 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008570 int cc;
8571
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008572 *partial = NULL;
8573
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008574 cc = name[*lenp];
8575 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008576 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008577 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008578 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008579 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008580 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008581 {
8582 *lenp = 0;
8583 return (char_u *)""; /* just in case */
8584 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008585 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008586 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008587 }
8588
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008589 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8590 {
8591 *partial = v->di_tv.vval.v_partial;
8592 if (*partial == NULL)
8593 {
8594 *lenp = 0;
8595 return (char_u *)""; /* just in case */
8596 }
8597 *lenp = (int)STRLEN((*partial)->pt_name);
8598 return (*partial)->pt_name;
8599 }
8600
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008601 return name;
8602}
8603
8604/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 * Allocate a variable for the result of a function.
8606 * Return OK or FAIL.
8607 */
8608 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008609get_func_tv(
8610 char_u *name, /* name of the function */
8611 int len, /* length of "name" */
8612 typval_T *rettv,
8613 char_u **arg, /* argument, pointing to the '(' */
8614 linenr_T firstline, /* first line of range */
8615 linenr_T lastline, /* last line of range */
8616 int *doesrange, /* return: function handled range */
8617 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008618 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008619 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620{
8621 char_u *argp;
8622 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008623 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 int argcount = 0; /* number of arguments found */
8625
8626 /*
8627 * Get the arguments.
8628 */
8629 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008630 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 {
8632 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8633 if (*argp == ')' || *argp == ',' || *argp == NUL)
8634 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8636 {
8637 ret = FAIL;
8638 break;
8639 }
8640 ++argcount;
8641 if (*argp != ',')
8642 break;
8643 }
8644 if (*argp == ')')
8645 ++argp;
8646 else
8647 ret = FAIL;
8648
8649 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008650 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008651 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008653 {
8654 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008655 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008656 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008657 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659
8660 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008661 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662
8663 *arg = skipwhite(argp);
8664 return ret;
8665}
8666
8667
8668/*
8669 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008670 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008671 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008673 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008674call_func(
8675 char_u *funcname, /* name of the function */
8676 int len, /* length of "name" */
8677 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008678 int argcount_in, /* number of "argvars" */
8679 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008680 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008681 linenr_T firstline, /* first line of range */
8682 linenr_T lastline, /* last line of range */
8683 int *doesrange, /* return: function handled range */
8684 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008685 partial_T *partial, /* optional, can be NULL */
8686 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687{
8688 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689#define ERROR_UNKNOWN 0
8690#define ERROR_TOOMANY 1
8691#define ERROR_TOOFEW 2
8692#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008693#define ERROR_DICT 4
8694#define ERROR_NONE 5
8695#define ERROR_OTHER 6
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008696#define ERROR_BOTH 7
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 int error = ERROR_NONE;
8698 int i;
8699 int llen;
8700 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701#define FLEN_FIXED 40
8702 char_u fname_buf[FLEN_FIXED + 1];
8703 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008704 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008705 int argcount = argcount_in;
8706 typval_T *argvars = argvars_in;
8707 dict_T *selfdict = selfdict_in;
8708 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
8709 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008710
8711 /* Make a copy of the name, if it comes from a funcref variable it could
8712 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008713 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008714 if (name == NULL)
8715 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716
8717 /*
8718 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8719 * Change <SNR>123_name() to K_SNR 123_name().
8720 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 llen = eval_fname_script(name);
8723 if (llen > 0)
8724 {
8725 fname_buf[0] = K_SPECIAL;
8726 fname_buf[1] = KS_EXTRA;
8727 fname_buf[2] = (int)KE_SNR;
8728 i = 3;
8729 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8730 {
8731 if (current_SID <= 0)
8732 error = ERROR_SCRIPT;
8733 else
8734 {
8735 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8736 i = (int)STRLEN(fname_buf);
8737 }
8738 }
8739 if (i + STRLEN(name + llen) < FLEN_FIXED)
8740 {
8741 STRCPY(fname_buf + i, name + llen);
8742 fname = fname_buf;
8743 }
8744 else
8745 {
8746 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8747 if (fname == NULL)
8748 error = ERROR_OTHER;
8749 else
8750 {
8751 mch_memmove(fname, fname_buf, (size_t)i);
8752 STRCPY(fname + i, name + llen);
8753 }
8754 }
8755 }
8756 else
8757 fname = name;
8758
8759 *doesrange = FALSE;
8760
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008761 if (partial != NULL)
8762 {
8763 if (partial->pt_dict != NULL)
8764 {
8765 if (selfdict_in != NULL)
8766 error = ERROR_BOTH;
8767 selfdict = partial->pt_dict;
8768 }
8769 if (error == ERROR_NONE && partial->pt_argc > 0)
8770 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008771 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
8772 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
8773 for (i = 0; i < argcount_in; ++i)
8774 argv[i + argv_clear] = argvars_in[i];
8775 argvars = argv;
8776 argcount = partial->pt_argc + argcount_in;
8777 }
8778 }
8779
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780
8781 /* execute the function if no errors detected and executing */
8782 if (evaluate && error == ERROR_NONE)
8783 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008784 char_u *rfname = fname;
8785
8786 /* Ignore "g:" before a function name. */
8787 if (fname[0] == 'g' && fname[1] == ':')
8788 rfname = fname + 2;
8789
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008790 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8791 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 error = ERROR_UNKNOWN;
8793
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008794 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 {
8796 /*
8797 * User defined function.
8798 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008799 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008800
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008802 /* Trigger FuncUndefined event, may load the function. */
8803 if (fp == NULL
8804 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008805 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008806 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008808 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008809 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 }
8811#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008812 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008813 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008814 {
8815 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008816 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008817 }
8818
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 if (fp != NULL)
8820 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008821 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008823 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008825 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008827 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008828 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 else
8830 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008831 int did_save_redo = FALSE;
8832
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 /*
8834 * Call the user function.
8835 * Save and restore search patterns, script variables and
8836 * redo buffer.
8837 */
8838 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008839#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008840 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008841#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008842 {
8843 saveRedobuff();
8844 did_save_redo = TRUE;
8845 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008846 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008847 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008848 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008849 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8850 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8851 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008852 /* Function was unreferenced while being used, free it
8853 * now. */
8854 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008855 if (did_save_redo)
8856 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 restore_search_patterns();
8858 error = ERROR_NONE;
8859 }
8860 }
8861 }
8862 else
8863 {
8864 /*
8865 * Find the function name in the table, call its implementation.
8866 */
8867 i = find_internal_func(fname);
8868 if (i >= 0)
8869 {
8870 if (argcount < functions[i].f_min_argc)
8871 error = ERROR_TOOFEW;
8872 else if (argcount > functions[i].f_max_argc)
8873 error = ERROR_TOOMANY;
8874 else
8875 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008876 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008877 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 error = ERROR_NONE;
8879 }
8880 }
8881 }
8882 /*
8883 * The function call (or "FuncUndefined" autocommand sequence) might
8884 * have been aborted by an error, an interrupt, or an explicitly thrown
8885 * exception that has not been caught so far. This situation can be
8886 * tested for by calling aborting(). For an error in an internal
8887 * function or for the "E132" error in call_user_func(), however, the
8888 * throw point at which the "force_abort" flag (temporarily reset by
8889 * emsg()) is normally updated has not been reached yet. We need to
8890 * update that flag first to make aborting() reliable.
8891 */
8892 update_force_abort();
8893 }
8894 if (error == ERROR_NONE)
8895 ret = OK;
8896
8897 /*
8898 * Report an error unless the argument evaluation or function call has been
8899 * cancelled due to an aborting error, an interrupt, or an exception.
8900 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008901 if (!aborting())
8902 {
8903 switch (error)
8904 {
8905 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008906 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008907 break;
8908 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008909 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008910 break;
8911 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008912 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008913 name);
8914 break;
8915 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008916 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008917 name);
8918 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008919 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008920 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008921 name);
8922 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008923 case ERROR_BOTH:
Bram Moolenaarab1fa392016-03-15 19:33:34 +01008924 emsg_funcname(e_dict_both, name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008925 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008926 }
8927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008929 while (argv_clear > 0)
8930 clear_tv(&argv[--argv_clear]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 if (fname != name && fname != fname_buf)
8932 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008933 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934
8935 return ret;
8936}
8937
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008938/*
8939 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008940 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008941 */
8942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008943emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008944{
8945 char_u *p;
8946
8947 if (*name == K_SPECIAL)
8948 p = concat_str((char_u *)"<SNR>", name + 3);
8949 else
8950 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008951 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008952 if (p != name)
8953 vim_free(p);
8954}
8955
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008956/*
8957 * Return TRUE for a non-zero Number and a non-empty String.
8958 */
8959 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008960non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008961{
8962 return ((argvars[0].v_type == VAR_NUMBER
8963 && argvars[0].vval.v_number != 0)
8964 || (argvars[0].v_type == VAR_STRING
8965 && argvars[0].vval.v_string != NULL
8966 && *argvars[0].vval.v_string != NUL));
8967}
8968
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969/*********************************************
8970 * Implementation of the built-in functions
8971 */
8972
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008973#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008974static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008975
8976/*
8977 * Get the float value of "argvars[0]" into "f".
8978 * Returns FAIL when the argument is not a Number or Float.
8979 */
8980 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008981get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008982{
8983 if (argvars[0].v_type == VAR_FLOAT)
8984 {
8985 *f = argvars[0].vval.v_float;
8986 return OK;
8987 }
8988 if (argvars[0].v_type == VAR_NUMBER)
8989 {
8990 *f = (float_T)argvars[0].vval.v_number;
8991 return OK;
8992 }
8993 EMSG(_("E808: Number or Float required"));
8994 return FAIL;
8995}
8996
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008997/*
8998 * "abs(expr)" function
8999 */
9000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009001f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009002{
9003 if (argvars[0].v_type == VAR_FLOAT)
9004 {
9005 rettv->v_type = VAR_FLOAT;
9006 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9007 }
9008 else
9009 {
9010 varnumber_T n;
9011 int error = FALSE;
9012
9013 n = get_tv_number_chk(&argvars[0], &error);
9014 if (error)
9015 rettv->vval.v_number = -1;
9016 else if (n > 0)
9017 rettv->vval.v_number = n;
9018 else
9019 rettv->vval.v_number = -n;
9020 }
9021}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009022
9023/*
9024 * "acos()" function
9025 */
9026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009027f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009028{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009029 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009030
9031 rettv->v_type = VAR_FLOAT;
9032 if (get_float_arg(argvars, &f) == OK)
9033 rettv->vval.v_float = acos(f);
9034 else
9035 rettv->vval.v_float = 0.0;
9036}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009037#endif
9038
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009040 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 */
9042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009043f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044{
Bram Moolenaar33570922005-01-25 22:26:29 +00009045 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009047 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009048 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009050 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009051 && !tv_check_lock(l->lv_lock,
9052 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009053 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009054 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009055 }
9056 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009057 EMSG(_(e_listreq));
9058}
9059
9060/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009061 * "alloc_fail(id, countdown, repeat)" function
9062 */
9063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009064f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009065{
9066 if (argvars[0].v_type != VAR_NUMBER
9067 || argvars[0].vval.v_number <= 0
9068 || argvars[1].v_type != VAR_NUMBER
9069 || argvars[1].vval.v_number < 0
9070 || argvars[2].v_type != VAR_NUMBER)
9071 EMSG(_(e_invarg));
9072 else
9073 {
9074 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009075 if (alloc_fail_id >= aid_last)
9076 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009077 alloc_fail_countdown = argvars[1].vval.v_number;
9078 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009079 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009080 }
9081}
9082
9083/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009084 * "and(expr, expr)" function
9085 */
9086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009087f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009088{
9089 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9090 & get_tv_number_chk(&argvars[1], NULL);
9091}
9092
9093/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009094 * "append(lnum, string/list)" function
9095 */
9096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009097f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009098{
9099 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009100 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009101 list_T *l = NULL;
9102 listitem_T *li = NULL;
9103 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009104 long added = 0;
9105
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009106 /* When coming here from Insert mode, sync undo, so that this can be
9107 * undone separately from what was previously inserted. */
9108 if (u_sync_once == 2)
9109 {
9110 u_sync_once = 1; /* notify that u_sync() was called */
9111 u_sync(TRUE);
9112 }
9113
Bram Moolenaar0d660222005-01-07 21:51:51 +00009114 lnum = get_tv_lnum(argvars);
9115 if (lnum >= 0
9116 && lnum <= curbuf->b_ml.ml_line_count
9117 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009118 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009119 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009120 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009121 l = argvars[1].vval.v_list;
9122 if (l == NULL)
9123 return;
9124 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009125 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009126 for (;;)
9127 {
9128 if (l == NULL)
9129 tv = &argvars[1]; /* append a string */
9130 else if (li == NULL)
9131 break; /* end of list */
9132 else
9133 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009134 line = get_tv_string_chk(tv);
9135 if (line == NULL) /* type error */
9136 {
9137 rettv->vval.v_number = 1; /* Failed */
9138 break;
9139 }
9140 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009141 ++added;
9142 if (l == NULL)
9143 break;
9144 li = li->li_next;
9145 }
9146
9147 appended_lines_mark(lnum, added);
9148 if (curwin->w_cursor.lnum > lnum)
9149 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009151 else
9152 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153}
9154
9155/*
9156 * "argc()" function
9157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009159f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009161 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162}
9163
9164/*
9165 * "argidx()" function
9166 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009168f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009170 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171}
9172
9173/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009174 * "arglistid()" function
9175 */
9176 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009177f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009178{
9179 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009180
9181 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009182 wp = find_tabwin(&argvars[0], &argvars[1]);
9183 if (wp != NULL)
9184 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009185}
9186
9187/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 * "argv(nr)" function
9189 */
9190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009191f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192{
9193 int idx;
9194
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009195 if (argvars[0].v_type != VAR_UNKNOWN)
9196 {
9197 idx = get_tv_number_chk(&argvars[0], NULL);
9198 if (idx >= 0 && idx < ARGCOUNT)
9199 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9200 else
9201 rettv->vval.v_string = NULL;
9202 rettv->v_type = VAR_STRING;
9203 }
9204 else if (rettv_list_alloc(rettv) == OK)
9205 for (idx = 0; idx < ARGCOUNT; ++idx)
9206 list_append_string(rettv->vval.v_list,
9207 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208}
9209
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009210static void prepare_assert_error(garray_T*gap);
9211static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9212static void assert_error(garray_T *gap);
9213static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009214
9215/*
9216 * Prepare "gap" for an assert error and add the sourcing position.
9217 */
9218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009219prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009220{
9221 char buf[NUMBUFLEN];
9222
9223 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009224 if (sourcing_name != NULL)
9225 {
9226 ga_concat(gap, sourcing_name);
9227 if (sourcing_lnum > 0)
9228 ga_concat(gap, (char_u *)" ");
9229 }
9230 if (sourcing_lnum > 0)
9231 {
9232 sprintf(buf, "line %ld", (long)sourcing_lnum);
9233 ga_concat(gap, (char_u *)buf);
9234 }
9235 if (sourcing_name != NULL || sourcing_lnum > 0)
9236 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009237}
9238
9239/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009240 * Append "str" to "gap", escaping unprintable characters.
9241 * Changes NL to \n, CR to \r, etc.
9242 */
9243 static void
9244ga_concat_esc(garray_T *gap, char_u *str)
9245{
9246 char_u *p;
9247 char_u buf[NUMBUFLEN];
9248
Bram Moolenaarf1551962016-03-15 12:55:58 +01009249 if (str == NULL)
9250 {
9251 ga_concat(gap, (char_u *)"NULL");
9252 return;
9253 }
9254
Bram Moolenaar23689172016-02-15 22:37:37 +01009255 for (p = str; *p != NUL; ++p)
9256 switch (*p)
9257 {
9258 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9259 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9260 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9261 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9262 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9263 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9264 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9265 default:
9266 if (*p < ' ')
9267 {
9268 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9269 ga_concat(gap, buf);
9270 }
9271 else
9272 ga_append(gap, *p);
9273 break;
9274 }
9275}
9276
9277/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009278 * Fill "gap" with information about an assert error.
9279 */
9280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009281fill_assert_error(
9282 garray_T *gap,
9283 typval_T *opt_msg_tv,
9284 char_u *exp_str,
9285 typval_T *exp_tv,
9286 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009287{
9288 char_u numbuf[NUMBUFLEN];
9289 char_u *tofree;
9290
9291 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9292 {
9293 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9294 vim_free(tofree);
9295 }
9296 else
9297 {
9298 ga_concat(gap, (char_u *)"Expected ");
9299 if (exp_str == NULL)
9300 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009301 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009302 vim_free(tofree);
9303 }
9304 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009305 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009306 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009307 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009308 vim_free(tofree);
9309 }
9310}
Bram Moolenaar43345542015-11-29 17:35:35 +01009311
9312/*
9313 * Add an assert error to v:errors.
9314 */
9315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009316assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009317{
9318 struct vimvar *vp = &vimvars[VV_ERRORS];
9319
9320 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9321 /* Make sure v:errors is a list. */
9322 set_vim_var_list(VV_ERRORS, list_alloc());
9323 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9324}
9325
Bram Moolenaar43345542015-11-29 17:35:35 +01009326/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009327 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009328 */
9329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009330f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009331{
9332 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009333
9334 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9335 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009336 prepare_assert_error(&ga);
9337 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9338 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009339 ga_clear(&ga);
9340 }
9341}
9342
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009343/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009344 * "assert_exception(string[, msg])" function
9345 */
9346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009347f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009348{
9349 garray_T ga;
9350 char *error;
9351
9352 error = (char *)get_tv_string_chk(&argvars[0]);
9353 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9354 {
9355 prepare_assert_error(&ga);
9356 ga_concat(&ga, (char_u *)"v:exception is not set");
9357 assert_error(&ga);
9358 ga_clear(&ga);
9359 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009360 else if (error != NULL
9361 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009362 {
9363 prepare_assert_error(&ga);
9364 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9365 &vimvars[VV_EXCEPTION].vv_tv);
9366 assert_error(&ga);
9367 ga_clear(&ga);
9368 }
9369}
9370
9371/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009372 * "assert_fails(cmd [, error])" function
9373 */
9374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009375f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009376{
9377 char_u *cmd = get_tv_string_chk(&argvars[0]);
9378 garray_T ga;
9379
9380 called_emsg = FALSE;
9381 suppress_errthrow = TRUE;
9382 emsg_silent = TRUE;
9383 do_cmdline_cmd(cmd);
9384 if (!called_emsg)
9385 {
9386 prepare_assert_error(&ga);
9387 ga_concat(&ga, (char_u *)"command did not fail: ");
9388 ga_concat(&ga, cmd);
9389 assert_error(&ga);
9390 ga_clear(&ga);
9391 }
9392 else if (argvars[1].v_type != VAR_UNKNOWN)
9393 {
9394 char_u buf[NUMBUFLEN];
9395 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9396
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009397 if (error == NULL
9398 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009399 {
9400 prepare_assert_error(&ga);
9401 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9402 &vimvars[VV_ERRMSG].vv_tv);
9403 assert_error(&ga);
9404 ga_clear(&ga);
9405 }
9406 }
9407
9408 called_emsg = FALSE;
9409 suppress_errthrow = FALSE;
9410 emsg_silent = FALSE;
9411 emsg_on_display = FALSE;
9412 set_vim_var_string(VV_ERRMSG, NULL, 0);
9413}
9414
9415/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009416 * Common for assert_true() and assert_false().
9417 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009419assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009420{
9421 int error = FALSE;
9422 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009423
Bram Moolenaar37127922016-02-06 20:29:28 +01009424 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009425 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009426 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009427 if (argvars[0].v_type != VAR_NUMBER
9428 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9429 || error)
9430 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009431 prepare_assert_error(&ga);
9432 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009433 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009434 NULL, &argvars[0]);
9435 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009436 ga_clear(&ga);
9437 }
9438}
9439
9440/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009441 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009442 */
9443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009444f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009445{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009446 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009447}
9448
9449/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009450 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009451 */
9452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009453f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009454{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009455 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009456}
9457
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009458#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009459/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009460 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009461 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009463f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009464{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009465 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009466
9467 rettv->v_type = VAR_FLOAT;
9468 if (get_float_arg(argvars, &f) == OK)
9469 rettv->vval.v_float = asin(f);
9470 else
9471 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009472}
9473
9474/*
9475 * "atan()" function
9476 */
9477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009478f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009479{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009480 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009481
9482 rettv->v_type = VAR_FLOAT;
9483 if (get_float_arg(argvars, &f) == OK)
9484 rettv->vval.v_float = atan(f);
9485 else
9486 rettv->vval.v_float = 0.0;
9487}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009488
9489/*
9490 * "atan2()" function
9491 */
9492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009493f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009494{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009495 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009496
9497 rettv->v_type = VAR_FLOAT;
9498 if (get_float_arg(argvars, &fx) == OK
9499 && get_float_arg(&argvars[1], &fy) == OK)
9500 rettv->vval.v_float = atan2(fx, fy);
9501 else
9502 rettv->vval.v_float = 0.0;
9503}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009504#endif
9505
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506/*
9507 * "browse(save, title, initdir, default)" function
9508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009510f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511{
9512#ifdef FEAT_BROWSE
9513 int save;
9514 char_u *title;
9515 char_u *initdir;
9516 char_u *defname;
9517 char_u buf[NUMBUFLEN];
9518 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009519 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009521 save = get_tv_number_chk(&argvars[0], &error);
9522 title = get_tv_string_chk(&argvars[1]);
9523 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9524 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009526 if (error || title == NULL || initdir == NULL || defname == NULL)
9527 rettv->vval.v_string = NULL;
9528 else
9529 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009530 do_browse(save ? BROWSE_SAVE : 0,
9531 title, defname, NULL, initdir, NULL, curbuf);
9532#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009533 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009534#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009535 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009536}
9537
9538/*
9539 * "browsedir(title, initdir)" function
9540 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009542f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009543{
9544#ifdef FEAT_BROWSE
9545 char_u *title;
9546 char_u *initdir;
9547 char_u buf[NUMBUFLEN];
9548
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009549 title = get_tv_string_chk(&argvars[0]);
9550 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009551
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009552 if (title == NULL || initdir == NULL)
9553 rettv->vval.v_string = NULL;
9554 else
9555 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009556 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009558 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009560 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561}
9562
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009563static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009564
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565/*
9566 * Find a buffer by number or exact name.
9567 */
9568 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009569find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570{
9571 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009573 if (avar->v_type == VAR_NUMBER)
9574 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009575 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009577 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009578 if (buf == NULL)
9579 {
9580 /* No full path name match, try a match with a URL or a "nofile"
9581 * buffer, these don't use the full path. */
9582 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9583 if (buf->b_fname != NULL
9584 && (path_with_url(buf->b_fname)
9585#ifdef FEAT_QUICKFIX
9586 || bt_nofile(buf)
9587#endif
9588 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009589 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009590 break;
9591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 }
9593 return buf;
9594}
9595
9596/*
9597 * "bufexists(expr)" function
9598 */
9599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009600f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009602 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603}
9604
9605/*
9606 * "buflisted(expr)" function
9607 */
9608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009609f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610{
9611 buf_T *buf;
9612
9613 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009614 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615}
9616
9617/*
9618 * "bufloaded(expr)" function
9619 */
9620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009621f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622{
9623 buf_T *buf;
9624
9625 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009626 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627}
9628
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009629 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009630buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 int save_magic;
9633 char_u *save_cpo;
9634 buf_T *buf;
9635
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9637 save_magic = p_magic;
9638 p_magic = TRUE;
9639 save_cpo = p_cpo;
9640 p_cpo = (char_u *)"";
9641
9642 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009643 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644
9645 p_magic = save_magic;
9646 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009647 return buf;
9648}
9649
9650/*
9651 * Get buffer by number or pattern.
9652 */
9653 static buf_T *
9654get_buf_tv(typval_T *tv, int curtab_only)
9655{
9656 char_u *name = tv->vval.v_string;
9657 buf_T *buf;
9658
9659 if (tv->v_type == VAR_NUMBER)
9660 return buflist_findnr((int)tv->vval.v_number);
9661 if (tv->v_type != VAR_STRING)
9662 return NULL;
9663 if (name == NULL || *name == NUL)
9664 return curbuf;
9665 if (name[0] == '$' && name[1] == NUL)
9666 return lastbuf;
9667
9668 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669
9670 /* If not found, try expanding the name, like done for bufexists(). */
9671 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009672 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673
9674 return buf;
9675}
9676
9677/*
9678 * "bufname(expr)" function
9679 */
9680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009681f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682{
9683 buf_T *buf;
9684
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009685 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009687 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009688 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009690 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009692 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 --emsg_off;
9694}
9695
9696/*
9697 * "bufnr(expr)" function
9698 */
9699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009700f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701{
9702 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009703 int error = FALSE;
9704 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009706 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009708 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009709 --emsg_off;
9710
9711 /* If the buffer isn't found and the second argument is not zero create a
9712 * new buffer. */
9713 if (buf == NULL
9714 && argvars[1].v_type != VAR_UNKNOWN
9715 && get_tv_number_chk(&argvars[1], &error) != 0
9716 && !error
9717 && (name = get_tv_string_chk(&argvars[0])) != NULL
9718 && !error)
9719 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9720
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009722 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009724 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725}
9726
9727/*
9728 * "bufwinnr(nr)" function
9729 */
9730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009731f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732{
9733#ifdef FEAT_WINDOWS
9734 win_T *wp;
9735 int winnr = 0;
9736#endif
9737 buf_T *buf;
9738
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009739 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009741 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742#ifdef FEAT_WINDOWS
9743 for (wp = firstwin; wp; wp = wp->w_next)
9744 {
9745 ++winnr;
9746 if (wp->w_buffer == buf)
9747 break;
9748 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009749 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009751 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752#endif
9753 --emsg_off;
9754}
9755
9756/*
9757 * "byte2line(byte)" function
9758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009760f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761{
9762#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009763 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764#else
9765 long boff = 0;
9766
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009767 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009769 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009771 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 (linenr_T)0, &boff);
9773#endif
9774}
9775
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009777byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009778{
9779#ifdef FEAT_MBYTE
9780 char_u *t;
9781#endif
9782 char_u *str;
9783 long idx;
9784
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009785 str = get_tv_string_chk(&argvars[0]);
9786 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009787 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009788 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009789 return;
9790
9791#ifdef FEAT_MBYTE
9792 t = str;
9793 for ( ; idx > 0; idx--)
9794 {
9795 if (*t == NUL) /* EOL reached */
9796 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009797 if (enc_utf8 && comp)
9798 t += utf_ptr2len(t);
9799 else
9800 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009801 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009802 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009803#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009804 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009805 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009806#endif
9807}
9808
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009809/*
9810 * "byteidx()" function
9811 */
9812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009813f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009814{
9815 byteidx(argvars, rettv, FALSE);
9816}
9817
9818/*
9819 * "byteidxcomp()" function
9820 */
9821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009822f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009823{
9824 byteidx(argvars, rettv, TRUE);
9825}
9826
Bram Moolenaardb913952012-06-29 12:54:53 +02009827 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009828func_call(
9829 char_u *name,
9830 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009831 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009832 dict_T *selfdict,
9833 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009834{
9835 listitem_T *item;
9836 typval_T argv[MAX_FUNC_ARGS + 1];
9837 int argc = 0;
9838 int dummy;
9839 int r = 0;
9840
9841 for (item = args->vval.v_list->lv_first; item != NULL;
9842 item = item->li_next)
9843 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009844 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +02009845 {
9846 EMSG(_("E699: Too many arguments"));
9847 break;
9848 }
9849 /* Make a copy of each argument. This is needed to be able to set
9850 * v_lock to VAR_FIXED in the copy without changing the original list.
9851 */
9852 copy_tv(&item->li_tv, &argv[argc++]);
9853 }
9854
9855 if (item == NULL)
9856 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9857 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009858 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +02009859
9860 /* Free the arguments. */
9861 while (argc > 0)
9862 clear_tv(&argv[--argc]);
9863
9864 return r;
9865}
9866
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009867/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009868 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009869 */
9870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009871f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009872{
9873 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009874 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00009875 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009876
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009877 if (argvars[1].v_type != VAR_LIST)
9878 {
9879 EMSG(_(e_listreq));
9880 return;
9881 }
9882 if (argvars[1].vval.v_list == NULL)
9883 return;
9884
9885 if (argvars[0].v_type == VAR_FUNC)
9886 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009887 else if (argvars[0].v_type == VAR_PARTIAL)
9888 {
9889 partial = argvars[0].vval.v_partial;
9890 func = partial->pt_name;
9891 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009892 else
9893 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009894 if (*func == NUL)
9895 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009896
Bram Moolenaare9a41262005-01-15 22:18:47 +00009897 if (argvars[2].v_type != VAR_UNKNOWN)
9898 {
9899 if (argvars[2].v_type != VAR_DICT)
9900 {
9901 EMSG(_(e_dictreq));
9902 return;
9903 }
9904 selfdict = argvars[2].vval.v_dict;
9905 }
9906
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009907 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009908}
9909
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009910#ifdef FEAT_FLOAT
9911/*
9912 * "ceil({float})" function
9913 */
9914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009915f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009916{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009917 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009918
9919 rettv->v_type = VAR_FLOAT;
9920 if (get_float_arg(argvars, &f) == OK)
9921 rettv->vval.v_float = ceil(f);
9922 else
9923 rettv->vval.v_float = 0.0;
9924}
9925#endif
9926
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01009927#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009928/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009929 * "ch_close()" function
9930 */
9931 static void
9932f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9933{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009934 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009935
9936 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +01009937 {
Bram Moolenaar8b374212016-02-24 20:43:06 +01009938 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +01009939 channel_clear(channel);
9940 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009941}
9942
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01009943/*
9944 * "ch_getbufnr()" function
9945 */
9946 static void
9947f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
9948{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009949 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01009950
9951 rettv->vval.v_number = -1;
9952 if (channel != NULL)
9953 {
9954 char_u *what = get_tv_string(&argvars[1]);
9955 int part;
9956
9957 if (STRCMP(what, "err") == 0)
9958 part = PART_ERR;
9959 else if (STRCMP(what, "out") == 0)
9960 part = PART_OUT;
9961 else if (STRCMP(what, "in") == 0)
9962 part = PART_IN;
9963 else
9964 part = PART_SOCK;
9965 if (channel->ch_part[part].ch_buffer != NULL)
9966 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
9967 }
9968}
9969
Bram Moolenaar02e83b42016-02-21 20:10:26 +01009970/*
9971 * "ch_getjob()" function
9972 */
9973 static void
9974f_ch_getjob(typval_T *argvars, typval_T *rettv)
9975{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009976 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +01009977
9978 if (channel != NULL)
9979 {
9980 rettv->v_type = VAR_JOB;
9981 rettv->vval.v_job = channel->ch_job;
9982 if (channel->ch_job != NULL)
9983 ++channel->ch_job->jv_refcount;
9984 }
9985}
Bram Moolenaar02e83b42016-02-21 20:10:26 +01009986
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009987/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +01009988 * "ch_log()" function
9989 */
9990 static void
9991f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
9992{
9993 char_u *msg = get_tv_string(&argvars[0]);
9994 channel_T *channel = NULL;
9995
9996 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009997 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +01009998
9999 ch_log(channel, (char *)msg);
10000}
10001
10002/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010003 * "ch_logfile()" function
10004 */
10005 static void
10006f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10007{
10008 char_u *fname;
10009 char_u *opt = (char_u *)"";
10010 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010011
10012 fname = get_tv_string(&argvars[0]);
10013 if (argvars[1].v_type == VAR_STRING)
10014 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010015 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010016}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010017
10018/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010019 * "ch_open()" function
10020 */
10021 static void
10022f_ch_open(typval_T *argvars, typval_T *rettv)
10023{
Bram Moolenaar77073442016-02-13 23:23:53 +010010024 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010025 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010026}
10027
10028/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010029 * "ch_read()" function
10030 */
10031 static void
10032f_ch_read(typval_T *argvars, typval_T *rettv)
10033{
10034 common_channel_read(argvars, rettv, FALSE);
10035}
10036
10037/*
10038 * "ch_readraw()" function
10039 */
10040 static void
10041f_ch_readraw(typval_T *argvars, typval_T *rettv)
10042{
10043 common_channel_read(argvars, rettv, TRUE);
10044}
10045
10046/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010047 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010048 */
10049 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010050f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10051{
10052 ch_expr_common(argvars, rettv, TRUE);
10053}
10054
10055/*
10056 * "ch_sendexpr()" function
10057 */
10058 static void
10059f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10060{
10061 ch_expr_common(argvars, rettv, FALSE);
10062}
10063
10064/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010065 * "ch_evalraw()" function
10066 */
10067 static void
10068f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10069{
10070 ch_raw_common(argvars, rettv, TRUE);
10071}
10072
10073/*
10074 * "ch_sendraw()" function
10075 */
10076 static void
10077f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10078{
10079 ch_raw_common(argvars, rettv, FALSE);
10080}
10081
10082/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010083 * "ch_setoptions()" function
10084 */
10085 static void
10086f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10087{
10088 channel_T *channel;
10089 jobopt_T opt;
10090
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010091 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010092 if (channel == NULL)
10093 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010094 clear_job_options(&opt);
10095 if (get_job_options(&argvars[1], &opt,
10096 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010097 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010098 channel_set_options(channel, &opt);
10099}
10100
10101/*
10102 * "ch_status()" function
10103 */
10104 static void
10105f_ch_status(typval_T *argvars, typval_T *rettv)
10106{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010107 channel_T *channel;
10108
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010109 /* return an empty string by default */
10110 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010111 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010112
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010113 channel = get_channel_arg(&argvars[0], FALSE);
10114 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010115}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010116#endif
10117
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010118/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010119 * "changenr()" function
10120 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010122f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010123{
10124 rettv->vval.v_number = curbuf->b_u_seq_cur;
10125}
10126
10127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 * "char2nr(string)" function
10129 */
10130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010131f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132{
10133#ifdef FEAT_MBYTE
10134 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010135 {
10136 int utf8 = 0;
10137
10138 if (argvars[1].v_type != VAR_UNKNOWN)
10139 utf8 = get_tv_number_chk(&argvars[1], NULL);
10140
10141 if (utf8)
10142 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10143 else
10144 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 else
10147#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010148 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149}
10150
10151/*
10152 * "cindent(lnum)" function
10153 */
10154 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010155f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156{
10157#ifdef FEAT_CINDENT
10158 pos_T pos;
10159 linenr_T lnum;
10160
10161 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010162 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10164 {
10165 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010166 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 curwin->w_cursor = pos;
10168 }
10169 else
10170#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010171 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172}
10173
10174/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010175 * "clearmatches()" function
10176 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010178f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010179{
10180#ifdef FEAT_SEARCH_EXTRA
10181 clear_matches(curwin);
10182#endif
10183}
10184
10185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 * "col(string)" function
10187 */
10188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010189f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190{
10191 colnr_T col = 0;
10192 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010193 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010195 fp = var2fpos(&argvars[0], FALSE, &fnum);
10196 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 {
10198 if (fp->col == MAXCOL)
10199 {
10200 /* '> can be MAXCOL, get the length of the line then */
10201 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010202 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 else
10204 col = MAXCOL;
10205 }
10206 else
10207 {
10208 col = fp->col + 1;
10209#ifdef FEAT_VIRTUALEDIT
10210 /* col(".") when the cursor is on the NUL at the end of the line
10211 * because of "coladd" can be seen as an extra column. */
10212 if (virtual_active() && fp == &curwin->w_cursor)
10213 {
10214 char_u *p = ml_get_cursor();
10215
10216 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10217 curwin->w_virtcol - curwin->w_cursor.coladd))
10218 {
10219# ifdef FEAT_MBYTE
10220 int l;
10221
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010222 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 col += l;
10224# else
10225 if (*p != NUL && p[1] == NUL)
10226 ++col;
10227# endif
10228 }
10229 }
10230#endif
10231 }
10232 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010233 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234}
10235
Bram Moolenaar572cb562005-08-05 21:35:02 +000010236#if defined(FEAT_INS_EXPAND)
10237/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010238 * "complete()" function
10239 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010241f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010242{
10243 int startcol;
10244
10245 if ((State & INSERT) == 0)
10246 {
10247 EMSG(_("E785: complete() can only be used in Insert mode"));
10248 return;
10249 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010250
10251 /* Check for undo allowed here, because if something was already inserted
10252 * the line was already saved for undo and this check isn't done. */
10253 if (!undo_allowed())
10254 return;
10255
Bram Moolenaarade00832006-03-10 21:46:58 +000010256 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10257 {
10258 EMSG(_(e_invarg));
10259 return;
10260 }
10261
10262 startcol = get_tv_number_chk(&argvars[0], NULL);
10263 if (startcol <= 0)
10264 return;
10265
10266 set_completion(startcol - 1, argvars[1].vval.v_list);
10267}
10268
10269/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010270 * "complete_add()" function
10271 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010273f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010274{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010275 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010276}
10277
10278/*
10279 * "complete_check()" function
10280 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010282f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010283{
10284 int saved = RedrawingDisabled;
10285
10286 RedrawingDisabled = 0;
10287 ins_compl_check_keys(0);
10288 rettv->vval.v_number = compl_interrupted;
10289 RedrawingDisabled = saved;
10290}
10291#endif
10292
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293/*
10294 * "confirm(message, buttons[, default [, type]])" function
10295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010297f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298{
10299#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10300 char_u *message;
10301 char_u *buttons = NULL;
10302 char_u buf[NUMBUFLEN];
10303 char_u buf2[NUMBUFLEN];
10304 int def = 1;
10305 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010306 char_u *typestr;
10307 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010309 message = get_tv_string_chk(&argvars[0]);
10310 if (message == NULL)
10311 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010312 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010314 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10315 if (buttons == NULL)
10316 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010317 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010319 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010320 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010322 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10323 if (typestr == NULL)
10324 error = TRUE;
10325 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010327 switch (TOUPPER_ASC(*typestr))
10328 {
10329 case 'E': type = VIM_ERROR; break;
10330 case 'Q': type = VIM_QUESTION; break;
10331 case 'I': type = VIM_INFO; break;
10332 case 'W': type = VIM_WARNING; break;
10333 case 'G': type = VIM_GENERIC; break;
10334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 }
10336 }
10337 }
10338 }
10339
10340 if (buttons == NULL || *buttons == NUL)
10341 buttons = (char_u *)_("&Ok");
10342
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010343 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010344 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010345 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346#endif
10347}
10348
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010349/*
10350 * "copy()" function
10351 */
10352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010353f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010354{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010355 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010356}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010358#ifdef FEAT_FLOAT
10359/*
10360 * "cos()" function
10361 */
10362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010363f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010364{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010365 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010366
10367 rettv->v_type = VAR_FLOAT;
10368 if (get_float_arg(argvars, &f) == OK)
10369 rettv->vval.v_float = cos(f);
10370 else
10371 rettv->vval.v_float = 0.0;
10372}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010373
10374/*
10375 * "cosh()" function
10376 */
10377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010378f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010379{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010380 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010381
10382 rettv->v_type = VAR_FLOAT;
10383 if (get_float_arg(argvars, &f) == OK)
10384 rettv->vval.v_float = cosh(f);
10385 else
10386 rettv->vval.v_float = 0.0;
10387}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010388#endif
10389
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010391 * "count()" function
10392 */
10393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010394f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010395{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010396 long n = 0;
10397 int ic = FALSE;
10398
Bram Moolenaare9a41262005-01-15 22:18:47 +000010399 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010400 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010401 listitem_T *li;
10402 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010403 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010404
Bram Moolenaare9a41262005-01-15 22:18:47 +000010405 if ((l = argvars[0].vval.v_list) != NULL)
10406 {
10407 li = l->lv_first;
10408 if (argvars[2].v_type != VAR_UNKNOWN)
10409 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010410 int error = FALSE;
10411
10412 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010413 if (argvars[3].v_type != VAR_UNKNOWN)
10414 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010415 idx = get_tv_number_chk(&argvars[3], &error);
10416 if (!error)
10417 {
10418 li = list_find(l, idx);
10419 if (li == NULL)
10420 EMSGN(_(e_listidx), idx);
10421 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010422 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010423 if (error)
10424 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010425 }
10426
10427 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010428 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010429 ++n;
10430 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010431 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010432 else if (argvars[0].v_type == VAR_DICT)
10433 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010434 int todo;
10435 dict_T *d;
10436 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010437
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010438 if ((d = argvars[0].vval.v_dict) != NULL)
10439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010440 int error = FALSE;
10441
Bram Moolenaare9a41262005-01-15 22:18:47 +000010442 if (argvars[2].v_type != VAR_UNKNOWN)
10443 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010444 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010445 if (argvars[3].v_type != VAR_UNKNOWN)
10446 EMSG(_(e_invarg));
10447 }
10448
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010449 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010450 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010451 {
10452 if (!HASHITEM_EMPTY(hi))
10453 {
10454 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010455 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010456 ++n;
10457 }
10458 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010459 }
10460 }
10461 else
10462 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010463 rettv->vval.v_number = n;
10464}
10465
10466/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10468 *
10469 * Checks the existence of a cscope connection.
10470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010472f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473{
10474#ifdef FEAT_CSCOPE
10475 int num = 0;
10476 char_u *dbpath = NULL;
10477 char_u *prepend = NULL;
10478 char_u buf[NUMBUFLEN];
10479
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010480 if (argvars[0].v_type != VAR_UNKNOWN
10481 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010483 num = (int)get_tv_number(&argvars[0]);
10484 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010485 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010486 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487 }
10488
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010489 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490#endif
10491}
10492
10493/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010494 * "cursor(lnum, col)" function, or
10495 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010497 * Moves the cursor to the specified line and column.
10498 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010501f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502{
10503 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010504#ifdef FEAT_VIRTUALEDIT
10505 long coladd = 0;
10506#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010507 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010509 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010510 if (argvars[1].v_type == VAR_UNKNOWN)
10511 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010512 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010513 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010514
Bram Moolenaar493c1782014-05-28 14:34:46 +020010515 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010516 {
10517 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010518 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010519 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010520 line = pos.lnum;
10521 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010522#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010523 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010524#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010525 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010526 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010527 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010528 set_curswant = FALSE;
10529 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010530 }
10531 else
10532 {
10533 line = get_tv_lnum(argvars);
10534 col = get_tv_number_chk(&argvars[1], NULL);
10535#ifdef FEAT_VIRTUALEDIT
10536 if (argvars[2].v_type != VAR_UNKNOWN)
10537 coladd = get_tv_number_chk(&argvars[2], NULL);
10538#endif
10539 }
10540 if (line < 0 || col < 0
10541#ifdef FEAT_VIRTUALEDIT
10542 || coladd < 0
10543#endif
10544 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010545 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546 if (line > 0)
10547 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 if (col > 0)
10549 curwin->w_cursor.col = col - 1;
10550#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010551 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552#endif
10553
10554 /* Make sure the cursor is in a valid position. */
10555 check_cursor();
10556#ifdef FEAT_MBYTE
10557 /* Correct cursor for multi-byte character. */
10558 if (has_mbyte)
10559 mb_adjust_cursor();
10560#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010561
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010562 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010563 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564}
10565
10566/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010567 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568 */
10569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010570f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010572 int noref = 0;
10573
10574 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010575 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010576 if (noref < 0 || noref > 1)
10577 EMSG(_(e_invarg));
10578 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010579 {
10580 current_copyID += COPYID_INC;
10581 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583}
10584
10585/*
10586 * "delete()" function
10587 */
10588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010589f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010591 char_u nbuf[NUMBUFLEN];
10592 char_u *name;
10593 char_u *flags;
10594
10595 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010597 return;
10598
10599 name = get_tv_string(&argvars[0]);
10600 if (name == NULL || *name == NUL)
10601 {
10602 EMSG(_(e_invarg));
10603 return;
10604 }
10605
10606 if (argvars[1].v_type != VAR_UNKNOWN)
10607 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010609 flags = (char_u *)"";
10610
10611 if (*flags == NUL)
10612 /* delete a file */
10613 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10614 else if (STRCMP(flags, "d") == 0)
10615 /* delete an empty directory */
10616 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10617 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010618 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010619 rettv->vval.v_number = delete_recursive(name);
10620 else
10621 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622}
10623
10624/*
10625 * "did_filetype()" function
10626 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010628f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629{
10630#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010631 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632#endif
10633}
10634
10635/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010636 * "diff_filler()" function
10637 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010639f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010640{
10641#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010642 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010643#endif
10644}
10645
10646/*
10647 * "diff_hlID()" function
10648 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010650f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010651{
10652#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010653 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010654 static linenr_T prev_lnum = 0;
10655 static int changedtick = 0;
10656 static int fnum = 0;
10657 static int change_start = 0;
10658 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010659 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010660 int filler_lines;
10661 int col;
10662
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010663 if (lnum < 0) /* ignore type error in {lnum} arg */
10664 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010665 if (lnum != prev_lnum
10666 || changedtick != curbuf->b_changedtick
10667 || fnum != curbuf->b_fnum)
10668 {
10669 /* New line, buffer, change: need to get the values. */
10670 filler_lines = diff_check(curwin, lnum);
10671 if (filler_lines < 0)
10672 {
10673 if (filler_lines == -1)
10674 {
10675 change_start = MAXCOL;
10676 change_end = -1;
10677 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10678 hlID = HLF_ADD; /* added line */
10679 else
10680 hlID = HLF_CHD; /* changed line */
10681 }
10682 else
10683 hlID = HLF_ADD; /* added line */
10684 }
10685 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010686 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010687 prev_lnum = lnum;
10688 changedtick = curbuf->b_changedtick;
10689 fnum = curbuf->b_fnum;
10690 }
10691
10692 if (hlID == HLF_CHD || hlID == HLF_TXD)
10693 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010694 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010695 if (col >= change_start && col <= change_end)
10696 hlID = HLF_TXD; /* changed text */
10697 else
10698 hlID = HLF_CHD; /* changed line */
10699 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010700 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010701#endif
10702}
10703
10704/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010705 * "disable_char_avail_for_testing({expr})" function
10706 */
10707 static void
10708f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10709{
10710 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10711}
10712
10713/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010714 * "empty({expr})" function
10715 */
10716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010717f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010718{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010719 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010720
10721 switch (argvars[0].v_type)
10722 {
10723 case VAR_STRING:
10724 case VAR_FUNC:
10725 n = argvars[0].vval.v_string == NULL
10726 || *argvars[0].vval.v_string == NUL;
10727 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010728 case VAR_PARTIAL:
10729 n = FALSE;
10730 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010731 case VAR_NUMBER:
10732 n = argvars[0].vval.v_number == 0;
10733 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010734 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010735#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010736 n = argvars[0].vval.v_float == 0.0;
10737 break;
10738#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010739 case VAR_LIST:
10740 n = argvars[0].vval.v_list == NULL
10741 || argvars[0].vval.v_list->lv_first == NULL;
10742 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010743 case VAR_DICT:
10744 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010745 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010746 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010747 case VAR_SPECIAL:
10748 n = argvars[0].vval.v_number != VVAL_TRUE;
10749 break;
10750
Bram Moolenaar835dc632016-02-07 14:27:38 +010010751 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010752#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010753 n = argvars[0].vval.v_job == NULL
10754 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10755 break;
10756#endif
10757 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010758#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010759 n = argvars[0].vval.v_channel == NULL
10760 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010761 break;
10762#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010763 case VAR_UNKNOWN:
10764 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10765 n = TRUE;
10766 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010767 }
10768
10769 rettv->vval.v_number = n;
10770}
10771
10772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 * "escape({string}, {chars})" function
10774 */
10775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010776f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777{
10778 char_u buf[NUMBUFLEN];
10779
Bram Moolenaar758711c2005-02-02 23:11:38 +000010780 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10781 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010782 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783}
10784
10785/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010786 * "eval()" function
10787 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010789f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010790{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010791 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010792
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010793 s = get_tv_string_chk(&argvars[0]);
10794 if (s != NULL)
10795 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010796
Bram Moolenaar615b9972015-01-14 17:15:05 +010010797 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010798 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10799 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010800 if (p != NULL && !aborting())
10801 EMSG2(_(e_invexpr2), p);
10802 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010803 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010804 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010805 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010806 else if (*s != NUL)
10807 EMSG(_(e_trailing));
10808}
10809
10810/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811 * "eventhandler()" function
10812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010814f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010816 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817}
10818
10819/*
10820 * "executable()" function
10821 */
10822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010823f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010825 char_u *name = get_tv_string(&argvars[0]);
10826
10827 /* Check in $PATH and also check directly if there is a directory name. */
10828 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10829 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010830}
10831
10832/*
10833 * "exepath()" function
10834 */
10835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010836f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010837{
10838 char_u *p = NULL;
10839
Bram Moolenaarb5971142015-03-21 17:32:19 +010010840 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010841 rettv->v_type = VAR_STRING;
10842 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843}
10844
10845/*
10846 * "exists()" function
10847 */
10848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010849f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850{
10851 char_u *p;
10852 char_u *name;
10853 int n = FALSE;
10854 int len = 0;
10855
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010856 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 if (*p == '$') /* environment variable */
10858 {
10859 /* first try "normal" environment variables (fast) */
10860 if (mch_getenv(p + 1) != NULL)
10861 n = TRUE;
10862 else
10863 {
10864 /* try expanding things like $VIM and ${HOME} */
10865 p = expand_env_save(p);
10866 if (p != NULL && *p != '$')
10867 n = TRUE;
10868 vim_free(p);
10869 }
10870 }
10871 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010872 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010873 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010874 if (*skipwhite(p) != NUL)
10875 n = FALSE; /* trailing garbage */
10876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 else if (*p == '*') /* internal or user defined function */
10878 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010879 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 }
10881 else if (*p == ':')
10882 {
10883 n = cmd_exists(p + 1);
10884 }
10885 else if (*p == '#')
10886 {
10887#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010888 if (p[1] == '#')
10889 n = autocmd_supported(p + 2);
10890 else
10891 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892#endif
10893 }
10894 else /* internal variable */
10895 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010896 char_u *tofree;
10897 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010899 /* get_name_len() takes care of expanding curly braces */
10900 name = p;
10901 len = get_name_len(&p, &tofree, TRUE, FALSE);
10902 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010904 if (tofree != NULL)
10905 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010906 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010907 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010909 /* handle d.key, l[idx], f(expr) */
10910 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10911 if (n)
10912 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 }
10914 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010915 if (*p != NUL)
10916 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010918 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 }
10920
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010921 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922}
10923
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010924#ifdef FEAT_FLOAT
10925/*
10926 * "exp()" function
10927 */
10928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010929f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010930{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010931 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010932
10933 rettv->v_type = VAR_FLOAT;
10934 if (get_float_arg(argvars, &f) == OK)
10935 rettv->vval.v_float = exp(f);
10936 else
10937 rettv->vval.v_float = 0.0;
10938}
10939#endif
10940
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941/*
10942 * "expand()" function
10943 */
10944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010945f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946{
10947 char_u *s;
10948 int len;
10949 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010950 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010952 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010953 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010955 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010956 if (argvars[1].v_type != VAR_UNKNOWN
10957 && argvars[2].v_type != VAR_UNKNOWN
10958 && get_tv_number_chk(&argvars[2], &error)
10959 && !error)
10960 {
10961 rettv->v_type = VAR_LIST;
10962 rettv->vval.v_list = NULL;
10963 }
10964
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010965 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 if (*s == '%' || *s == '#' || *s == '<')
10967 {
10968 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010969 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010971 if (rettv->v_type == VAR_LIST)
10972 {
10973 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10974 list_append_string(rettv->vval.v_list, result, -1);
10975 else
10976 vim_free(result);
10977 }
10978 else
10979 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 }
10981 else
10982 {
10983 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010984 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010985 if (argvars[1].v_type != VAR_UNKNOWN
10986 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010987 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010988 if (!error)
10989 {
10990 ExpandInit(&xpc);
10991 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010992 if (p_wic)
10993 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010994 if (rettv->v_type == VAR_STRING)
10995 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10996 options, WILD_ALL);
10997 else if (rettv_list_alloc(rettv) != FAIL)
10998 {
10999 int i;
11000
11001 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11002 for (i = 0; i < xpc.xp_numfiles; i++)
11003 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11004 ExpandCleanup(&xpc);
11005 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011006 }
11007 else
11008 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009 }
11010}
11011
11012/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011013 * Go over all entries in "d2" and add them to "d1".
11014 * When "action" is "error" then a duplicate key is an error.
11015 * When "action" is "force" then a duplicate key is overwritten.
11016 * Otherwise duplicate keys are ignored ("action" is "keep").
11017 */
11018 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011019dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011020{
11021 dictitem_T *di1;
11022 hashitem_T *hi2;
11023 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011024 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011025
11026 todo = (int)d2->dv_hashtab.ht_used;
11027 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11028 {
11029 if (!HASHITEM_EMPTY(hi2))
11030 {
11031 --todo;
11032 di1 = dict_find(d1, hi2->hi_key, -1);
11033 if (d1->dv_scope != 0)
11034 {
11035 /* Disallow replacing a builtin function in l: and g:.
11036 * Check the key to be valid when adding to any
11037 * scope. */
11038 if (d1->dv_scope == VAR_DEF_SCOPE
11039 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11040 && var_check_func_name(hi2->hi_key,
11041 di1 == NULL))
11042 break;
11043 if (!valid_varname(hi2->hi_key))
11044 break;
11045 }
11046 if (di1 == NULL)
11047 {
11048 di1 = dictitem_copy(HI2DI(hi2));
11049 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11050 dictitem_free(di1);
11051 }
11052 else if (*action == 'e')
11053 {
11054 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11055 break;
11056 }
11057 else if (*action == 'f' && HI2DI(hi2) != di1)
11058 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011059 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11060 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011061 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011062 clear_tv(&di1->di_tv);
11063 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11064 }
11065 }
11066 }
11067}
11068
11069/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011070 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011071 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011072 */
11073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011074f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011075{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011076 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011077
Bram Moolenaare9a41262005-01-15 22:18:47 +000011078 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011079 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011080 list_T *l1, *l2;
11081 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011082 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011083 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011084
Bram Moolenaare9a41262005-01-15 22:18:47 +000011085 l1 = argvars[0].vval.v_list;
11086 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011087 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011088 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011089 {
11090 if (argvars[2].v_type != VAR_UNKNOWN)
11091 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011092 before = get_tv_number_chk(&argvars[2], &error);
11093 if (error)
11094 return; /* type error; errmsg already given */
11095
Bram Moolenaar758711c2005-02-02 23:11:38 +000011096 if (before == l1->lv_len)
11097 item = NULL;
11098 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011099 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011100 item = list_find(l1, before);
11101 if (item == NULL)
11102 {
11103 EMSGN(_(e_listidx), before);
11104 return;
11105 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011106 }
11107 }
11108 else
11109 item = NULL;
11110 list_extend(l1, l2, item);
11111
Bram Moolenaare9a41262005-01-15 22:18:47 +000011112 copy_tv(&argvars[0], rettv);
11113 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011114 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011115 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11116 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011117 dict_T *d1, *d2;
11118 char_u *action;
11119 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011120
11121 d1 = argvars[0].vval.v_dict;
11122 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011123 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011124 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011125 {
11126 /* Check the third argument. */
11127 if (argvars[2].v_type != VAR_UNKNOWN)
11128 {
11129 static char *(av[]) = {"keep", "force", "error"};
11130
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011131 action = get_tv_string_chk(&argvars[2]);
11132 if (action == NULL)
11133 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011134 for (i = 0; i < 3; ++i)
11135 if (STRCMP(action, av[i]) == 0)
11136 break;
11137 if (i == 3)
11138 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011139 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011140 return;
11141 }
11142 }
11143 else
11144 action = (char_u *)"force";
11145
Bram Moolenaara9922d62013-05-30 13:01:18 +020011146 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011147
Bram Moolenaare9a41262005-01-15 22:18:47 +000011148 copy_tv(&argvars[0], rettv);
11149 }
11150 }
11151 else
11152 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011153}
11154
11155/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011156 * "feedkeys()" function
11157 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011159f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011160{
11161 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011162 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011163 char_u *keys, *flags;
11164 char_u nbuf[NUMBUFLEN];
11165 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011166 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011167 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011168
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011169 /* This is not allowed in the sandbox. If the commands would still be
11170 * executed in the sandbox it would be OK, but it probably happens later,
11171 * when "sandbox" is no longer set. */
11172 if (check_secure())
11173 return;
11174
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011175 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011176
11177 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011178 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011179 flags = get_tv_string_buf(&argvars[1], nbuf);
11180 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011181 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011182 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011183 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011184 case 'n': remap = FALSE; break;
11185 case 'm': remap = TRUE; break;
11186 case 't': typed = TRUE; break;
11187 case 'i': insert = TRUE; break;
11188 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011189 }
11190 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011191 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011192
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011193 if (*keys != NUL || execute)
11194 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011195 /* Need to escape K_SPECIAL and CSI before putting the string in the
11196 * typeahead buffer. */
11197 keys_esc = vim_strsave_escape_csi(keys);
11198 if (keys_esc != NULL)
11199 {
11200 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011201 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011202 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011203 if (vgetc_busy)
11204 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011205 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011206 {
11207 int save_msg_scroll = msg_scroll;
11208
11209 /* Avoid a 1 second delay when the keys start Insert mode. */
11210 msg_scroll = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011211 exec_normal(TRUE);
Bram Moolenaar9e496852016-03-11 19:31:47 +010011212 msg_scroll |= save_msg_scroll;
11213 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011214 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011215 }
11216}
11217
11218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219 * "filereadable()" function
11220 */
11221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011222f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011224 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225 char_u *p;
11226 int n;
11227
Bram Moolenaarc236c162008-07-13 17:41:49 +000011228#ifndef O_NONBLOCK
11229# define O_NONBLOCK 0
11230#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011231 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011232 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11233 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234 {
11235 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011236 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 }
11238 else
11239 n = FALSE;
11240
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011241 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242}
11243
11244/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011245 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246 * rights to write into.
11247 */
11248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011249f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011251 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252}
11253
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011254 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011255findfilendir(
11256 typval_T *argvars UNUSED,
11257 typval_T *rettv,
11258 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011259{
11260#ifdef FEAT_SEARCHPATH
11261 char_u *fname;
11262 char_u *fresult = NULL;
11263 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11264 char_u *p;
11265 char_u pathbuf[NUMBUFLEN];
11266 int count = 1;
11267 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011268 int error = FALSE;
11269#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011270
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011271 rettv->vval.v_string = NULL;
11272 rettv->v_type = VAR_STRING;
11273
11274#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011276
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011277 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011279 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11280 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011281 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011282 else
11283 {
11284 if (*p != NUL)
11285 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011286
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011287 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011288 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011289 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011290 }
11291
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011292 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11293 error = TRUE;
11294
11295 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011296 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011297 do
11298 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011299 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011300 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011301 fresult = find_file_in_path_option(first ? fname : NULL,
11302 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011303 0, first, path,
11304 find_what,
11305 curbuf->b_ffname,
11306 find_what == FINDFILE_DIR
11307 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011308 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011309
11310 if (fresult != NULL && rettv->v_type == VAR_LIST)
11311 list_append_string(rettv->vval.v_list, fresult, -1);
11312
11313 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011314 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011315
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011316 if (rettv->v_type == VAR_STRING)
11317 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011318#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011319}
11320
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011321static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11322static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011323
11324/*
11325 * Implementation of map() and filter().
11326 */
11327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011328filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011329{
11330 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011331 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011332 listitem_T *li, *nli;
11333 list_T *l = NULL;
11334 dictitem_T *di;
11335 hashtab_T *ht;
11336 hashitem_T *hi;
11337 dict_T *d = NULL;
11338 typval_T save_val;
11339 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011340 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011341 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011342 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011343 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011344 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011345 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011346 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011347
Bram Moolenaare9a41262005-01-15 22:18:47 +000011348 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011349 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011350 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011351 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011352 return;
11353 }
11354 else if (argvars[0].v_type == VAR_DICT)
11355 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011356 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011357 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011358 return;
11359 }
11360 else
11361 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011362 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011363 return;
11364 }
11365
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011366 expr = get_tv_string_buf_chk(&argvars[1], buf);
11367 /* On type errors, the preceding call has already displayed an error
11368 * message. Avoid a misleading error message for an empty string that
11369 * was not passed as argument. */
11370 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011371 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011372 prepare_vimvar(VV_VAL, &save_val);
11373 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011374
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011375 /* We reset "did_emsg" to be able to detect whether an error
11376 * occurred during evaluation of the expression. */
11377 save_did_emsg = did_emsg;
11378 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011379
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011380 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011381 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011382 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011383 vimvars[VV_KEY].vv_type = VAR_STRING;
11384
11385 ht = &d->dv_hashtab;
11386 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011387 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011388 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011389 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011390 if (!HASHITEM_EMPTY(hi))
11391 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011392 int r;
11393
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011394 --todo;
11395 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011396 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011397 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11398 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011399 break;
11400 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011401 r = filter_map_one(&di->di_tv, expr, map, &rem);
11402 clear_tv(&vimvars[VV_KEY].vv_tv);
11403 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011404 break;
11405 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011406 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011407 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11408 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011409 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011410 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011411 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011412 }
11413 }
11414 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011415 }
11416 else
11417 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011418 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11419
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011420 for (li = l->lv_first; li != NULL; li = nli)
11421 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011422 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011423 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011424 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011425 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011426 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011427 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011428 break;
11429 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011430 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011431 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011432 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011433 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011434
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011435 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011436 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011437
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011438 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011439 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011440
11441 copy_tv(&argvars[0], rettv);
11442}
11443
11444 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011445filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011446{
Bram Moolenaar33570922005-01-25 22:26:29 +000011447 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011448 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011449 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011450
Bram Moolenaar33570922005-01-25 22:26:29 +000011451 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011452 s = expr;
11453 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011454 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011455 if (*s != NUL) /* check for trailing chars after expr */
11456 {
11457 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011458 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011459 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011460 }
11461 if (map)
11462 {
11463 /* map(): replace the list item value */
11464 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011465 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011466 *tv = rettv;
11467 }
11468 else
11469 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011470 int error = FALSE;
11471
Bram Moolenaare9a41262005-01-15 22:18:47 +000011472 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011473 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011474 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011475 /* On type error, nothing has been removed; return FAIL to stop the
11476 * loop. The error message was given by get_tv_number_chk(). */
11477 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011478 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011479 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011480 retval = OK;
11481theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011482 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011483 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011484}
11485
11486/*
11487 * "filter()" function
11488 */
11489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011490f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011491{
11492 filter_map(argvars, rettv, FALSE);
11493}
11494
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011495/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011496 * "finddir({fname}[, {path}[, {count}]])" function
11497 */
11498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011499f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011500{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011501 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011502}
11503
11504/*
11505 * "findfile({fname}[, {path}[, {count}]])" function
11506 */
11507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011508f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011509{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011510 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011511}
11512
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011513#ifdef FEAT_FLOAT
11514/*
11515 * "float2nr({float})" function
11516 */
11517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011518f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011519{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011520 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011521
11522 if (get_float_arg(argvars, &f) == OK)
11523 {
11524 if (f < -0x7fffffff)
11525 rettv->vval.v_number = -0x7fffffff;
11526 else if (f > 0x7fffffff)
11527 rettv->vval.v_number = 0x7fffffff;
11528 else
11529 rettv->vval.v_number = (varnumber_T)f;
11530 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011531}
11532
11533/*
11534 * "floor({float})" function
11535 */
11536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011537f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011538{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011539 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011540
11541 rettv->v_type = VAR_FLOAT;
11542 if (get_float_arg(argvars, &f) == OK)
11543 rettv->vval.v_float = floor(f);
11544 else
11545 rettv->vval.v_float = 0.0;
11546}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011547
11548/*
11549 * "fmod()" function
11550 */
11551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011552f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011553{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011554 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011555
11556 rettv->v_type = VAR_FLOAT;
11557 if (get_float_arg(argvars, &fx) == OK
11558 && get_float_arg(&argvars[1], &fy) == OK)
11559 rettv->vval.v_float = fmod(fx, fy);
11560 else
11561 rettv->vval.v_float = 0.0;
11562}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011563#endif
11564
Bram Moolenaar0d660222005-01-07 21:51:51 +000011565/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011566 * "fnameescape({string})" function
11567 */
11568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011569f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011570{
11571 rettv->vval.v_string = vim_strsave_fnameescape(
11572 get_tv_string(&argvars[0]), FALSE);
11573 rettv->v_type = VAR_STRING;
11574}
11575
11576/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011577 * "fnamemodify({fname}, {mods})" function
11578 */
11579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011580f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581{
11582 char_u *fname;
11583 char_u *mods;
11584 int usedlen = 0;
11585 int len;
11586 char_u *fbuf = NULL;
11587 char_u buf[NUMBUFLEN];
11588
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011589 fname = get_tv_string_chk(&argvars[0]);
11590 mods = get_tv_string_buf_chk(&argvars[1], buf);
11591 if (fname == NULL || mods == NULL)
11592 fname = NULL;
11593 else
11594 {
11595 len = (int)STRLEN(fname);
11596 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011599 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011601 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011602 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011603 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604 vim_free(fbuf);
11605}
11606
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011607static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608
11609/*
11610 * "foldclosed()" function
11611 */
11612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011613foldclosed_both(
11614 typval_T *argvars UNUSED,
11615 typval_T *rettv,
11616 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617{
11618#ifdef FEAT_FOLDING
11619 linenr_T lnum;
11620 linenr_T first, last;
11621
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011622 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011623 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11624 {
11625 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11626 {
11627 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011628 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011629 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011630 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631 return;
11632 }
11633 }
11634#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011635 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636}
11637
11638/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011639 * "foldclosed()" function
11640 */
11641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011642f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011643{
11644 foldclosed_both(argvars, rettv, FALSE);
11645}
11646
11647/*
11648 * "foldclosedend()" function
11649 */
11650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011651f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011652{
11653 foldclosed_both(argvars, rettv, TRUE);
11654}
11655
11656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657 * "foldlevel()" function
11658 */
11659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011660f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661{
11662#ifdef FEAT_FOLDING
11663 linenr_T lnum;
11664
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011665 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011667 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669}
11670
11671/*
11672 * "foldtext()" function
11673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011675f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676{
11677#ifdef FEAT_FOLDING
11678 linenr_T lnum;
11679 char_u *s;
11680 char_u *r;
11681 int len;
11682 char *txt;
11683#endif
11684
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011685 rettv->v_type = VAR_STRING;
11686 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011687#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011688 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11689 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11690 <= curbuf->b_ml.ml_line_count
11691 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692 {
11693 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011694 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11695 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696 {
11697 if (!linewhite(lnum))
11698 break;
11699 ++lnum;
11700 }
11701
11702 /* Find interesting text in this line. */
11703 s = skipwhite(ml_get(lnum));
11704 /* skip C comment-start */
11705 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011706 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011708 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011709 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011710 {
11711 s = skipwhite(ml_get(lnum + 1));
11712 if (*s == '*')
11713 s = skipwhite(s + 1);
11714 }
11715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716 txt = _("+-%s%3ld lines: ");
11717 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011718 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719 + 20 /* for %3ld */
11720 + STRLEN(s))); /* concatenated */
11721 if (r != NULL)
11722 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011723 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11724 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11725 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011726 len = (int)STRLEN(r);
11727 STRCAT(r, s);
11728 /* remove 'foldmarker' and 'commentstring' */
11729 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011730 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731 }
11732 }
11733#endif
11734}
11735
11736/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011737 * "foldtextresult(lnum)" function
11738 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011740f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011741{
11742#ifdef FEAT_FOLDING
11743 linenr_T lnum;
11744 char_u *text;
11745 char_u buf[51];
11746 foldinfo_T foldinfo;
11747 int fold_count;
11748#endif
11749
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011750 rettv->v_type = VAR_STRING;
11751 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011752#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011753 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011754 /* treat illegal types and illegal string values for {lnum} the same */
11755 if (lnum < 0)
11756 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011757 fold_count = foldedCount(curwin, lnum, &foldinfo);
11758 if (fold_count > 0)
11759 {
11760 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11761 &foldinfo, buf);
11762 if (text == buf)
11763 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011764 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011765 }
11766#endif
11767}
11768
11769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770 * "foreground()" function
11771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011773f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775#ifdef FEAT_GUI
11776 if (gui.in_use)
11777 gui_mch_set_foreground();
11778#else
11779# ifdef WIN32
11780 win32_set_foreground();
11781# endif
11782#endif
11783}
11784
11785/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011786 * "function()" function
11787 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011789f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011790{
11791 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011792 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011793 int use_string = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011794
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011795 if (argvars[0].v_type == VAR_FUNC)
11796 {
11797 /* function(MyFunc, [arg], dict) */
11798 s = argvars[0].vval.v_string;
11799 }
11800 else if (argvars[0].v_type == VAR_PARTIAL
11801 && argvars[0].vval.v_partial != NULL)
11802 /* function(dict.MyFunc, [arg]) */
11803 s = argvars[0].vval.v_partial->pt_name;
11804 else
11805 {
11806 /* function('MyFunc', [arg], dict) */
11807 s = get_tv_string(&argvars[0]);
11808 use_string = TRUE;
11809 }
11810
11811 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011812 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011813 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011814 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
11815 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011816 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011817 else
11818 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011819 int dict_idx = 0;
11820 int arg_idx = 0;
11821 list_T *list = NULL;
11822
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011823 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011824 {
11825 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011826 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011827
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011828 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11829 * also be called from another script. Using trans_function_name()
11830 * would also work, but some plugins depend on the name being
11831 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011832 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011833 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
11834 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011835 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011836 STRCPY(name, sid_buf);
11837 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011838 }
11839 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011840 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011841 name = vim_strsave(s);
11842
11843 if (argvars[1].v_type != VAR_UNKNOWN)
11844 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011845 if (argvars[2].v_type != VAR_UNKNOWN)
11846 {
11847 /* function(name, [args], dict) */
11848 arg_idx = 1;
11849 dict_idx = 2;
11850 }
11851 else if (argvars[1].v_type == VAR_DICT)
11852 /* function(name, dict) */
11853 dict_idx = 1;
11854 else
11855 /* function(name, [args]) */
11856 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010011857 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011858 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011859 if (argvars[dict_idx].v_type != VAR_DICT)
11860 {
11861 EMSG(_("E922: expected a dict"));
11862 vim_free(name);
11863 return;
11864 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011865 if (argvars[0].v_type == VAR_PARTIAL)
11866 {
11867 EMSG2(_(e_dict_both), name);
11868 vim_free(name);
11869 return;
11870 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011871 if (argvars[dict_idx].vval.v_dict == NULL)
11872 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011873 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011874 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011875 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011876 if (argvars[arg_idx].v_type != VAR_LIST)
11877 {
11878 EMSG(_("E923: Second argument of function() must be a list or a dict"));
11879 vim_free(name);
11880 return;
11881 }
11882 list = argvars[arg_idx].vval.v_list;
11883 if (list == NULL || list->lv_len == 0)
11884 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011885 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011886 }
11887 if (dict_idx > 0 || arg_idx > 0)
11888 {
11889 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011890
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011891 if (pt != NULL)
11892 {
11893 if (arg_idx > 0)
11894 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011895 listitem_T *li;
11896 int i = 0;
11897
11898 pt->pt_argv = (typval_T *)alloc(
11899 sizeof(typval_T) * list->lv_len);
11900 if (pt->pt_argv == NULL)
11901 {
11902 vim_free(pt);
11903 vim_free(name);
11904 return;
11905 }
11906 else
11907 {
11908 pt->pt_argc = list->lv_len;
11909 for (li = list->lv_first; li != NULL; li = li->li_next)
11910 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
11911 }
11912 }
11913
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011914 if (argvars[0].v_type == VAR_PARTIAL)
11915 {
11916 pt->pt_dict = argvars[0].vval.v_partial->pt_dict;
11917 ++pt->pt_dict->dv_refcount;
11918 }
11919 else if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011920 {
11921 pt->pt_dict = argvars[dict_idx].vval.v_dict;
11922 ++pt->pt_dict->dv_refcount;
11923 }
11924
11925 pt->pt_refcount = 1;
11926 pt->pt_name = name;
11927 func_ref(pt->pt_name);
11928 }
11929 rettv->v_type = VAR_PARTIAL;
11930 rettv->vval.v_partial = pt;
11931 }
11932 else
11933 {
11934 rettv->v_type = VAR_FUNC;
11935 rettv->vval.v_string = name;
11936 func_ref(name);
11937 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011938 }
11939}
11940
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011941 static void
11942partial_free(partial_T *pt)
11943{
11944 int i;
11945
11946 for (i = 0; i < pt->pt_argc; ++i)
11947 clear_tv(&pt->pt_argv[i]);
11948 vim_free(pt->pt_argv);
11949 func_unref(pt->pt_name);
11950 vim_free(pt->pt_name);
11951 vim_free(pt);
11952}
11953
11954/*
11955 * Unreference a closure: decrement the reference count and free it when it
11956 * becomes zero.
11957 */
11958 void
11959partial_unref(partial_T *pt)
11960{
11961 if (pt != NULL && --pt->pt_refcount <= 0)
11962 partial_free(pt);
11963}
11964
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011965/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011966 * "garbagecollect()" function
11967 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011969f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011970{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011971 /* This is postponed until we are back at the toplevel, because we may be
11972 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11973 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011974
11975 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11976 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011977}
11978
11979/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011980 * "get()" function
11981 */
11982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011983f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011984{
Bram Moolenaar33570922005-01-25 22:26:29 +000011985 listitem_T *li;
11986 list_T *l;
11987 dictitem_T *di;
11988 dict_T *d;
11989 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011990
Bram Moolenaare9a41262005-01-15 22:18:47 +000011991 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011992 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011993 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011994 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011995 int error = FALSE;
11996
11997 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11998 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011999 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012000 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012001 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012002 else if (argvars[0].v_type == VAR_DICT)
12003 {
12004 if ((d = argvars[0].vval.v_dict) != NULL)
12005 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012006 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012007 if (di != NULL)
12008 tv = &di->di_tv;
12009 }
12010 }
12011 else
12012 EMSG2(_(e_listdictarg), "get()");
12013
12014 if (tv == NULL)
12015 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012016 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012017 copy_tv(&argvars[2], rettv);
12018 }
12019 else
12020 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012021}
12022
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012023static 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 +000012024
12025/*
12026 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012027 * Return a range (from start to end) of lines in rettv from the specified
12028 * buffer.
12029 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012030 */
12031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012032get_buffer_lines(
12033 buf_T *buf,
12034 linenr_T start,
12035 linenr_T end,
12036 int retlist,
12037 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012038{
12039 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012040
Bram Moolenaar959a1432013-12-14 12:17:38 +010012041 rettv->v_type = VAR_STRING;
12042 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012043 if (retlist && rettv_list_alloc(rettv) == FAIL)
12044 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012045
12046 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12047 return;
12048
12049 if (!retlist)
12050 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012051 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12052 p = ml_get_buf(buf, start, FALSE);
12053 else
12054 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012055 rettv->vval.v_string = vim_strsave(p);
12056 }
12057 else
12058 {
12059 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012060 return;
12061
12062 if (start < 1)
12063 start = 1;
12064 if (end > buf->b_ml.ml_line_count)
12065 end = buf->b_ml.ml_line_count;
12066 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012067 if (list_append_string(rettv->vval.v_list,
12068 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012069 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012070 }
12071}
12072
12073/*
12074 * "getbufline()" function
12075 */
12076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012077f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012078{
12079 linenr_T lnum;
12080 linenr_T end;
12081 buf_T *buf;
12082
12083 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12084 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012085 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012086 --emsg_off;
12087
Bram Moolenaar661b1822005-07-28 22:36:45 +000012088 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012089 if (argvars[2].v_type == VAR_UNKNOWN)
12090 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012091 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012092 end = get_tv_lnum_buf(&argvars[2], buf);
12093
Bram Moolenaar342337a2005-07-21 21:11:17 +000012094 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012095}
12096
Bram Moolenaar0d660222005-01-07 21:51:51 +000012097/*
12098 * "getbufvar()" function
12099 */
12100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012101f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012102{
12103 buf_T *buf;
12104 buf_T *save_curbuf;
12105 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012106 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012107 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012108
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012109 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12110 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012111 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012112 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012113
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012114 rettv->v_type = VAR_STRING;
12115 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012116
12117 if (buf != NULL && varname != NULL)
12118 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012119 /* set curbuf to be our buf, temporarily */
12120 save_curbuf = curbuf;
12121 curbuf = buf;
12122
Bram Moolenaar0d660222005-01-07 21:51:51 +000012123 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012124 {
12125 if (get_option_tv(&varname, rettv, TRUE) == OK)
12126 done = TRUE;
12127 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012128 else if (STRCMP(varname, "changedtick") == 0)
12129 {
12130 rettv->v_type = VAR_NUMBER;
12131 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012132 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012133 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012134 else
12135 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012136 /* Look up the variable. */
12137 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12138 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12139 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012140 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012141 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012142 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012143 done = TRUE;
12144 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012145 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012146
12147 /* restore previous notion of curbuf */
12148 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012149 }
12150
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012151 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12152 /* use the default value */
12153 copy_tv(&argvars[2], rettv);
12154
Bram Moolenaar0d660222005-01-07 21:51:51 +000012155 --emsg_off;
12156}
12157
12158/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159 * "getchar()" function
12160 */
12161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012162f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163{
12164 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012165 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012167 /* Position the cursor. Needed after a message that ends in a space. */
12168 windgoto(msg_row, msg_col);
12169
Bram Moolenaar071d4272004-06-13 20:20:40 +000012170 ++no_mapping;
12171 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012172 for (;;)
12173 {
12174 if (argvars[0].v_type == VAR_UNKNOWN)
12175 /* getchar(): blocking wait. */
12176 n = safe_vgetc();
12177 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12178 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012179 n = vpeekc_any();
12180 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012181 /* illegal argument or getchar(0) and no char avail: return zero */
12182 n = 0;
12183 else
12184 /* getchar(0) and char avail: return char */
12185 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012186
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012187 if (n == K_IGNORE)
12188 continue;
12189 break;
12190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191 --no_mapping;
12192 --allow_keys;
12193
Bram Moolenaar219b8702006-11-01 14:32:36 +000012194 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12195 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12196 vimvars[VV_MOUSE_COL].vv_nr = 0;
12197
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012198 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199 if (IS_SPECIAL(n) || mod_mask != 0)
12200 {
12201 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12202 int i = 0;
12203
12204 /* Turn a special key into three bytes, plus modifier. */
12205 if (mod_mask != 0)
12206 {
12207 temp[i++] = K_SPECIAL;
12208 temp[i++] = KS_MODIFIER;
12209 temp[i++] = mod_mask;
12210 }
12211 if (IS_SPECIAL(n))
12212 {
12213 temp[i++] = K_SPECIAL;
12214 temp[i++] = K_SECOND(n);
12215 temp[i++] = K_THIRD(n);
12216 }
12217#ifdef FEAT_MBYTE
12218 else if (has_mbyte)
12219 i += (*mb_char2bytes)(n, temp + i);
12220#endif
12221 else
12222 temp[i++] = n;
12223 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012224 rettv->v_type = VAR_STRING;
12225 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012226
12227#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012228 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012229 {
12230 int row = mouse_row;
12231 int col = mouse_col;
12232 win_T *win;
12233 linenr_T lnum;
12234# ifdef FEAT_WINDOWS
12235 win_T *wp;
12236# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012237 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012238
12239 if (row >= 0 && col >= 0)
12240 {
12241 /* Find the window at the mouse coordinates and compute the
12242 * text position. */
12243 win = mouse_find_win(&row, &col);
12244 (void)mouse_comp_pos(win, &row, &col, &lnum);
12245# ifdef FEAT_WINDOWS
12246 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012247 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012248# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012249 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012250 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12251 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12252 }
12253 }
12254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255 }
12256}
12257
12258/*
12259 * "getcharmod()" function
12260 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012262f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012263{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012264 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265}
12266
12267/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012268 * "getcharsearch()" function
12269 */
12270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012271f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012272{
12273 if (rettv_dict_alloc(rettv) != FAIL)
12274 {
12275 dict_T *dict = rettv->vval.v_dict;
12276
12277 dict_add_nr_str(dict, "char", 0L, last_csearch());
12278 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12279 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12280 }
12281}
12282
12283/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284 * "getcmdline()" function
12285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012287f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012288{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012289 rettv->v_type = VAR_STRING;
12290 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291}
12292
12293/*
12294 * "getcmdpos()" function
12295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012297f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012299 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300}
12301
12302/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012303 * "getcmdtype()" function
12304 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012306f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012307{
12308 rettv->v_type = VAR_STRING;
12309 rettv->vval.v_string = alloc(2);
12310 if (rettv->vval.v_string != NULL)
12311 {
12312 rettv->vval.v_string[0] = get_cmdline_type();
12313 rettv->vval.v_string[1] = NUL;
12314 }
12315}
12316
12317/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012318 * "getcmdwintype()" function
12319 */
12320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012321f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012322{
12323 rettv->v_type = VAR_STRING;
12324 rettv->vval.v_string = NULL;
12325#ifdef FEAT_CMDWIN
12326 rettv->vval.v_string = alloc(2);
12327 if (rettv->vval.v_string != NULL)
12328 {
12329 rettv->vval.v_string[0] = cmdwin_type;
12330 rettv->vval.v_string[1] = NUL;
12331 }
12332#endif
12333}
12334
12335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336 * "getcwd()" function
12337 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012339f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012341 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012342 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012344 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012345 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012346
12347 wp = find_tabwin(&argvars[0], &argvars[1]);
12348 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012350 if (wp->w_localdir != NULL)
12351 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12352 else if(globaldir != NULL)
12353 rettv->vval.v_string = vim_strsave(globaldir);
12354 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012355 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012356 cwd = alloc(MAXPATHL);
12357 if (cwd != NULL)
12358 {
12359 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12360 rettv->vval.v_string = vim_strsave(cwd);
12361 vim_free(cwd);
12362 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012363 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012364#ifdef BACKSLASH_IN_FILENAME
12365 if (rettv->vval.v_string != NULL)
12366 slash_adjust(rettv->vval.v_string);
12367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368 }
12369}
12370
12371/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012372 * "getfontname()" function
12373 */
12374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012375f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012376{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012377 rettv->v_type = VAR_STRING;
12378 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012379#ifdef FEAT_GUI
12380 if (gui.in_use)
12381 {
12382 GuiFont font;
12383 char_u *name = NULL;
12384
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012385 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012386 {
12387 /* Get the "Normal" font. Either the name saved by
12388 * hl_set_font_name() or from the font ID. */
12389 font = gui.norm_font;
12390 name = hl_get_font_name();
12391 }
12392 else
12393 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012394 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012395 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12396 return;
12397 font = gui_mch_get_font(name, FALSE);
12398 if (font == NOFONT)
12399 return; /* Invalid font name, return empty string. */
12400 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012401 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012402 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012403 gui_mch_free_font(font);
12404 }
12405#endif
12406}
12407
12408/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012409 * "getfperm({fname})" function
12410 */
12411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012412f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012413{
12414 char_u *fname;
12415 struct stat st;
12416 char_u *perm = NULL;
12417 char_u flags[] = "rwx";
12418 int i;
12419
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012420 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012421
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012422 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012423 if (mch_stat((char *)fname, &st) >= 0)
12424 {
12425 perm = vim_strsave((char_u *)"---------");
12426 if (perm != NULL)
12427 {
12428 for (i = 0; i < 9; i++)
12429 {
12430 if (st.st_mode & (1 << (8 - i)))
12431 perm[i] = flags[i % 3];
12432 }
12433 }
12434 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012435 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012436}
12437
12438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439 * "getfsize({fname})" function
12440 */
12441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012442f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012443{
12444 char_u *fname;
12445 struct stat st;
12446
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012447 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012449 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012450
12451 if (mch_stat((char *)fname, &st) >= 0)
12452 {
12453 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012454 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012456 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012457 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012458
12459 /* non-perfect check for overflow */
12460 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12461 rettv->vval.v_number = -2;
12462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012463 }
12464 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012465 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012466}
12467
12468/*
12469 * "getftime({fname})" function
12470 */
12471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012472f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473{
12474 char_u *fname;
12475 struct stat st;
12476
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012477 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012478
12479 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012480 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012481 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012482 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483}
12484
12485/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012486 * "getftype({fname})" function
12487 */
12488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012489f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012490{
12491 char_u *fname;
12492 struct stat st;
12493 char_u *type = NULL;
12494 char *t;
12495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012496 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012497
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012498 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012499 if (mch_lstat((char *)fname, &st) >= 0)
12500 {
12501#ifdef S_ISREG
12502 if (S_ISREG(st.st_mode))
12503 t = "file";
12504 else if (S_ISDIR(st.st_mode))
12505 t = "dir";
12506# ifdef S_ISLNK
12507 else if (S_ISLNK(st.st_mode))
12508 t = "link";
12509# endif
12510# ifdef S_ISBLK
12511 else if (S_ISBLK(st.st_mode))
12512 t = "bdev";
12513# endif
12514# ifdef S_ISCHR
12515 else if (S_ISCHR(st.st_mode))
12516 t = "cdev";
12517# endif
12518# ifdef S_ISFIFO
12519 else if (S_ISFIFO(st.st_mode))
12520 t = "fifo";
12521# endif
12522# ifdef S_ISSOCK
12523 else if (S_ISSOCK(st.st_mode))
12524 t = "fifo";
12525# endif
12526 else
12527 t = "other";
12528#else
12529# ifdef S_IFMT
12530 switch (st.st_mode & S_IFMT)
12531 {
12532 case S_IFREG: t = "file"; break;
12533 case S_IFDIR: t = "dir"; break;
12534# ifdef S_IFLNK
12535 case S_IFLNK: t = "link"; break;
12536# endif
12537# ifdef S_IFBLK
12538 case S_IFBLK: t = "bdev"; break;
12539# endif
12540# ifdef S_IFCHR
12541 case S_IFCHR: t = "cdev"; break;
12542# endif
12543# ifdef S_IFIFO
12544 case S_IFIFO: t = "fifo"; break;
12545# endif
12546# ifdef S_IFSOCK
12547 case S_IFSOCK: t = "socket"; break;
12548# endif
12549 default: t = "other";
12550 }
12551# else
12552 if (mch_isdir(fname))
12553 t = "dir";
12554 else
12555 t = "file";
12556# endif
12557#endif
12558 type = vim_strsave((char_u *)t);
12559 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012560 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012561}
12562
12563/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012564 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012565 */
12566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012567f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012568{
12569 linenr_T lnum;
12570 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012571 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012572
12573 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012574 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012575 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012576 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012577 retlist = FALSE;
12578 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012579 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012580 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012581 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012582 retlist = TRUE;
12583 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012584
Bram Moolenaar342337a2005-07-21 21:11:17 +000012585 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012586}
12587
12588/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012589 * "getmatches()" function
12590 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012592f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012593{
12594#ifdef FEAT_SEARCH_EXTRA
12595 dict_T *dict;
12596 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012597 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012598
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012599 if (rettv_list_alloc(rettv) == OK)
12600 {
12601 while (cur != NULL)
12602 {
12603 dict = dict_alloc();
12604 if (dict == NULL)
12605 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012606 if (cur->match.regprog == NULL)
12607 {
12608 /* match added with matchaddpos() */
12609 for (i = 0; i < MAXPOSMATCH; ++i)
12610 {
12611 llpos_T *llpos;
12612 char buf[6];
12613 list_T *l;
12614
12615 llpos = &cur->pos.pos[i];
12616 if (llpos->lnum == 0)
12617 break;
12618 l = list_alloc();
12619 if (l == NULL)
12620 break;
12621 list_append_number(l, (varnumber_T)llpos->lnum);
12622 if (llpos->col > 0)
12623 {
12624 list_append_number(l, (varnumber_T)llpos->col);
12625 list_append_number(l, (varnumber_T)llpos->len);
12626 }
12627 sprintf(buf, "pos%d", i + 1);
12628 dict_add_list(dict, buf, l);
12629 }
12630 }
12631 else
12632 {
12633 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12634 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012635 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012636 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12637 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012638# ifdef FEAT_CONCEAL
12639 if (cur->conceal_char)
12640 {
12641 char_u buf[MB_MAXBYTES + 1];
12642
12643 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12644 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12645 }
12646# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012647 list_append_dict(rettv->vval.v_list, dict);
12648 cur = cur->next;
12649 }
12650 }
12651#endif
12652}
12653
12654/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012655 * "getpid()" function
12656 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012658f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012659{
12660 rettv->vval.v_number = mch_get_pid();
12661}
12662
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012663static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012664
12665/*
12666 * "getcurpos()" function
12667 */
12668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012669f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012670{
12671 getpos_both(argvars, rettv, TRUE);
12672}
12673
Bram Moolenaar18081e32008-02-20 19:11:07 +000012674/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012675 * "getpos(string)" function
12676 */
12677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012678f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012679{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012680 getpos_both(argvars, rettv, FALSE);
12681}
12682
12683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012684getpos_both(
12685 typval_T *argvars,
12686 typval_T *rettv,
12687 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012688{
Bram Moolenaara5525202006-03-02 22:52:09 +000012689 pos_T *fp;
12690 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012691 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012692
12693 if (rettv_list_alloc(rettv) == OK)
12694 {
12695 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012696 if (getcurpos)
12697 fp = &curwin->w_cursor;
12698 else
12699 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012700 if (fnum != -1)
12701 list_append_number(l, (varnumber_T)fnum);
12702 else
12703 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012704 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12705 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012706 list_append_number(l, (fp != NULL)
12707 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012708 : (varnumber_T)0);
12709 list_append_number(l,
12710#ifdef FEAT_VIRTUALEDIT
12711 (fp != NULL) ? (varnumber_T)fp->coladd :
12712#endif
12713 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012714 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012715 {
12716 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012717 list_append_number(l, curwin->w_curswant == MAXCOL ?
12718 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012719 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012720 }
12721 else
12722 rettv->vval.v_number = FALSE;
12723}
12724
12725/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012726 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012727 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012729f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012730{
12731#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012732 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012733#endif
12734
Bram Moolenaar2641f772005-03-25 21:58:17 +000012735#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012736 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012737 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012738 wp = NULL;
12739 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12740 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012741 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012742 if (wp == NULL)
12743 return;
12744 }
12745
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012746 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012747 }
12748#endif
12749}
12750
12751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012752 * "getreg()" function
12753 */
12754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012755f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012756{
12757 char_u *strregname;
12758 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012759 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012760 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012761 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012763 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012764 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012765 strregname = get_tv_string_chk(&argvars[0]);
12766 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012767 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012768 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012769 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012770 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12771 return_list = get_tv_number_chk(&argvars[2], &error);
12772 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012775 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012776
12777 if (error)
12778 return;
12779
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780 regname = (strregname == NULL ? '"' : *strregname);
12781 if (regname == 0)
12782 regname = '"';
12783
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012784 if (return_list)
12785 {
12786 rettv->v_type = VAR_LIST;
12787 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12788 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012789 if (rettv->vval.v_list != NULL)
12790 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012791 }
12792 else
12793 {
12794 rettv->v_type = VAR_STRING;
12795 rettv->vval.v_string = get_reg_contents(regname,
12796 arg2 ? GREG_EXPR_SRC : 0);
12797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798}
12799
12800/*
12801 * "getregtype()" function
12802 */
12803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012804f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805{
12806 char_u *strregname;
12807 int regname;
12808 char_u buf[NUMBUFLEN + 2];
12809 long reglen = 0;
12810
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012811 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012812 {
12813 strregname = get_tv_string_chk(&argvars[0]);
12814 if (strregname == NULL) /* type error; errmsg already given */
12815 {
12816 rettv->v_type = VAR_STRING;
12817 rettv->vval.v_string = NULL;
12818 return;
12819 }
12820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821 else
12822 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012823 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824
12825 regname = (strregname == NULL ? '"' : *strregname);
12826 if (regname == 0)
12827 regname = '"';
12828
12829 buf[0] = NUL;
12830 buf[1] = NUL;
12831 switch (get_reg_type(regname, &reglen))
12832 {
12833 case MLINE: buf[0] = 'V'; break;
12834 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835 case MBLOCK:
12836 buf[0] = Ctrl_V;
12837 sprintf((char *)buf + 1, "%ld", reglen + 1);
12838 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012840 rettv->v_type = VAR_STRING;
12841 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842}
12843
12844/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012845 * "gettabvar()" function
12846 */
12847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012848f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012849{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012850 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012851 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012852 dictitem_T *v;
12853 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012854 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012855
12856 rettv->v_type = VAR_STRING;
12857 rettv->vval.v_string = NULL;
12858
12859 varname = get_tv_string_chk(&argvars[1]);
12860 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12861 if (tp != NULL && varname != NULL)
12862 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012863 /* Set tp to be our tabpage, temporarily. Also set the window to the
12864 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012865 if (switch_win(&oldcurwin, &oldtabpage,
12866 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012867 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012868 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012869 /* look up the variable */
12870 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12871 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12872 if (v != NULL)
12873 {
12874 copy_tv(&v->di_tv, rettv);
12875 done = TRUE;
12876 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012877 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012878
12879 /* restore previous notion of curwin */
12880 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012881 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012882
12883 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012884 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012885}
12886
12887/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012888 * "gettabwinvar()" function
12889 */
12890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012891f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012892{
12893 getwinvar(argvars, rettv, 1);
12894}
12895
12896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012897 * "getwinposx()" function
12898 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012900f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012901{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012902 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012903#ifdef FEAT_GUI
12904 if (gui.in_use)
12905 {
12906 int x, y;
12907
12908 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012909 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910 }
12911#endif
12912}
12913
12914/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010012915 * "win_findbuf()" function
12916 */
12917 static void
12918f_win_findbuf(typval_T *argvars, typval_T *rettv)
12919{
12920 if (rettv_list_alloc(rettv) != FAIL)
12921 win_findbuf(argvars, rettv->vval.v_list);
12922}
12923
12924/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010012925 * "win_getid()" function
12926 */
12927 static void
12928f_win_getid(typval_T *argvars, typval_T *rettv)
12929{
12930 rettv->vval.v_number = win_getid(argvars);
12931}
12932
12933/*
12934 * "win_gotoid()" function
12935 */
12936 static void
12937f_win_gotoid(typval_T *argvars, typval_T *rettv)
12938{
12939 rettv->vval.v_number = win_gotoid(argvars);
12940}
12941
12942/*
12943 * "win_id2tabwin()" function
12944 */
12945 static void
12946f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
12947{
12948 if (rettv_list_alloc(rettv) != FAIL)
12949 win_id2tabwin(argvars, rettv->vval.v_list);
12950}
12951
12952/*
12953 * "win_id2win()" function
12954 */
12955 static void
12956f_win_id2win(typval_T *argvars, typval_T *rettv)
12957{
12958 rettv->vval.v_number = win_id2win(argvars);
12959}
12960
12961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962 * "getwinposy()" function
12963 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012965f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012967 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968#ifdef FEAT_GUI
12969 if (gui.in_use)
12970 {
12971 int x, y;
12972
12973 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012974 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012975 }
12976#endif
12977}
12978
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012979/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012980 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012981 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012982 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012983find_win_by_nr(
12984 typval_T *vp,
12985 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012986{
12987#ifdef FEAT_WINDOWS
12988 win_T *wp;
12989#endif
12990 int nr;
12991
12992 nr = get_tv_number_chk(vp, NULL);
12993
12994#ifdef FEAT_WINDOWS
12995 if (nr < 0)
12996 return NULL;
12997 if (nr == 0)
12998 return curwin;
12999
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013000 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13001 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013002 if (--nr <= 0)
13003 break;
13004 return wp;
13005#else
13006 if (nr == 0 || nr == 1)
13007 return curwin;
13008 return NULL;
13009#endif
13010}
13011
Bram Moolenaar071d4272004-06-13 20:20:40 +000013012/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013013 * Find window specified by "wvp" in tabpage "tvp".
13014 */
13015 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013016find_tabwin(
13017 typval_T *wvp, /* VAR_UNKNOWN for current window */
13018 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013019{
13020 win_T *wp = NULL;
13021 tabpage_T *tp = NULL;
13022 long n;
13023
13024 if (wvp->v_type != VAR_UNKNOWN)
13025 {
13026 if (tvp->v_type != VAR_UNKNOWN)
13027 {
13028 n = get_tv_number(tvp);
13029 if (n >= 0)
13030 tp = find_tabpage(n);
13031 }
13032 else
13033 tp = curtab;
13034
13035 if (tp != NULL)
13036 wp = find_win_by_nr(wvp, tp);
13037 }
13038 else
13039 wp = curwin;
13040
13041 return wp;
13042}
13043
13044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013045 * "getwinvar()" function
13046 */
13047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013048f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013050 getwinvar(argvars, rettv, 0);
13051}
13052
13053/*
13054 * getwinvar() and gettabwinvar()
13055 */
13056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013057getwinvar(
13058 typval_T *argvars,
13059 typval_T *rettv,
13060 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013061{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013062 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013063 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013064 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013065 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013066 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013067#ifdef FEAT_WINDOWS
13068 win_T *oldcurwin;
13069 tabpage_T *oldtabpage;
13070 int need_switch_win;
13071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013072
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013073#ifdef FEAT_WINDOWS
13074 if (off == 1)
13075 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13076 else
13077 tp = curtab;
13078#endif
13079 win = find_win_by_nr(&argvars[off], tp);
13080 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013081 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013082
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013083 rettv->v_type = VAR_STRING;
13084 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085
13086 if (win != NULL && varname != NULL)
13087 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013088#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013089 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013090 * otherwise the window is not valid. Only do this when needed,
13091 * autocommands get blocked. */
13092 need_switch_win = !(tp == curtab && win == curwin);
13093 if (!need_switch_win
13094 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13095#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013096 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013097 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013098 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013099 if (get_option_tv(&varname, rettv, 1) == OK)
13100 done = TRUE;
13101 }
13102 else
13103 {
13104 /* Look up the variable. */
13105 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13106 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13107 varname, FALSE);
13108 if (v != NULL)
13109 {
13110 copy_tv(&v->di_tv, rettv);
13111 done = TRUE;
13112 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013114 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013115
Bram Moolenaarba117c22015-09-29 16:53:22 +020013116#ifdef FEAT_WINDOWS
13117 if (need_switch_win)
13118 /* restore previous notion of curwin */
13119 restore_win(oldcurwin, oldtabpage, TRUE);
13120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013121 }
13122
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013123 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13124 /* use the default return value */
13125 copy_tv(&argvars[off + 2], rettv);
13126
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127 --emsg_off;
13128}
13129
13130/*
13131 * "glob()" function
13132 */
13133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013134f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013136 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013138 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013139
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013140 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013141 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013142 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013143 if (argvars[1].v_type != VAR_UNKNOWN)
13144 {
13145 if (get_tv_number_chk(&argvars[1], &error))
13146 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013147 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013148 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013149 if (get_tv_number_chk(&argvars[2], &error))
13150 {
13151 rettv->v_type = VAR_LIST;
13152 rettv->vval.v_list = NULL;
13153 }
13154 if (argvars[3].v_type != VAR_UNKNOWN
13155 && get_tv_number_chk(&argvars[3], &error))
13156 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013157 }
13158 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013159 if (!error)
13160 {
13161 ExpandInit(&xpc);
13162 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013163 if (p_wic)
13164 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013165 if (rettv->v_type == VAR_STRING)
13166 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013167 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013168 else if (rettv_list_alloc(rettv) != FAIL)
13169 {
13170 int i;
13171
13172 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13173 NULL, options, WILD_ALL_KEEP);
13174 for (i = 0; i < xpc.xp_numfiles; i++)
13175 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13176
13177 ExpandCleanup(&xpc);
13178 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013179 }
13180 else
13181 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013182}
13183
13184/*
13185 * "globpath()" function
13186 */
13187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013188f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013189{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013190 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013191 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013192 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013193 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013194 garray_T ga;
13195 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013197 /* When the optional second argument is non-zero, don't remove matches
13198 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013199 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013200 if (argvars[2].v_type != VAR_UNKNOWN)
13201 {
13202 if (get_tv_number_chk(&argvars[2], &error))
13203 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013204 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013205 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013206 if (get_tv_number_chk(&argvars[3], &error))
13207 {
13208 rettv->v_type = VAR_LIST;
13209 rettv->vval.v_list = NULL;
13210 }
13211 if (argvars[4].v_type != VAR_UNKNOWN
13212 && get_tv_number_chk(&argvars[4], &error))
13213 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013214 }
13215 }
13216 if (file != NULL && !error)
13217 {
13218 ga_init2(&ga, (int)sizeof(char_u *), 10);
13219 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13220 if (rettv->v_type == VAR_STRING)
13221 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13222 else if (rettv_list_alloc(rettv) != FAIL)
13223 for (i = 0; i < ga.ga_len; ++i)
13224 list_append_string(rettv->vval.v_list,
13225 ((char_u **)(ga.ga_data))[i], -1);
13226 ga_clear_strings(&ga);
13227 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013228 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013229 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230}
13231
13232/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013233 * "glob2regpat()" function
13234 */
13235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013236f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013237{
13238 char_u *pat = get_tv_string_chk(&argvars[0]);
13239
13240 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013241 rettv->vval.v_string = (pat == NULL)
13242 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013243}
13244
13245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246 * "has()" function
13247 */
13248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013249f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250{
13251 int i;
13252 char_u *name;
13253 int n = FALSE;
13254 static char *(has_list[]) =
13255 {
13256#ifdef AMIGA
13257 "amiga",
13258# ifdef FEAT_ARP
13259 "arp",
13260# endif
13261#endif
13262#ifdef __BEOS__
13263 "beos",
13264#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013265#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013266 "mac",
13267#endif
13268#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013269 "macunix", /* built with 'darwin' enabled */
13270#endif
13271#if defined(__APPLE__) && __APPLE__ == 1
13272 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274#ifdef __QNX__
13275 "qnx",
13276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277#ifdef UNIX
13278 "unix",
13279#endif
13280#ifdef VMS
13281 "vms",
13282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013283#ifdef WIN32
13284 "win32",
13285#endif
13286#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13287 "win32unix",
13288#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013289#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290 "win64",
13291#endif
13292#ifdef EBCDIC
13293 "ebcdic",
13294#endif
13295#ifndef CASE_INSENSITIVE_FILENAME
13296 "fname_case",
13297#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013298#ifdef HAVE_ACL
13299 "acl",
13300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013301#ifdef FEAT_ARABIC
13302 "arabic",
13303#endif
13304#ifdef FEAT_AUTOCMD
13305 "autocmd",
13306#endif
13307#ifdef FEAT_BEVAL
13308 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013309# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13310 "balloon_multiline",
13311# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312#endif
13313#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13314 "builtin_terms",
13315# ifdef ALL_BUILTIN_TCAPS
13316 "all_builtin_terms",
13317# endif
13318#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013319#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13320 || defined(FEAT_GUI_W32) \
13321 || defined(FEAT_GUI_MOTIF))
13322 "browsefilter",
13323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324#ifdef FEAT_BYTEOFF
13325 "byte_offset",
13326#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013327#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013328 "channel",
13329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013330#ifdef FEAT_CINDENT
13331 "cindent",
13332#endif
13333#ifdef FEAT_CLIENTSERVER
13334 "clientserver",
13335#endif
13336#ifdef FEAT_CLIPBOARD
13337 "clipboard",
13338#endif
13339#ifdef FEAT_CMDL_COMPL
13340 "cmdline_compl",
13341#endif
13342#ifdef FEAT_CMDHIST
13343 "cmdline_hist",
13344#endif
13345#ifdef FEAT_COMMENTS
13346 "comments",
13347#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013348#ifdef FEAT_CONCEAL
13349 "conceal",
13350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013351#ifdef FEAT_CRYPT
13352 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013353 "crypt-blowfish",
13354 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355#endif
13356#ifdef FEAT_CSCOPE
13357 "cscope",
13358#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013359#ifdef FEAT_CURSORBIND
13360 "cursorbind",
13361#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013362#ifdef CURSOR_SHAPE
13363 "cursorshape",
13364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013365#ifdef DEBUG
13366 "debug",
13367#endif
13368#ifdef FEAT_CON_DIALOG
13369 "dialog_con",
13370#endif
13371#ifdef FEAT_GUI_DIALOG
13372 "dialog_gui",
13373#endif
13374#ifdef FEAT_DIFF
13375 "diff",
13376#endif
13377#ifdef FEAT_DIGRAPHS
13378 "digraphs",
13379#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013380#ifdef FEAT_DIRECTX
13381 "directx",
13382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013383#ifdef FEAT_DND
13384 "dnd",
13385#endif
13386#ifdef FEAT_EMACS_TAGS
13387 "emacs_tags",
13388#endif
13389 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013390 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391#ifdef FEAT_SEARCH_EXTRA
13392 "extra_search",
13393#endif
13394#ifdef FEAT_FKMAP
13395 "farsi",
13396#endif
13397#ifdef FEAT_SEARCHPATH
13398 "file_in_path",
13399#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013400#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013401 "filterpipe",
13402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403#ifdef FEAT_FIND_ID
13404 "find_in_path",
13405#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013406#ifdef FEAT_FLOAT
13407 "float",
13408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409#ifdef FEAT_FOLDING
13410 "folding",
13411#endif
13412#ifdef FEAT_FOOTER
13413 "footer",
13414#endif
13415#if !defined(USE_SYSTEM) && defined(UNIX)
13416 "fork",
13417#endif
13418#ifdef FEAT_GETTEXT
13419 "gettext",
13420#endif
13421#ifdef FEAT_GUI
13422 "gui",
13423#endif
13424#ifdef FEAT_GUI_ATHENA
13425# ifdef FEAT_GUI_NEXTAW
13426 "gui_neXtaw",
13427# else
13428 "gui_athena",
13429# endif
13430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013431#ifdef FEAT_GUI_GTK
13432 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013433# ifdef USE_GTK3
13434 "gui_gtk3",
13435# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013436 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013437# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013439#ifdef FEAT_GUI_GNOME
13440 "gui_gnome",
13441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442#ifdef FEAT_GUI_MAC
13443 "gui_mac",
13444#endif
13445#ifdef FEAT_GUI_MOTIF
13446 "gui_motif",
13447#endif
13448#ifdef FEAT_GUI_PHOTON
13449 "gui_photon",
13450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013451#ifdef FEAT_GUI_W32
13452 "gui_win32",
13453#endif
13454#ifdef FEAT_HANGULIN
13455 "hangul_input",
13456#endif
13457#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13458 "iconv",
13459#endif
13460#ifdef FEAT_INS_EXPAND
13461 "insert_expand",
13462#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013463#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013464 "job",
13465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466#ifdef FEAT_JUMPLIST
13467 "jumplist",
13468#endif
13469#ifdef FEAT_KEYMAP
13470 "keymap",
13471#endif
13472#ifdef FEAT_LANGMAP
13473 "langmap",
13474#endif
13475#ifdef FEAT_LIBCALL
13476 "libcall",
13477#endif
13478#ifdef FEAT_LINEBREAK
13479 "linebreak",
13480#endif
13481#ifdef FEAT_LISP
13482 "lispindent",
13483#endif
13484#ifdef FEAT_LISTCMDS
13485 "listcmds",
13486#endif
13487#ifdef FEAT_LOCALMAP
13488 "localmap",
13489#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013490#ifdef FEAT_LUA
13491# ifndef DYNAMIC_LUA
13492 "lua",
13493# endif
13494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495#ifdef FEAT_MENU
13496 "menu",
13497#endif
13498#ifdef FEAT_SESSION
13499 "mksession",
13500#endif
13501#ifdef FEAT_MODIFY_FNAME
13502 "modify_fname",
13503#endif
13504#ifdef FEAT_MOUSE
13505 "mouse",
13506#endif
13507#ifdef FEAT_MOUSESHAPE
13508 "mouseshape",
13509#endif
13510#if defined(UNIX) || defined(VMS)
13511# ifdef FEAT_MOUSE_DEC
13512 "mouse_dec",
13513# endif
13514# ifdef FEAT_MOUSE_GPM
13515 "mouse_gpm",
13516# endif
13517# ifdef FEAT_MOUSE_JSB
13518 "mouse_jsbterm",
13519# endif
13520# ifdef FEAT_MOUSE_NET
13521 "mouse_netterm",
13522# endif
13523# ifdef FEAT_MOUSE_PTERM
13524 "mouse_pterm",
13525# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013526# ifdef FEAT_MOUSE_SGR
13527 "mouse_sgr",
13528# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013529# ifdef FEAT_SYSMOUSE
13530 "mouse_sysmouse",
13531# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013532# ifdef FEAT_MOUSE_URXVT
13533 "mouse_urxvt",
13534# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535# ifdef FEAT_MOUSE_XTERM
13536 "mouse_xterm",
13537# endif
13538#endif
13539#ifdef FEAT_MBYTE
13540 "multi_byte",
13541#endif
13542#ifdef FEAT_MBYTE_IME
13543 "multi_byte_ime",
13544#endif
13545#ifdef FEAT_MULTI_LANG
13546 "multi_lang",
13547#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013548#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013549#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013550 "mzscheme",
13551#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553#ifdef FEAT_OLE
13554 "ole",
13555#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013556 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557#ifdef FEAT_PATH_EXTRA
13558 "path_extra",
13559#endif
13560#ifdef FEAT_PERL
13561#ifndef DYNAMIC_PERL
13562 "perl",
13563#endif
13564#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013565#ifdef FEAT_PERSISTENT_UNDO
13566 "persistent_undo",
13567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568#ifdef FEAT_PYTHON
13569#ifndef DYNAMIC_PYTHON
13570 "python",
13571#endif
13572#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013573#ifdef FEAT_PYTHON3
13574#ifndef DYNAMIC_PYTHON3
13575 "python3",
13576#endif
13577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578#ifdef FEAT_POSTSCRIPT
13579 "postscript",
13580#endif
13581#ifdef FEAT_PRINTER
13582 "printer",
13583#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013584#ifdef FEAT_PROFILE
13585 "profile",
13586#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013587#ifdef FEAT_RELTIME
13588 "reltime",
13589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590#ifdef FEAT_QUICKFIX
13591 "quickfix",
13592#endif
13593#ifdef FEAT_RIGHTLEFT
13594 "rightleft",
13595#endif
13596#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13597 "ruby",
13598#endif
13599#ifdef FEAT_SCROLLBIND
13600 "scrollbind",
13601#endif
13602#ifdef FEAT_CMDL_INFO
13603 "showcmd",
13604 "cmdline_info",
13605#endif
13606#ifdef FEAT_SIGNS
13607 "signs",
13608#endif
13609#ifdef FEAT_SMARTINDENT
13610 "smartindent",
13611#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013612#ifdef STARTUPTIME
13613 "startuptime",
13614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615#ifdef FEAT_STL_OPT
13616 "statusline",
13617#endif
13618#ifdef FEAT_SUN_WORKSHOP
13619 "sun_workshop",
13620#endif
13621#ifdef FEAT_NETBEANS_INTG
13622 "netbeans_intg",
13623#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013624#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013625 "spell",
13626#endif
13627#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628 "syntax",
13629#endif
13630#if defined(USE_SYSTEM) || !defined(UNIX)
13631 "system",
13632#endif
13633#ifdef FEAT_TAG_BINS
13634 "tag_binary",
13635#endif
13636#ifdef FEAT_TAG_OLDSTATIC
13637 "tag_old_static",
13638#endif
13639#ifdef FEAT_TAG_ANYWHITE
13640 "tag_any_white",
13641#endif
13642#ifdef FEAT_TCL
13643# ifndef DYNAMIC_TCL
13644 "tcl",
13645# endif
13646#endif
13647#ifdef TERMINFO
13648 "terminfo",
13649#endif
13650#ifdef FEAT_TERMRESPONSE
13651 "termresponse",
13652#endif
13653#ifdef FEAT_TEXTOBJ
13654 "textobjects",
13655#endif
13656#ifdef HAVE_TGETENT
13657 "tgetent",
13658#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010013659#ifdef FEAT_TIMERS
13660 "timers",
13661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662#ifdef FEAT_TITLE
13663 "title",
13664#endif
13665#ifdef FEAT_TOOLBAR
13666 "toolbar",
13667#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013668#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13669 "unnamedplus",
13670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671#ifdef FEAT_USR_CMDS
13672 "user-commands", /* was accidentally included in 5.4 */
13673 "user_commands",
13674#endif
13675#ifdef FEAT_VIMINFO
13676 "viminfo",
13677#endif
13678#ifdef FEAT_VERTSPLIT
13679 "vertsplit",
13680#endif
13681#ifdef FEAT_VIRTUALEDIT
13682 "virtualedit",
13683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685#ifdef FEAT_VISUALEXTRA
13686 "visualextra",
13687#endif
13688#ifdef FEAT_VREPLACE
13689 "vreplace",
13690#endif
13691#ifdef FEAT_WILDIGN
13692 "wildignore",
13693#endif
13694#ifdef FEAT_WILDMENU
13695 "wildmenu",
13696#endif
13697#ifdef FEAT_WINDOWS
13698 "windows",
13699#endif
13700#ifdef FEAT_WAK
13701 "winaltkeys",
13702#endif
13703#ifdef FEAT_WRITEBACKUP
13704 "writebackup",
13705#endif
13706#ifdef FEAT_XIM
13707 "xim",
13708#endif
13709#ifdef FEAT_XFONTSET
13710 "xfontset",
13711#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013712#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013713 "xpm",
13714 "xpm_w32", /* for backward compatibility */
13715#else
13716# if defined(HAVE_XPM)
13717 "xpm",
13718# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720#ifdef USE_XSMP
13721 "xsmp",
13722#endif
13723#ifdef USE_XSMP_INTERACT
13724 "xsmp_interact",
13725#endif
13726#ifdef FEAT_XCLIPBOARD
13727 "xterm_clipboard",
13728#endif
13729#ifdef FEAT_XTERM_SAVE
13730 "xterm_save",
13731#endif
13732#if defined(UNIX) && defined(FEAT_X11)
13733 "X11",
13734#endif
13735 NULL
13736 };
13737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013738 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739 for (i = 0; has_list[i] != NULL; ++i)
13740 if (STRICMP(name, has_list[i]) == 0)
13741 {
13742 n = TRUE;
13743 break;
13744 }
13745
13746 if (n == FALSE)
13747 {
13748 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013749 {
13750 if (name[5] == '-'
13751 && STRLEN(name) > 11
13752 && vim_isdigit(name[6])
13753 && vim_isdigit(name[8])
13754 && vim_isdigit(name[10]))
13755 {
13756 int major = atoi((char *)name + 6);
13757 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013758
13759 /* Expect "patch-9.9.01234". */
13760 n = (major < VIM_VERSION_MAJOR
13761 || (major == VIM_VERSION_MAJOR
13762 && (minor < VIM_VERSION_MINOR
13763 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013764 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013765 }
13766 else
13767 n = has_patch(atoi((char *)name + 5));
13768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013769 else if (STRICMP(name, "vim_starting") == 0)
13770 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013771#ifdef FEAT_MBYTE
13772 else if (STRICMP(name, "multi_byte_encoding") == 0)
13773 n = has_mbyte;
13774#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013775#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13776 else if (STRICMP(name, "balloon_multiline") == 0)
13777 n = multiline_balloon_available();
13778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779#ifdef DYNAMIC_TCL
13780 else if (STRICMP(name, "tcl") == 0)
13781 n = tcl_enabled(FALSE);
13782#endif
13783#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13784 else if (STRICMP(name, "iconv") == 0)
13785 n = iconv_enabled(FALSE);
13786#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013787#ifdef DYNAMIC_LUA
13788 else if (STRICMP(name, "lua") == 0)
13789 n = lua_enabled(FALSE);
13790#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013791#ifdef DYNAMIC_MZSCHEME
13792 else if (STRICMP(name, "mzscheme") == 0)
13793 n = mzscheme_enabled(FALSE);
13794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795#ifdef DYNAMIC_RUBY
13796 else if (STRICMP(name, "ruby") == 0)
13797 n = ruby_enabled(FALSE);
13798#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013799#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800#ifdef DYNAMIC_PYTHON
13801 else if (STRICMP(name, "python") == 0)
13802 n = python_enabled(FALSE);
13803#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013804#endif
13805#ifdef FEAT_PYTHON3
13806#ifdef DYNAMIC_PYTHON3
13807 else if (STRICMP(name, "python3") == 0)
13808 n = python3_enabled(FALSE);
13809#endif
13810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811#ifdef DYNAMIC_PERL
13812 else if (STRICMP(name, "perl") == 0)
13813 n = perl_enabled(FALSE);
13814#endif
13815#ifdef FEAT_GUI
13816 else if (STRICMP(name, "gui_running") == 0)
13817 n = (gui.in_use || gui.starting);
13818# ifdef FEAT_GUI_W32
13819 else if (STRICMP(name, "gui_win32s") == 0)
13820 n = gui_is_win32s();
13821# endif
13822# ifdef FEAT_BROWSE
13823 else if (STRICMP(name, "browse") == 0)
13824 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13825# endif
13826#endif
13827#ifdef FEAT_SYN_HL
13828 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013829 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830#endif
13831#if defined(WIN3264)
13832 else if (STRICMP(name, "win95") == 0)
13833 n = mch_windows95();
13834#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013835#ifdef FEAT_NETBEANS_INTG
13836 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013837 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013839 }
13840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013841 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842}
13843
13844/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013845 * "has_key()" function
13846 */
13847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013848f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013849{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013850 if (argvars[0].v_type != VAR_DICT)
13851 {
13852 EMSG(_(e_dictreq));
13853 return;
13854 }
13855 if (argvars[0].vval.v_dict == NULL)
13856 return;
13857
13858 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013859 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013860}
13861
13862/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013863 * "haslocaldir()" function
13864 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013866f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013867{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013868 win_T *wp = NULL;
13869
13870 wp = find_tabwin(&argvars[0], &argvars[1]);
13871 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013872}
13873
13874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875 * "hasmapto()" function
13876 */
13877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013878f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013879{
13880 char_u *name;
13881 char_u *mode;
13882 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013883 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013885 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013886 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013887 mode = (char_u *)"nvo";
13888 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013889 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013890 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013891 if (argvars[2].v_type != VAR_UNKNOWN)
13892 abbr = get_tv_number(&argvars[2]);
13893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894
Bram Moolenaar2c932302006-03-18 21:42:09 +000013895 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013896 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013898 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899}
13900
13901/*
13902 * "histadd()" function
13903 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013905f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013906{
13907#ifdef FEAT_CMDHIST
13908 int histype;
13909 char_u *str;
13910 char_u buf[NUMBUFLEN];
13911#endif
13912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013913 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013914 if (check_restricted() || check_secure())
13915 return;
13916#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013917 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13918 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919 if (histype >= 0)
13920 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013921 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013922 if (*str != NUL)
13923 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013924 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013926 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927 return;
13928 }
13929 }
13930#endif
13931}
13932
13933/*
13934 * "histdel()" function
13935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013937f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938{
13939#ifdef FEAT_CMDHIST
13940 int n;
13941 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013942 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013944 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13945 if (str == NULL)
13946 n = 0;
13947 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013948 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013949 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013950 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013952 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013953 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954 else
13955 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013956 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013957 get_tv_string_buf(&argvars[1], buf));
13958 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959#endif
13960}
13961
13962/*
13963 * "histget()" function
13964 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013966f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967{
13968#ifdef FEAT_CMDHIST
13969 int type;
13970 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013971 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013973 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13974 if (str == NULL)
13975 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013977 {
13978 type = get_histtype(str);
13979 if (argvars[1].v_type == VAR_UNKNOWN)
13980 idx = get_history_idx(type);
13981 else
13982 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13983 /* -1 on type error */
13984 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013987 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013989 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990}
13991
13992/*
13993 * "histnr()" function
13994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013996f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997{
13998 int i;
13999
14000#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014001 char_u *history = get_tv_string_chk(&argvars[0]);
14002
14003 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014004 if (i >= HIST_CMD && i < HIST_COUNT)
14005 i = get_history_idx(i);
14006 else
14007#endif
14008 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014009 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010}
14011
14012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013 * "highlightID(name)" function
14014 */
14015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014016f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014017{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014018 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014019}
14020
14021/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014022 * "highlight_exists()" function
14023 */
14024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014025f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014026{
14027 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14028}
14029
14030/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031 * "hostname()" function
14032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014034f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035{
14036 char_u hostname[256];
14037
14038 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014039 rettv->v_type = VAR_STRING;
14040 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014041}
14042
14043/*
14044 * iconv() function
14045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014047f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048{
14049#ifdef FEAT_MBYTE
14050 char_u buf1[NUMBUFLEN];
14051 char_u buf2[NUMBUFLEN];
14052 char_u *from, *to, *str;
14053 vimconv_T vimconv;
14054#endif
14055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014056 rettv->v_type = VAR_STRING;
14057 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058
14059#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014060 str = get_tv_string(&argvars[0]);
14061 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14062 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014063 vimconv.vc_type = CONV_NONE;
14064 convert_setup(&vimconv, from, to);
14065
14066 /* If the encodings are equal, no conversion needed. */
14067 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014068 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014069 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014070 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071
14072 convert_setup(&vimconv, NULL, NULL);
14073 vim_free(from);
14074 vim_free(to);
14075#endif
14076}
14077
14078/*
14079 * "indent()" function
14080 */
14081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014082f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083{
14084 linenr_T lnum;
14085
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014086 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014087 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014088 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014089 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014090 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091}
14092
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014093/*
14094 * "index()" function
14095 */
14096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014097f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014098{
Bram Moolenaar33570922005-01-25 22:26:29 +000014099 list_T *l;
14100 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014101 long idx = 0;
14102 int ic = FALSE;
14103
14104 rettv->vval.v_number = -1;
14105 if (argvars[0].v_type != VAR_LIST)
14106 {
14107 EMSG(_(e_listreq));
14108 return;
14109 }
14110 l = argvars[0].vval.v_list;
14111 if (l != NULL)
14112 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014113 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014114 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014115 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014116 int error = FALSE;
14117
Bram Moolenaar758711c2005-02-02 23:11:38 +000014118 /* Start at specified item. Use the cached index that list_find()
14119 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014120 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014121 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014122 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014123 ic = get_tv_number_chk(&argvars[3], &error);
14124 if (error)
14125 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014126 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014127
Bram Moolenaar758711c2005-02-02 23:11:38 +000014128 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014129 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014130 {
14131 rettv->vval.v_number = idx;
14132 break;
14133 }
14134 }
14135}
14136
Bram Moolenaar071d4272004-06-13 20:20:40 +000014137static int inputsecret_flag = 0;
14138
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014139static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014140
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014142 * This function is used by f_input() and f_inputdialog() functions. The third
14143 * argument to f_input() specifies the type of completion to use at the
14144 * prompt. The third argument to f_inputdialog() specifies the value to return
14145 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014146 */
14147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014148get_user_input(
14149 typval_T *argvars,
14150 typval_T *rettv,
14151 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014152{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014153 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154 char_u *p = NULL;
14155 int c;
14156 char_u buf[NUMBUFLEN];
14157 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014158 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014159 int xp_type = EXPAND_NOTHING;
14160 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014162 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014163 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164
14165#ifdef NO_CONSOLE_INPUT
14166 /* While starting up, there is no place to enter text. */
14167 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014168 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169#endif
14170
14171 cmd_silent = FALSE; /* Want to see the prompt. */
14172 if (prompt != NULL)
14173 {
14174 /* Only the part of the message after the last NL is considered as
14175 * prompt for the command line */
14176 p = vim_strrchr(prompt, '\n');
14177 if (p == NULL)
14178 p = prompt;
14179 else
14180 {
14181 ++p;
14182 c = *p;
14183 *p = NUL;
14184 msg_start();
14185 msg_clr_eos();
14186 msg_puts_attr(prompt, echo_attr);
14187 msg_didout = FALSE;
14188 msg_starthere();
14189 *p = c;
14190 }
14191 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014192
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014193 if (argvars[1].v_type != VAR_UNKNOWN)
14194 {
14195 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14196 if (defstr != NULL)
14197 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014199 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014200 {
14201 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014202 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014203 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014204
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014205 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014206 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014207
Bram Moolenaar4463f292005-09-25 22:20:24 +000014208 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14209 if (xp_name == NULL)
14210 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014211
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014212 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014213
Bram Moolenaar4463f292005-09-25 22:20:24 +000014214 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14215 &xp_arg) == FAIL)
14216 return;
14217 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014218 }
14219
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014220 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014221 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014222 int save_ex_normal_busy = ex_normal_busy;
14223 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014224 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014225 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14226 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014227 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014228 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014229 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014230 && argvars[1].v_type != VAR_UNKNOWN
14231 && argvars[2].v_type != VAR_UNKNOWN)
14232 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14233 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014234
14235 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014237 /* since the user typed this, no need to wait for return */
14238 need_wait_return = FALSE;
14239 msg_didout = FALSE;
14240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241 cmd_silent = cmd_silent_save;
14242}
14243
14244/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014245 * "input()" function
14246 * Also handles inputsecret() when inputsecret is set.
14247 */
14248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014249f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014250{
14251 get_user_input(argvars, rettv, FALSE);
14252}
14253
14254/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255 * "inputdialog()" function
14256 */
14257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014258f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014259{
14260#if defined(FEAT_GUI_TEXTDIALOG)
14261 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14262 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14263 {
14264 char_u *message;
14265 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014266 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014267
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014268 message = get_tv_string_chk(&argvars[0]);
14269 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014270 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014271 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272 else
14273 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014274 if (message != NULL && defstr != NULL
14275 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014276 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014277 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278 else
14279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014280 if (message != NULL && defstr != NULL
14281 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014282 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014283 rettv->vval.v_string = vim_strsave(
14284 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014286 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014287 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014288 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289 }
14290 else
14291#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014292 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293}
14294
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014295/*
14296 * "inputlist()" function
14297 */
14298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014299f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014300{
14301 listitem_T *li;
14302 int selected;
14303 int mouse_used;
14304
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014305#ifdef NO_CONSOLE_INPUT
14306 /* While starting up, there is no place to enter text. */
14307 if (no_console_input())
14308 return;
14309#endif
14310 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14311 {
14312 EMSG2(_(e_listarg), "inputlist()");
14313 return;
14314 }
14315
14316 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014317 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014318 lines_left = Rows; /* avoid more prompt */
14319 msg_scroll = TRUE;
14320 msg_clr_eos();
14321
14322 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14323 {
14324 msg_puts(get_tv_string(&li->li_tv));
14325 msg_putchar('\n');
14326 }
14327
14328 /* Ask for choice. */
14329 selected = prompt_for_number(&mouse_used);
14330 if (mouse_used)
14331 selected -= lines_left;
14332
14333 rettv->vval.v_number = selected;
14334}
14335
14336
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14338
14339/*
14340 * "inputrestore()" function
14341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014343f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344{
14345 if (ga_userinput.ga_len > 0)
14346 {
14347 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14349 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014350 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351 }
14352 else if (p_verbose > 1)
14353 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014354 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014355 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014356 }
14357}
14358
14359/*
14360 * "inputsave()" function
14361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014363f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014365 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366 if (ga_grow(&ga_userinput, 1) == OK)
14367 {
14368 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14369 + ga_userinput.ga_len);
14370 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014371 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372 }
14373 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014374 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375}
14376
14377/*
14378 * "inputsecret()" function
14379 */
14380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014381f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382{
14383 ++cmdline_star;
14384 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014385 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386 --cmdline_star;
14387 --inputsecret_flag;
14388}
14389
14390/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014391 * "insert()" function
14392 */
14393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014394f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014395{
14396 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014397 listitem_T *item;
14398 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014399 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014400
14401 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014402 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014403 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014404 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014405 {
14406 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014407 before = get_tv_number_chk(&argvars[2], &error);
14408 if (error)
14409 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014410
Bram Moolenaar758711c2005-02-02 23:11:38 +000014411 if (before == l->lv_len)
14412 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014413 else
14414 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014415 item = list_find(l, before);
14416 if (item == NULL)
14417 {
14418 EMSGN(_(e_listidx), before);
14419 l = NULL;
14420 }
14421 }
14422 if (l != NULL)
14423 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014424 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014425 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014426 }
14427 }
14428}
14429
14430/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014431 * "invert(expr)" function
14432 */
14433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014434f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014435{
14436 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14437}
14438
14439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440 * "isdirectory()" function
14441 */
14442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014443f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014445 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446}
14447
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014448/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014449 * "islocked()" function
14450 */
14451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014452f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014453{
14454 lval_T lv;
14455 char_u *end;
14456 dictitem_T *di;
14457
14458 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014459 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14460 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014461 if (end != NULL && lv.ll_name != NULL)
14462 {
14463 if (*end != NUL)
14464 EMSG(_(e_trailing));
14465 else
14466 {
14467 if (lv.ll_tv == NULL)
14468 {
14469 if (check_changedtick(lv.ll_name))
14470 rettv->vval.v_number = 1; /* always locked */
14471 else
14472 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014473 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014474 if (di != NULL)
14475 {
14476 /* Consider a variable locked when:
14477 * 1. the variable itself is locked
14478 * 2. the value of the variable is locked.
14479 * 3. the List or Dict value is locked.
14480 */
14481 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14482 || tv_islocked(&di->di_tv));
14483 }
14484 }
14485 }
14486 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014487 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014488 else if (lv.ll_newkey != NULL)
14489 EMSG2(_(e_dictkey), lv.ll_newkey);
14490 else if (lv.ll_list != NULL)
14491 /* List item. */
14492 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14493 else
14494 /* Dictionary item. */
14495 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14496 }
14497 }
14498
14499 clear_lval(&lv);
14500}
14501
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014502#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14503/*
14504 * "isnan()" function
14505 */
14506 static void
14507f_isnan(typval_T *argvars, typval_T *rettv)
14508{
14509 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14510 && isnan(argvars[0].vval.v_float);
14511}
14512#endif
14513
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014514static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014515
14516/*
14517 * Turn a dict into a list:
14518 * "what" == 0: list of keys
14519 * "what" == 1: list of values
14520 * "what" == 2: list of items
14521 */
14522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014523dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014524{
Bram Moolenaar33570922005-01-25 22:26:29 +000014525 list_T *l2;
14526 dictitem_T *di;
14527 hashitem_T *hi;
14528 listitem_T *li;
14529 listitem_T *li2;
14530 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014531 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014532
Bram Moolenaar8c711452005-01-14 21:53:12 +000014533 if (argvars[0].v_type != VAR_DICT)
14534 {
14535 EMSG(_(e_dictreq));
14536 return;
14537 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014538 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014539 return;
14540
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014541 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014542 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014543
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014544 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014545 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014546 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014547 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014548 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014549 --todo;
14550 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014551
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014552 li = listitem_alloc();
14553 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014554 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014555 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014556
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014557 if (what == 0)
14558 {
14559 /* keys() */
14560 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014561 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014562 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14563 }
14564 else if (what == 1)
14565 {
14566 /* values() */
14567 copy_tv(&di->di_tv, &li->li_tv);
14568 }
14569 else
14570 {
14571 /* items() */
14572 l2 = list_alloc();
14573 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014574 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014575 li->li_tv.vval.v_list = l2;
14576 if (l2 == NULL)
14577 break;
14578 ++l2->lv_refcount;
14579
14580 li2 = listitem_alloc();
14581 if (li2 == NULL)
14582 break;
14583 list_append(l2, li2);
14584 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014585 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014586 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14587
14588 li2 = listitem_alloc();
14589 if (li2 == NULL)
14590 break;
14591 list_append(l2, li2);
14592 copy_tv(&di->di_tv, &li2->li_tv);
14593 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014594 }
14595 }
14596}
14597
14598/*
14599 * "items(dict)" function
14600 */
14601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014602f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014603{
14604 dict_list(argvars, rettv, 2);
14605}
14606
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014607#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014608/*
14609 * Get the job from the argument.
14610 * Returns NULL if the job is invalid.
14611 */
14612 static job_T *
14613get_job_arg(typval_T *tv)
14614{
14615 job_T *job;
14616
14617 if (tv->v_type != VAR_JOB)
14618 {
14619 EMSG2(_(e_invarg2), get_tv_string(tv));
14620 return NULL;
14621 }
14622 job = tv->vval.v_job;
14623
14624 if (job == NULL)
14625 EMSG(_("E916: not a valid job"));
14626 return job;
14627}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014628
Bram Moolenaar835dc632016-02-07 14:27:38 +010014629/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014630 * "job_getchannel()" function
14631 */
14632 static void
14633f_job_getchannel(typval_T *argvars, typval_T *rettv)
14634{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014635 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014636
Bram Moolenaar65edff82016-02-21 16:40:11 +010014637 if (job != NULL)
14638 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014639 rettv->v_type = VAR_CHANNEL;
14640 rettv->vval.v_channel = job->jv_channel;
14641 if (job->jv_channel != NULL)
14642 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014643 }
14644}
14645
14646/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010014647 * "job_info()" function
14648 */
14649 static void
14650f_job_info(typval_T *argvars, typval_T *rettv)
14651{
14652 job_T *job = get_job_arg(&argvars[0]);
14653
14654 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
14655 job_info(job, rettv->vval.v_dict);
14656}
14657
14658/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014659 * "job_setoptions()" function
14660 */
14661 static void
14662f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14663{
14664 job_T *job = get_job_arg(&argvars[0]);
14665 jobopt_T opt;
14666
14667 if (job == NULL)
14668 return;
14669 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014670 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014671 return;
14672 job_set_options(job, &opt);
14673}
14674
14675/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014676 * "job_start()" function
14677 */
14678 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010014679f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014680{
Bram Moolenaar835dc632016-02-07 14:27:38 +010014681 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014682 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014683}
14684
14685/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014686 * "job_status()" function
14687 */
14688 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014689f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014690{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014691 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014692
Bram Moolenaar65edff82016-02-21 16:40:11 +010014693 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014694 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010014695 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010014696 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010014697 }
14698}
14699
14700/*
14701 * "job_stop()" function
14702 */
14703 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014704f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014705{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014706 job_T *job = get_job_arg(&argvars[0]);
14707
14708 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014709 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014710}
14711#endif
14712
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014714 * "join()" function
14715 */
14716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014717f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014718{
14719 garray_T ga;
14720 char_u *sep;
14721
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014722 if (argvars[0].v_type != VAR_LIST)
14723 {
14724 EMSG(_(e_listreq));
14725 return;
14726 }
14727 if (argvars[0].vval.v_list == NULL)
14728 return;
14729 if (argvars[1].v_type == VAR_UNKNOWN)
14730 sep = (char_u *)" ";
14731 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014732 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014733
14734 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014735
14736 if (sep != NULL)
14737 {
14738 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014739 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014740 ga_append(&ga, NUL);
14741 rettv->vval.v_string = (char_u *)ga.ga_data;
14742 }
14743 else
14744 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014745}
14746
14747/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014748 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014749 */
14750 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014751f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014752{
14753 js_read_T reader;
14754
14755 reader.js_buf = get_tv_string(&argvars[0]);
14756 reader.js_fill = NULL;
14757 reader.js_used = 0;
14758 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14759 EMSG(_(e_invarg));
14760}
14761
14762/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014763 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014764 */
14765 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014766f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014767{
14768 rettv->v_type = VAR_STRING;
14769 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14770}
14771
14772/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014773 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014774 */
14775 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014776f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014777{
14778 js_read_T reader;
14779
14780 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014781 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014782 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014783 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014784 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014785}
14786
14787/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014788 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014789 */
14790 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014791f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014792{
14793 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014794 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014795}
14796
14797/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014798 * "keys()" function
14799 */
14800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014801f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014802{
14803 dict_list(argvars, rettv, 0);
14804}
14805
14806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014807 * "last_buffer_nr()" function.
14808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014810f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014811{
14812 int n = 0;
14813 buf_T *buf;
14814
14815 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14816 if (n < buf->b_fnum)
14817 n = buf->b_fnum;
14818
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014819 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014820}
14821
14822/*
14823 * "len()" function
14824 */
14825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014826f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014827{
14828 switch (argvars[0].v_type)
14829 {
14830 case VAR_STRING:
14831 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014832 rettv->vval.v_number = (varnumber_T)STRLEN(
14833 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014834 break;
14835 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014836 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014837 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014838 case VAR_DICT:
14839 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14840 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014841 case VAR_UNKNOWN:
14842 case VAR_SPECIAL:
14843 case VAR_FLOAT:
14844 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010014845 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014846 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014847 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014848 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014849 break;
14850 }
14851}
14852
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014853static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014854
14855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014856libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014857{
14858#ifdef FEAT_LIBCALL
14859 char_u *string_in;
14860 char_u **string_result;
14861 int nr_result;
14862#endif
14863
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014864 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014865 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014866 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014867
14868 if (check_restricted() || check_secure())
14869 return;
14870
14871#ifdef FEAT_LIBCALL
14872 /* The first two args must be strings, otherwise its meaningless */
14873 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14874 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014875 string_in = NULL;
14876 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014877 string_in = argvars[2].vval.v_string;
14878 if (type == VAR_NUMBER)
14879 string_result = NULL;
14880 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014881 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014882 if (mch_libcall(argvars[0].vval.v_string,
14883 argvars[1].vval.v_string,
14884 string_in,
14885 argvars[2].vval.v_number,
14886 string_result,
14887 &nr_result) == OK
14888 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014889 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014890 }
14891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014892}
14893
14894/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014895 * "libcall()" function
14896 */
14897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014898f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014899{
14900 libcall_common(argvars, rettv, VAR_STRING);
14901}
14902
14903/*
14904 * "libcallnr()" function
14905 */
14906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014907f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014908{
14909 libcall_common(argvars, rettv, VAR_NUMBER);
14910}
14911
14912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014913 * "line(string)" function
14914 */
14915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014916f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014917{
14918 linenr_T lnum = 0;
14919 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014920 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014921
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014922 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014923 if (fp != NULL)
14924 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014925 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014926}
14927
14928/*
14929 * "line2byte(lnum)" function
14930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014932f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014933{
14934#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014935 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014936#else
14937 linenr_T lnum;
14938
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014939 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014940 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014941 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014942 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014943 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14944 if (rettv->vval.v_number >= 0)
14945 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014946#endif
14947}
14948
14949/*
14950 * "lispindent(lnum)" function
14951 */
14952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014953f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014954{
14955#ifdef FEAT_LISP
14956 pos_T pos;
14957 linenr_T lnum;
14958
14959 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014960 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14962 {
14963 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014964 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014965 curwin->w_cursor = pos;
14966 }
14967 else
14968#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014969 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014970}
14971
14972/*
14973 * "localtime()" function
14974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014976f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014977{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014978 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014979}
14980
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014981static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014982
14983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014984get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014985{
14986 char_u *keys;
14987 char_u *which;
14988 char_u buf[NUMBUFLEN];
14989 char_u *keys_buf = NULL;
14990 char_u *rhs;
14991 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014992 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014993 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014994 mapblock_T *mp;
14995 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014996
14997 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014998 rettv->v_type = VAR_STRING;
14999 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015000
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015001 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015002 if (*keys == NUL)
15003 return;
15004
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015005 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015006 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015007 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015008 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015009 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015010 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015011 if (argvars[3].v_type != VAR_UNKNOWN)
15012 get_dict = get_tv_number(&argvars[3]);
15013 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015 else
15016 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015017 if (which == NULL)
15018 return;
15019
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020 mode = get_map_mode(&which, 0);
15021
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015022 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015023 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015024 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015025
15026 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015027 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015028 /* Return a string. */
15029 if (rhs != NULL)
15030 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015031
Bram Moolenaarbd743252010-10-20 21:23:33 +020015032 }
15033 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15034 {
15035 /* Return a dictionary. */
15036 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15037 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15038 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015039
Bram Moolenaarbd743252010-10-20 21:23:33 +020015040 dict_add_nr_str(dict, "lhs", 0L, lhs);
15041 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15042 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15043 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15044 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15045 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15046 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015047 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015048 dict_add_nr_str(dict, "mode", 0L, mapmode);
15049
15050 vim_free(lhs);
15051 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015052 }
15053}
15054
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015055#ifdef FEAT_FLOAT
15056/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015057 * "log()" function
15058 */
15059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015060f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015061{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015062 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015063
15064 rettv->v_type = VAR_FLOAT;
15065 if (get_float_arg(argvars, &f) == OK)
15066 rettv->vval.v_float = log(f);
15067 else
15068 rettv->vval.v_float = 0.0;
15069}
15070
15071/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015072 * "log10()" function
15073 */
15074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015075f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015076{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015077 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015078
15079 rettv->v_type = VAR_FLOAT;
15080 if (get_float_arg(argvars, &f) == OK)
15081 rettv->vval.v_float = log10(f);
15082 else
15083 rettv->vval.v_float = 0.0;
15084}
15085#endif
15086
Bram Moolenaar1dced572012-04-05 16:54:08 +020015087#ifdef FEAT_LUA
15088/*
15089 * "luaeval()" function
15090 */
15091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015092f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015093{
15094 char_u *str;
15095 char_u buf[NUMBUFLEN];
15096
15097 str = get_tv_string_buf(&argvars[0], buf);
15098 do_luaeval(str, argvars + 1, rettv);
15099}
15100#endif
15101
Bram Moolenaar071d4272004-06-13 20:20:40 +000015102/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015103 * "map()" function
15104 */
15105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015106f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015107{
15108 filter_map(argvars, rettv, TRUE);
15109}
15110
15111/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015112 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113 */
15114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015115f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015116{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015117 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015118}
15119
15120/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015121 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015122 */
15123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015124f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015125{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015126 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015127}
15128
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015129static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015130
15131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015132find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015133{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015134 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015135 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015136 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015137 char_u *pat;
15138 regmatch_T regmatch;
15139 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015140 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141 char_u *save_cpo;
15142 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015143 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015144 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015145 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015146 list_T *l = NULL;
15147 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015148 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015149 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015150
15151 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15152 save_cpo = p_cpo;
15153 p_cpo = (char_u *)"";
15154
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015155 rettv->vval.v_number = -1;
15156 if (type == 3)
15157 {
15158 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015159 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015160 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015161 }
15162 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015163 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015164 rettv->v_type = VAR_STRING;
15165 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015167
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015168 if (argvars[0].v_type == VAR_LIST)
15169 {
15170 if ((l = argvars[0].vval.v_list) == NULL)
15171 goto theend;
15172 li = l->lv_first;
15173 }
15174 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015175 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015176 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015177 len = (long)STRLEN(str);
15178 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015180 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15181 if (pat == NULL)
15182 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015183
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015184 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015185 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015186 int error = FALSE;
15187
15188 start = get_tv_number_chk(&argvars[2], &error);
15189 if (error)
15190 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015191 if (l != NULL)
15192 {
15193 li = list_find(l, start);
15194 if (li == NULL)
15195 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015196 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015197 }
15198 else
15199 {
15200 if (start < 0)
15201 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015202 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015203 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015204 /* When "count" argument is there ignore matches before "start",
15205 * otherwise skip part of the string. Differs when pattern is "^"
15206 * or "\<". */
15207 if (argvars[3].v_type != VAR_UNKNOWN)
15208 startcol = start;
15209 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015210 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015211 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015212 len -= start;
15213 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015214 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015215
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015216 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015217 nth = get_tv_number_chk(&argvars[3], &error);
15218 if (error)
15219 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015220 }
15221
15222 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15223 if (regmatch.regprog != NULL)
15224 {
15225 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015226
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015227 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015228 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015229 if (l != NULL)
15230 {
15231 if (li == NULL)
15232 {
15233 match = FALSE;
15234 break;
15235 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015236 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015237 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015238 if (str == NULL)
15239 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015240 }
15241
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015242 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015243
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015244 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015245 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015246 if (l == NULL && !match)
15247 break;
15248
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015249 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015250 if (l != NULL)
15251 {
15252 li = li->li_next;
15253 ++idx;
15254 }
15255 else
15256 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015257#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015258 startcol = (colnr_T)(regmatch.startp[0]
15259 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015260#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015261 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015262#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015263 if (startcol > (colnr_T)len
15264 || str + startcol <= regmatch.startp[0])
15265 {
15266 match = FALSE;
15267 break;
15268 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015269 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015270 }
15271
15272 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015273 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015274 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015275 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015276 int i;
15277
15278 /* return list with matched string and submatches */
15279 for (i = 0; i < NSUBEXP; ++i)
15280 {
15281 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015282 {
15283 if (list_append_string(rettv->vval.v_list,
15284 (char_u *)"", 0) == FAIL)
15285 break;
15286 }
15287 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015288 regmatch.startp[i],
15289 (int)(regmatch.endp[i] - regmatch.startp[i]))
15290 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015291 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015292 }
15293 }
15294 else if (type == 2)
15295 {
15296 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015297 if (l != NULL)
15298 copy_tv(&li->li_tv, rettv);
15299 else
15300 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015301 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015302 }
15303 else if (l != NULL)
15304 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015305 else
15306 {
15307 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015308 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015309 (varnumber_T)(regmatch.startp[0] - str);
15310 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015311 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015312 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015313 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314 }
15315 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015316 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015317 }
15318
15319theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015320 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015321 p_cpo = save_cpo;
15322}
15323
15324/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015325 * "match()" function
15326 */
15327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015328f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015329{
15330 find_some_match(argvars, rettv, 1);
15331}
15332
15333/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015334 * "matchadd()" function
15335 */
15336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015337f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015338{
15339#ifdef FEAT_SEARCH_EXTRA
15340 char_u buf[NUMBUFLEN];
15341 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15342 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15343 int prio = 10; /* default priority */
15344 int id = -1;
15345 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015346 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015347
15348 rettv->vval.v_number = -1;
15349
15350 if (grp == NULL || pat == NULL)
15351 return;
15352 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015353 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015354 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015355 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015356 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015357 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015358 if (argvars[4].v_type != VAR_UNKNOWN)
15359 {
15360 if (argvars[4].v_type != VAR_DICT)
15361 {
15362 EMSG(_(e_dictreq));
15363 return;
15364 }
15365 if (dict_find(argvars[4].vval.v_dict,
15366 (char_u *)"conceal", -1) != NULL)
15367 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15368 (char_u *)"conceal", FALSE);
15369 }
15370 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015371 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015372 if (error == TRUE)
15373 return;
15374 if (id >= 1 && id <= 3)
15375 {
15376 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15377 return;
15378 }
15379
Bram Moolenaar6561d522015-07-21 15:48:27 +020015380 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15381 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015382#endif
15383}
15384
15385/*
15386 * "matchaddpos()" function
15387 */
15388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015389f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015390{
15391#ifdef FEAT_SEARCH_EXTRA
15392 char_u buf[NUMBUFLEN];
15393 char_u *group;
15394 int prio = 10;
15395 int id = -1;
15396 int error = FALSE;
15397 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015398 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015399
15400 rettv->vval.v_number = -1;
15401
15402 group = get_tv_string_buf_chk(&argvars[0], buf);
15403 if (group == NULL)
15404 return;
15405
15406 if (argvars[1].v_type != VAR_LIST)
15407 {
15408 EMSG2(_(e_listarg), "matchaddpos()");
15409 return;
15410 }
15411 l = argvars[1].vval.v_list;
15412 if (l == NULL)
15413 return;
15414
15415 if (argvars[2].v_type != VAR_UNKNOWN)
15416 {
15417 prio = get_tv_number_chk(&argvars[2], &error);
15418 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015419 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015420 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015421 if (argvars[4].v_type != VAR_UNKNOWN)
15422 {
15423 if (argvars[4].v_type != VAR_DICT)
15424 {
15425 EMSG(_(e_dictreq));
15426 return;
15427 }
15428 if (dict_find(argvars[4].vval.v_dict,
15429 (char_u *)"conceal", -1) != NULL)
15430 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15431 (char_u *)"conceal", FALSE);
15432 }
15433 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015434 }
15435 if (error == TRUE)
15436 return;
15437
15438 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15439 if (id == 1 || id == 2)
15440 {
15441 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15442 return;
15443 }
15444
Bram Moolenaar6561d522015-07-21 15:48:27 +020015445 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15446 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015447#endif
15448}
15449
15450/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015451 * "matcharg()" function
15452 */
15453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015454f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015455{
15456 if (rettv_list_alloc(rettv) == OK)
15457 {
15458#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015459 int id = get_tv_number(&argvars[0]);
15460 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015461
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015462 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015463 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015464 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15465 {
15466 list_append_string(rettv->vval.v_list,
15467 syn_id2name(m->hlg_id), -1);
15468 list_append_string(rettv->vval.v_list, m->pattern, -1);
15469 }
15470 else
15471 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015472 list_append_string(rettv->vval.v_list, NULL, -1);
15473 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015474 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015475 }
15476#endif
15477 }
15478}
15479
15480/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015481 * "matchdelete()" function
15482 */
15483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015484f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015485{
15486#ifdef FEAT_SEARCH_EXTRA
15487 rettv->vval.v_number = match_delete(curwin,
15488 (int)get_tv_number(&argvars[0]), TRUE);
15489#endif
15490}
15491
15492/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015493 * "matchend()" function
15494 */
15495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015496f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015497{
15498 find_some_match(argvars, rettv, 0);
15499}
15500
15501/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015502 * "matchlist()" function
15503 */
15504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015505f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015506{
15507 find_some_match(argvars, rettv, 3);
15508}
15509
15510/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015511 * "matchstr()" function
15512 */
15513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015514f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015515{
15516 find_some_match(argvars, rettv, 2);
15517}
15518
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015519static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015520
15521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015522max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015523{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015524 long n = 0;
15525 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015526 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015527
15528 if (argvars[0].v_type == VAR_LIST)
15529 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015530 list_T *l;
15531 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015532
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015533 l = argvars[0].vval.v_list;
15534 if (l != NULL)
15535 {
15536 li = l->lv_first;
15537 if (li != NULL)
15538 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015539 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015540 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015541 {
15542 li = li->li_next;
15543 if (li == NULL)
15544 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015545 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015546 if (domax ? i > n : i < n)
15547 n = i;
15548 }
15549 }
15550 }
15551 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015552 else if (argvars[0].v_type == VAR_DICT)
15553 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015554 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015555 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015556 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015557 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015558
15559 d = argvars[0].vval.v_dict;
15560 if (d != NULL)
15561 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015562 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015563 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015564 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015565 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015566 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015567 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015568 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015569 if (first)
15570 {
15571 n = i;
15572 first = FALSE;
15573 }
15574 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015575 n = i;
15576 }
15577 }
15578 }
15579 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015580 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015581 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015582 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015583}
15584
15585/*
15586 * "max()" function
15587 */
15588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015589f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015590{
15591 max_min(argvars, rettv, TRUE);
15592}
15593
15594/*
15595 * "min()" function
15596 */
15597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015598f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015599{
15600 max_min(argvars, rettv, FALSE);
15601}
15602
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015603static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015604
15605/*
15606 * Create the directory in which "dir" is located, and higher levels when
15607 * needed.
15608 */
15609 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015610mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015611{
15612 char_u *p;
15613 char_u *updir;
15614 int r = FAIL;
15615
15616 /* Get end of directory name in "dir".
15617 * We're done when it's "/" or "c:/". */
15618 p = gettail_sep(dir);
15619 if (p <= get_past_head(dir))
15620 return OK;
15621
15622 /* If the directory exists we're done. Otherwise: create it.*/
15623 updir = vim_strnsave(dir, (int)(p - dir));
15624 if (updir == NULL)
15625 return FAIL;
15626 if (mch_isdir(updir))
15627 r = OK;
15628 else if (mkdir_recurse(updir, prot) == OK)
15629 r = vim_mkdir_emsg(updir, prot);
15630 vim_free(updir);
15631 return r;
15632}
15633
15634#ifdef vim_mkdir
15635/*
15636 * "mkdir()" function
15637 */
15638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015639f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015640{
15641 char_u *dir;
15642 char_u buf[NUMBUFLEN];
15643 int prot = 0755;
15644
15645 rettv->vval.v_number = FAIL;
15646 if (check_restricted() || check_secure())
15647 return;
15648
15649 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015650 if (*dir == NUL)
15651 rettv->vval.v_number = FAIL;
15652 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015653 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015654 if (*gettail(dir) == NUL)
15655 /* remove trailing slashes */
15656 *gettail_sep(dir) = NUL;
15657
15658 if (argvars[1].v_type != VAR_UNKNOWN)
15659 {
15660 if (argvars[2].v_type != VAR_UNKNOWN)
15661 prot = get_tv_number_chk(&argvars[2], NULL);
15662 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15663 mkdir_recurse(dir, prot);
15664 }
15665 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015666 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015667}
15668#endif
15669
Bram Moolenaar0d660222005-01-07 21:51:51 +000015670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671 * "mode()" function
15672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015674f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015675{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015676 char_u buf[3];
15677
15678 buf[1] = NUL;
15679 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681 if (VIsual_active)
15682 {
15683 if (VIsual_select)
15684 buf[0] = VIsual_mode + 's' - 'v';
15685 else
15686 buf[0] = VIsual_mode;
15687 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015688 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015689 || State == CONFIRM)
15690 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015691 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015692 if (State == ASKMORE)
15693 buf[1] = 'm';
15694 else if (State == CONFIRM)
15695 buf[1] = '?';
15696 }
15697 else if (State == EXTERNCMD)
15698 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015699 else if (State & INSERT)
15700 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015701#ifdef FEAT_VREPLACE
15702 if (State & VREPLACE_FLAG)
15703 {
15704 buf[0] = 'R';
15705 buf[1] = 'v';
15706 }
15707 else
15708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709 if (State & REPLACE_FLAG)
15710 buf[0] = 'R';
15711 else
15712 buf[0] = 'i';
15713 }
15714 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015715 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015717 if (exmode_active)
15718 buf[1] = 'v';
15719 }
15720 else if (exmode_active)
15721 {
15722 buf[0] = 'c';
15723 buf[1] = 'e';
15724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015726 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015727 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015728 if (finish_op)
15729 buf[1] = 'o';
15730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015731
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015732 /* Clear out the minor mode when the argument is not a non-zero number or
15733 * non-empty string. */
15734 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015735 buf[1] = NUL;
15736
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015737 rettv->vval.v_string = vim_strsave(buf);
15738 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739}
15740
Bram Moolenaar429fa852013-04-15 12:27:36 +020015741#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015742/*
15743 * "mzeval()" function
15744 */
15745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015746f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015747{
15748 char_u *str;
15749 char_u buf[NUMBUFLEN];
15750
15751 str = get_tv_string_buf(&argvars[0], buf);
15752 do_mzeval(str, rettv);
15753}
Bram Moolenaar75676462013-01-30 14:55:42 +010015754
15755 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015756mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015757{
15758 typval_T argvars[3];
15759
15760 argvars[0].v_type = VAR_STRING;
15761 argvars[0].vval.v_string = name;
15762 copy_tv(args, &argvars[1]);
15763 argvars[2].v_type = VAR_UNKNOWN;
15764 f_call(argvars, rettv);
15765 clear_tv(&argvars[1]);
15766}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015767#endif
15768
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015770 * "nextnonblank()" function
15771 */
15772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015773f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015774{
15775 linenr_T lnum;
15776
15777 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015779 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015780 {
15781 lnum = 0;
15782 break;
15783 }
15784 if (*skipwhite(ml_get(lnum)) != NUL)
15785 break;
15786 }
15787 rettv->vval.v_number = lnum;
15788}
15789
15790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015791 * "nr2char()" function
15792 */
15793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015794f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015795{
15796 char_u buf[NUMBUFLEN];
15797
15798#ifdef FEAT_MBYTE
15799 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015800 {
15801 int utf8 = 0;
15802
15803 if (argvars[1].v_type != VAR_UNKNOWN)
15804 utf8 = get_tv_number_chk(&argvars[1], NULL);
15805 if (utf8)
15806 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15807 else
15808 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015810 else
15811#endif
15812 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015813 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015814 buf[1] = NUL;
15815 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015816 rettv->v_type = VAR_STRING;
15817 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015818}
15819
15820/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015821 * "or(expr, expr)" function
15822 */
15823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015824f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015825{
15826 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15827 | get_tv_number_chk(&argvars[1], NULL);
15828}
15829
15830/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015831 * "pathshorten()" function
15832 */
15833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015834f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015835{
15836 char_u *p;
15837
15838 rettv->v_type = VAR_STRING;
15839 p = get_tv_string_chk(&argvars[0]);
15840 if (p == NULL)
15841 rettv->vval.v_string = NULL;
15842 else
15843 {
15844 p = vim_strsave(p);
15845 rettv->vval.v_string = p;
15846 if (p != NULL)
15847 shorten_dir(p);
15848 }
15849}
15850
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015851#ifdef FEAT_PERL
15852/*
15853 * "perleval()" function
15854 */
15855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015856f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015857{
15858 char_u *str;
15859 char_u buf[NUMBUFLEN];
15860
15861 str = get_tv_string_buf(&argvars[0], buf);
15862 do_perleval(str, rettv);
15863}
15864#endif
15865
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015866#ifdef FEAT_FLOAT
15867/*
15868 * "pow()" function
15869 */
15870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015871f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015872{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015873 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015874
15875 rettv->v_type = VAR_FLOAT;
15876 if (get_float_arg(argvars, &fx) == OK
15877 && get_float_arg(&argvars[1], &fy) == OK)
15878 rettv->vval.v_float = pow(fx, fy);
15879 else
15880 rettv->vval.v_float = 0.0;
15881}
15882#endif
15883
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015884/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015885 * "prevnonblank()" function
15886 */
15887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015888f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015889{
15890 linenr_T lnum;
15891
15892 lnum = get_tv_lnum(argvars);
15893 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15894 lnum = 0;
15895 else
15896 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15897 --lnum;
15898 rettv->vval.v_number = lnum;
15899}
15900
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015901/* This dummy va_list is here because:
15902 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15903 * - locally in the function results in a "used before set" warning
15904 * - using va_start() to initialize it gives "function with fixed args" error */
15905static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015906
Bram Moolenaar8c711452005-01-14 21:53:12 +000015907/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015908 * "printf()" function
15909 */
15910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015911f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015912{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015913 char_u buf[NUMBUFLEN];
15914 int len;
15915 char_u *s;
15916 int saved_did_emsg = did_emsg;
15917 char *fmt;
15918
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015919 rettv->v_type = VAR_STRING;
15920 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015921
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015922 /* Get the required length, allocate the buffer and do it for real. */
15923 did_emsg = FALSE;
15924 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15925 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15926 if (!did_emsg)
15927 {
15928 s = alloc(len + 1);
15929 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015930 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015931 rettv->vval.v_string = s;
15932 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015933 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015934 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015935 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015936}
15937
15938/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015939 * "pumvisible()" function
15940 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015942f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015943{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015944#ifdef FEAT_INS_EXPAND
15945 if (pum_visible())
15946 rettv->vval.v_number = 1;
15947#endif
15948}
15949
Bram Moolenaardb913952012-06-29 12:54:53 +020015950#ifdef FEAT_PYTHON3
15951/*
15952 * "py3eval()" function
15953 */
15954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015955f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015956{
15957 char_u *str;
15958 char_u buf[NUMBUFLEN];
15959
15960 str = get_tv_string_buf(&argvars[0], buf);
15961 do_py3eval(str, rettv);
15962}
15963#endif
15964
15965#ifdef FEAT_PYTHON
15966/*
15967 * "pyeval()" function
15968 */
15969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015970f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015971{
15972 char_u *str;
15973 char_u buf[NUMBUFLEN];
15974
15975 str = get_tv_string_buf(&argvars[0], buf);
15976 do_pyeval(str, rettv);
15977}
15978#endif
15979
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015980/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015981 * "range()" function
15982 */
15983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015984f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015985{
15986 long start;
15987 long end;
15988 long stride = 1;
15989 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015990 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015991
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015992 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015993 if (argvars[1].v_type == VAR_UNKNOWN)
15994 {
15995 end = start - 1;
15996 start = 0;
15997 }
15998 else
15999 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016000 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016001 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016002 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016003 }
16004
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016005 if (error)
16006 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016007 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016008 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016009 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016010 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016011 else
16012 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016013 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016014 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016015 if (list_append_number(rettv->vval.v_list,
16016 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016017 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016018 }
16019}
16020
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016021/*
16022 * "readfile()" function
16023 */
16024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016025f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016026{
16027 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016028 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016029 char_u *fname;
16030 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016031 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16032 int io_size = sizeof(buf);
16033 int readlen; /* size of last fread() */
16034 char_u *prev = NULL; /* previously read bytes, if any */
16035 long prevlen = 0; /* length of data in prev */
16036 long prevsize = 0; /* size of prev buffer */
16037 long maxline = MAXLNUM;
16038 long cnt = 0;
16039 char_u *p; /* position in buf */
16040 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016041
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016042 if (argvars[1].v_type != VAR_UNKNOWN)
16043 {
16044 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16045 binary = TRUE;
16046 if (argvars[2].v_type != VAR_UNKNOWN)
16047 maxline = get_tv_number(&argvars[2]);
16048 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016049
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016050 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016051 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016052
16053 /* Always open the file in binary mode, library functions have a mind of
16054 * their own about CR-LF conversion. */
16055 fname = get_tv_string(&argvars[0]);
16056 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16057 {
16058 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16059 return;
16060 }
16061
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016062 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016063 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016064 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016065
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016066 /* This for loop processes what was read, but is also entered at end
16067 * of file so that either:
16068 * - an incomplete line gets written
16069 * - a "binary" file gets an empty line at the end if it ends in a
16070 * newline. */
16071 for (p = buf, start = buf;
16072 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16073 ++p)
16074 {
16075 if (*p == '\n' || readlen <= 0)
16076 {
16077 listitem_T *li;
16078 char_u *s = NULL;
16079 long_u len = p - start;
16080
16081 /* Finished a line. Remove CRs before NL. */
16082 if (readlen > 0 && !binary)
16083 {
16084 while (len > 0 && start[len - 1] == '\r')
16085 --len;
16086 /* removal may cross back to the "prev" string */
16087 if (len == 0)
16088 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16089 --prevlen;
16090 }
16091 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016092 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016093 else
16094 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016095 /* Change "prev" buffer to be the right size. This way
16096 * the bytes are only copied once, and very long lines are
16097 * allocated only once. */
16098 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016099 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016100 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016101 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016102 prev = NULL; /* the list will own the string */
16103 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016104 }
16105 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016106 if (s == NULL)
16107 {
16108 do_outofmem_msg((long_u) prevlen + len + 1);
16109 failed = TRUE;
16110 break;
16111 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016112
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016113 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016114 {
16115 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016116 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016117 break;
16118 }
16119 li->li_tv.v_type = VAR_STRING;
16120 li->li_tv.v_lock = 0;
16121 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016122 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016123
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016124 start = p + 1; /* step over newline */
16125 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016126 break;
16127 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016128 else if (*p == NUL)
16129 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016130#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016131 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16132 * when finding the BF and check the previous two bytes. */
16133 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016134 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016135 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16136 * + 1, these may be in the "prev" string. */
16137 char_u back1 = p >= buf + 1 ? p[-1]
16138 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16139 char_u back2 = p >= buf + 2 ? p[-2]
16140 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16141 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016142
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016143 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016144 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016145 char_u *dest = p - 2;
16146
16147 /* Usually a BOM is at the beginning of a file, and so at
16148 * the beginning of a line; then we can just step over it.
16149 */
16150 if (start == dest)
16151 start = p + 1;
16152 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016153 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016154 /* have to shuffle buf to close gap */
16155 int adjust_prevlen = 0;
16156
16157 if (dest < buf)
16158 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016159 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016160 dest = buf;
16161 }
16162 if (readlen > p - buf + 1)
16163 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16164 readlen -= 3 - adjust_prevlen;
16165 prevlen -= adjust_prevlen;
16166 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016167 }
16168 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016169 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016170#endif
16171 } /* for */
16172
16173 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16174 break;
16175 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016176 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016177 /* There's part of a line in buf, store it in "prev". */
16178 if (p - start + prevlen >= prevsize)
16179 {
16180 /* need bigger "prev" buffer */
16181 char_u *newprev;
16182
16183 /* A common use case is ordinary text files and "prev" gets a
16184 * fragment of a line, so the first allocation is made
16185 * small, to avoid repeatedly 'allocing' large and
16186 * 'reallocing' small. */
16187 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016188 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016189 else
16190 {
16191 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016192 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016193 prevsize = grow50pc > growmin ? grow50pc : growmin;
16194 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016195 newprev = prev == NULL ? alloc(prevsize)
16196 : vim_realloc(prev, prevsize);
16197 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016198 {
16199 do_outofmem_msg((long_u)prevsize);
16200 failed = TRUE;
16201 break;
16202 }
16203 prev = newprev;
16204 }
16205 /* Add the line part to end of "prev". */
16206 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016207 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016208 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016209 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016210
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016211 /*
16212 * For a negative line count use only the lines at the end of the file,
16213 * free the rest.
16214 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016215 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016216 while (cnt > -maxline)
16217 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016218 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016219 --cnt;
16220 }
16221
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016222 if (failed)
16223 {
16224 list_free(rettv->vval.v_list, TRUE);
16225 /* readfile doc says an empty list is returned on error */
16226 rettv->vval.v_list = list_alloc();
16227 }
16228
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016229 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016230 fclose(fd);
16231}
16232
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016233#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016234static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016235
16236/*
16237 * Convert a List to proftime_T.
16238 * Return FAIL when there is something wrong.
16239 */
16240 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016241list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016242{
16243 long n1, n2;
16244 int error = FALSE;
16245
16246 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16247 || arg->vval.v_list->lv_len != 2)
16248 return FAIL;
16249 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16250 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16251# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016252 tm->HighPart = n1;
16253 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016254# else
16255 tm->tv_sec = n1;
16256 tm->tv_usec = n2;
16257# endif
16258 return error ? FAIL : OK;
16259}
16260#endif /* FEAT_RELTIME */
16261
16262/*
16263 * "reltime()" function
16264 */
16265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016266f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016267{
16268#ifdef FEAT_RELTIME
16269 proftime_T res;
16270 proftime_T start;
16271
16272 if (argvars[0].v_type == VAR_UNKNOWN)
16273 {
16274 /* No arguments: get current time. */
16275 profile_start(&res);
16276 }
16277 else if (argvars[1].v_type == VAR_UNKNOWN)
16278 {
16279 if (list2proftime(&argvars[0], &res) == FAIL)
16280 return;
16281 profile_end(&res);
16282 }
16283 else
16284 {
16285 /* Two arguments: compute the difference. */
16286 if (list2proftime(&argvars[0], &start) == FAIL
16287 || list2proftime(&argvars[1], &res) == FAIL)
16288 return;
16289 profile_sub(&res, &start);
16290 }
16291
16292 if (rettv_list_alloc(rettv) == OK)
16293 {
16294 long n1, n2;
16295
16296# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016297 n1 = res.HighPart;
16298 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016299# else
16300 n1 = res.tv_sec;
16301 n2 = res.tv_usec;
16302# endif
16303 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16304 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16305 }
16306#endif
16307}
16308
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016309#ifdef FEAT_FLOAT
16310/*
16311 * "reltimefloat()" function
16312 */
16313 static void
16314f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16315{
16316# ifdef FEAT_RELTIME
16317 proftime_T tm;
16318# endif
16319
16320 rettv->v_type = VAR_FLOAT;
16321 rettv->vval.v_float = 0;
16322# ifdef FEAT_RELTIME
16323 if (list2proftime(&argvars[0], &tm) == OK)
16324 rettv->vval.v_float = profile_float(&tm);
16325# endif
16326}
16327#endif
16328
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016329/*
16330 * "reltimestr()" function
16331 */
16332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016333f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016334{
16335#ifdef FEAT_RELTIME
16336 proftime_T tm;
16337#endif
16338
16339 rettv->v_type = VAR_STRING;
16340 rettv->vval.v_string = NULL;
16341#ifdef FEAT_RELTIME
16342 if (list2proftime(&argvars[0], &tm) == OK)
16343 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16344#endif
16345}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016346
Bram Moolenaar0d660222005-01-07 21:51:51 +000016347#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016348static void make_connection(void);
16349static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016350
16351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016352make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016353{
16354 if (X_DISPLAY == NULL
16355# ifdef FEAT_GUI
16356 && !gui.in_use
16357# endif
16358 )
16359 {
16360 x_force_connect = TRUE;
16361 setup_term_clip();
16362 x_force_connect = FALSE;
16363 }
16364}
16365
16366 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016367check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016368{
16369 make_connection();
16370 if (X_DISPLAY == NULL)
16371 {
16372 EMSG(_("E240: No connection to Vim server"));
16373 return FAIL;
16374 }
16375 return OK;
16376}
16377#endif
16378
16379#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016380static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016381
16382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016383remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016384{
16385 char_u *server_name;
16386 char_u *keys;
16387 char_u *r = NULL;
16388 char_u buf[NUMBUFLEN];
16389# ifdef WIN32
16390 HWND w;
16391# else
16392 Window w;
16393# endif
16394
16395 if (check_restricted() || check_secure())
16396 return;
16397
16398# ifdef FEAT_X11
16399 if (check_connection() == FAIL)
16400 return;
16401# endif
16402
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016403 server_name = get_tv_string_chk(&argvars[0]);
16404 if (server_name == NULL)
16405 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016406 keys = get_tv_string_buf(&argvars[1], buf);
16407# ifdef WIN32
16408 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16409# else
16410 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16411 < 0)
16412# endif
16413 {
16414 if (r != NULL)
16415 EMSG(r); /* sending worked but evaluation failed */
16416 else
16417 EMSG2(_("E241: Unable to send to %s"), server_name);
16418 return;
16419 }
16420
16421 rettv->vval.v_string = r;
16422
16423 if (argvars[2].v_type != VAR_UNKNOWN)
16424 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016425 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016426 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016427 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016428
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016429 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016430 v.di_tv.v_type = VAR_STRING;
16431 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016432 idvar = get_tv_string_chk(&argvars[2]);
16433 if (idvar != NULL)
16434 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016435 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016436 }
16437}
16438#endif
16439
16440/*
16441 * "remote_expr()" function
16442 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016444f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016445{
16446 rettv->v_type = VAR_STRING;
16447 rettv->vval.v_string = NULL;
16448#ifdef FEAT_CLIENTSERVER
16449 remote_common(argvars, rettv, TRUE);
16450#endif
16451}
16452
16453/*
16454 * "remote_foreground()" function
16455 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016457f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016458{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016459#ifdef FEAT_CLIENTSERVER
16460# ifdef WIN32
16461 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016462 {
16463 char_u *server_name = get_tv_string_chk(&argvars[0]);
16464
16465 if (server_name != NULL)
16466 serverForeground(server_name);
16467 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016468# else
16469 /* Send a foreground() expression to the server. */
16470 argvars[1].v_type = VAR_STRING;
16471 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16472 argvars[2].v_type = VAR_UNKNOWN;
16473 remote_common(argvars, rettv, TRUE);
16474 vim_free(argvars[1].vval.v_string);
16475# endif
16476#endif
16477}
16478
Bram Moolenaar0d660222005-01-07 21:51:51 +000016479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016480f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016481{
16482#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016483 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016484 char_u *s = NULL;
16485# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016486 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016487# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016488 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016489
16490 if (check_restricted() || check_secure())
16491 {
16492 rettv->vval.v_number = -1;
16493 return;
16494 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016495 serverid = get_tv_string_chk(&argvars[0]);
16496 if (serverid == NULL)
16497 {
16498 rettv->vval.v_number = -1;
16499 return; /* type error; errmsg already given */
16500 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016501# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016502 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016503 if (n == 0)
16504 rettv->vval.v_number = -1;
16505 else
16506 {
16507 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16508 rettv->vval.v_number = (s != NULL);
16509 }
16510# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016511 if (check_connection() == FAIL)
16512 return;
16513
16514 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016515 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016516# endif
16517
16518 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16519 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016520 char_u *retvar;
16521
Bram Moolenaar33570922005-01-25 22:26:29 +000016522 v.di_tv.v_type = VAR_STRING;
16523 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016524 retvar = get_tv_string_chk(&argvars[1]);
16525 if (retvar != NULL)
16526 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016527 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016528 }
16529#else
16530 rettv->vval.v_number = -1;
16531#endif
16532}
16533
Bram Moolenaar0d660222005-01-07 21:51:51 +000016534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016535f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016536{
16537 char_u *r = NULL;
16538
16539#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016540 char_u *serverid = get_tv_string_chk(&argvars[0]);
16541
16542 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016543 {
16544# ifdef WIN32
16545 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016546 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016547
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016548 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016549 if (n != 0)
16550 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16551 if (r == NULL)
16552# else
16553 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016554 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016555# endif
16556 EMSG(_("E277: Unable to read a server reply"));
16557 }
16558#endif
16559 rettv->v_type = VAR_STRING;
16560 rettv->vval.v_string = r;
16561}
16562
16563/*
16564 * "remote_send()" function
16565 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016567f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016568{
16569 rettv->v_type = VAR_STRING;
16570 rettv->vval.v_string = NULL;
16571#ifdef FEAT_CLIENTSERVER
16572 remote_common(argvars, rettv, FALSE);
16573#endif
16574}
16575
16576/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016577 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016578 */
16579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016580f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016581{
Bram Moolenaar33570922005-01-25 22:26:29 +000016582 list_T *l;
16583 listitem_T *item, *item2;
16584 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016585 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016586 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016587 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016588 dict_T *d;
16589 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016590 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016591
Bram Moolenaar8c711452005-01-14 21:53:12 +000016592 if (argvars[0].v_type == VAR_DICT)
16593 {
16594 if (argvars[2].v_type != VAR_UNKNOWN)
16595 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016596 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016597 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016598 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016599 key = get_tv_string_chk(&argvars[1]);
16600 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016601 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016602 di = dict_find(d, key, -1);
16603 if (di == NULL)
16604 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016605 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16606 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016607 {
16608 *rettv = di->di_tv;
16609 init_tv(&di->di_tv);
16610 dictitem_remove(d, di);
16611 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016612 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016613 }
16614 }
16615 else if (argvars[0].v_type != VAR_LIST)
16616 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016617 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016618 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016619 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016620 int error = FALSE;
16621
16622 idx = get_tv_number_chk(&argvars[1], &error);
16623 if (error)
16624 ; /* type error: do nothing, errmsg already given */
16625 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016626 EMSGN(_(e_listidx), idx);
16627 else
16628 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016629 if (argvars[2].v_type == VAR_UNKNOWN)
16630 {
16631 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016632 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016633 *rettv = item->li_tv;
16634 vim_free(item);
16635 }
16636 else
16637 {
16638 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016639 end = get_tv_number_chk(&argvars[2], &error);
16640 if (error)
16641 ; /* type error: do nothing */
16642 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016643 EMSGN(_(e_listidx), end);
16644 else
16645 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016646 int cnt = 0;
16647
16648 for (li = item; li != NULL; li = li->li_next)
16649 {
16650 ++cnt;
16651 if (li == item2)
16652 break;
16653 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016654 if (li == NULL) /* didn't find "item2" after "item" */
16655 EMSG(_(e_invrange));
16656 else
16657 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016658 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016659 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016660 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016661 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016662 l->lv_first = item;
16663 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016664 item->li_prev = NULL;
16665 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016666 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016667 }
16668 }
16669 }
16670 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016671 }
16672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016673}
16674
16675/*
16676 * "rename({from}, {to})" function
16677 */
16678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016679f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680{
16681 char_u buf[NUMBUFLEN];
16682
16683 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016684 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016685 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016686 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16687 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016688}
16689
16690/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016691 * "repeat()" function
16692 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016694f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016695{
16696 char_u *p;
16697 int n;
16698 int slen;
16699 int len;
16700 char_u *r;
16701 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016702
16703 n = get_tv_number(&argvars[1]);
16704 if (argvars[0].v_type == VAR_LIST)
16705 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016706 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016707 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016708 if (list_extend(rettv->vval.v_list,
16709 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016710 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016711 }
16712 else
16713 {
16714 p = get_tv_string(&argvars[0]);
16715 rettv->v_type = VAR_STRING;
16716 rettv->vval.v_string = NULL;
16717
16718 slen = (int)STRLEN(p);
16719 len = slen * n;
16720 if (len <= 0)
16721 return;
16722
16723 r = alloc(len + 1);
16724 if (r != NULL)
16725 {
16726 for (i = 0; i < n; i++)
16727 mch_memmove(r + i * slen, p, (size_t)slen);
16728 r[len] = NUL;
16729 }
16730
16731 rettv->vval.v_string = r;
16732 }
16733}
16734
16735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736 * "resolve()" function
16737 */
16738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016739f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016740{
16741 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016742#ifdef HAVE_READLINK
16743 char_u *buf = NULL;
16744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016745
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016746 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016747#ifdef FEAT_SHORTCUT
16748 {
16749 char_u *v = NULL;
16750
16751 v = mch_resolve_shortcut(p);
16752 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016753 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016754 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016755 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756 }
16757#else
16758# ifdef HAVE_READLINK
16759 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760 char_u *cpy;
16761 int len;
16762 char_u *remain = NULL;
16763 char_u *q;
16764 int is_relative_to_current = FALSE;
16765 int has_trailing_pathsep = FALSE;
16766 int limit = 100;
16767
16768 p = vim_strsave(p);
16769
16770 if (p[0] == '.' && (vim_ispathsep(p[1])
16771 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16772 is_relative_to_current = TRUE;
16773
16774 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016775 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016776 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016778 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016780
16781 q = getnextcomp(p);
16782 if (*q != NUL)
16783 {
16784 /* Separate the first path component in "p", and keep the
16785 * remainder (beginning with the path separator). */
16786 remain = vim_strsave(q - 1);
16787 q[-1] = NUL;
16788 }
16789
Bram Moolenaard9462e32011-04-11 21:35:11 +020016790 buf = alloc(MAXPATHL + 1);
16791 if (buf == NULL)
16792 goto fail;
16793
Bram Moolenaar071d4272004-06-13 20:20:40 +000016794 for (;;)
16795 {
16796 for (;;)
16797 {
16798 len = readlink((char *)p, (char *)buf, MAXPATHL);
16799 if (len <= 0)
16800 break;
16801 buf[len] = NUL;
16802
16803 if (limit-- == 0)
16804 {
16805 vim_free(p);
16806 vim_free(remain);
16807 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016808 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016809 goto fail;
16810 }
16811
16812 /* Ensure that the result will have a trailing path separator
16813 * if the argument has one. */
16814 if (remain == NULL && has_trailing_pathsep)
16815 add_pathsep(buf);
16816
16817 /* Separate the first path component in the link value and
16818 * concatenate the remainders. */
16819 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16820 if (*q != NUL)
16821 {
16822 if (remain == NULL)
16823 remain = vim_strsave(q - 1);
16824 else
16825 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016826 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016827 if (cpy != NULL)
16828 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016829 vim_free(remain);
16830 remain = cpy;
16831 }
16832 }
16833 q[-1] = NUL;
16834 }
16835
16836 q = gettail(p);
16837 if (q > p && *q == NUL)
16838 {
16839 /* Ignore trailing path separator. */
16840 q[-1] = NUL;
16841 q = gettail(p);
16842 }
16843 if (q > p && !mch_isFullName(buf))
16844 {
16845 /* symlink is relative to directory of argument */
16846 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16847 if (cpy != NULL)
16848 {
16849 STRCPY(cpy, p);
16850 STRCPY(gettail(cpy), buf);
16851 vim_free(p);
16852 p = cpy;
16853 }
16854 }
16855 else
16856 {
16857 vim_free(p);
16858 p = vim_strsave(buf);
16859 }
16860 }
16861
16862 if (remain == NULL)
16863 break;
16864
16865 /* Append the first path component of "remain" to "p". */
16866 q = getnextcomp(remain + 1);
16867 len = q - remain - (*q != NUL);
16868 cpy = vim_strnsave(p, STRLEN(p) + len);
16869 if (cpy != NULL)
16870 {
16871 STRNCAT(cpy, remain, len);
16872 vim_free(p);
16873 p = cpy;
16874 }
16875 /* Shorten "remain". */
16876 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016877 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016878 else
16879 {
16880 vim_free(remain);
16881 remain = NULL;
16882 }
16883 }
16884
16885 /* If the result is a relative path name, make it explicitly relative to
16886 * the current directory if and only if the argument had this form. */
16887 if (!vim_ispathsep(*p))
16888 {
16889 if (is_relative_to_current
16890 && *p != NUL
16891 && !(p[0] == '.'
16892 && (p[1] == NUL
16893 || vim_ispathsep(p[1])
16894 || (p[1] == '.'
16895 && (p[2] == NUL
16896 || vim_ispathsep(p[2]))))))
16897 {
16898 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016899 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016900 if (cpy != NULL)
16901 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016902 vim_free(p);
16903 p = cpy;
16904 }
16905 }
16906 else if (!is_relative_to_current)
16907 {
16908 /* Strip leading "./". */
16909 q = p;
16910 while (q[0] == '.' && vim_ispathsep(q[1]))
16911 q += 2;
16912 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016913 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016914 }
16915 }
16916
16917 /* Ensure that the result will have no trailing path separator
16918 * if the argument had none. But keep "/" or "//". */
16919 if (!has_trailing_pathsep)
16920 {
16921 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016922 if (after_pathsep(p, q))
16923 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016924 }
16925
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016926 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927 }
16928# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016929 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930# endif
16931#endif
16932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016933 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016934
16935#ifdef HAVE_READLINK
16936fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016937 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016938#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016939 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016940}
16941
16942/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016943 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944 */
16945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016946f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947{
Bram Moolenaar33570922005-01-25 22:26:29 +000016948 list_T *l;
16949 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016950
Bram Moolenaar0d660222005-01-07 21:51:51 +000016951 if (argvars[0].v_type != VAR_LIST)
16952 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016953 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016954 && !tv_check_lock(l->lv_lock,
16955 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016956 {
16957 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016958 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016959 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016960 while (li != NULL)
16961 {
16962 ni = li->li_prev;
16963 list_append(l, li);
16964 li = ni;
16965 }
16966 rettv->vval.v_list = l;
16967 rettv->v_type = VAR_LIST;
16968 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016969 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016971}
16972
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016973#define SP_NOMOVE 0x01 /* don't move cursor */
16974#define SP_REPEAT 0x02 /* repeat to find outer pair */
16975#define SP_RETCOUNT 0x04 /* return matchcount */
16976#define SP_SETPCMARK 0x08 /* set previous context mark */
16977#define SP_START 0x10 /* accept match at start position */
16978#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16979#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016980#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016981
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016982static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016983
16984/*
16985 * Get flags for a search function.
16986 * Possibly sets "p_ws".
16987 * Returns BACKWARD, FORWARD or zero (for an error).
16988 */
16989 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016990get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016991{
16992 int dir = FORWARD;
16993 char_u *flags;
16994 char_u nbuf[NUMBUFLEN];
16995 int mask;
16996
16997 if (varp->v_type != VAR_UNKNOWN)
16998 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016999 flags = get_tv_string_buf_chk(varp, nbuf);
17000 if (flags == NULL)
17001 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017002 while (*flags != NUL)
17003 {
17004 switch (*flags)
17005 {
17006 case 'b': dir = BACKWARD; break;
17007 case 'w': p_ws = TRUE; break;
17008 case 'W': p_ws = FALSE; break;
17009 default: mask = 0;
17010 if (flagsp != NULL)
17011 switch (*flags)
17012 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017013 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017014 case 'e': mask = SP_END; break;
17015 case 'm': mask = SP_RETCOUNT; break;
17016 case 'n': mask = SP_NOMOVE; break;
17017 case 'p': mask = SP_SUBPAT; break;
17018 case 'r': mask = SP_REPEAT; break;
17019 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017020 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017021 }
17022 if (mask == 0)
17023 {
17024 EMSG2(_(e_invarg2), flags);
17025 dir = 0;
17026 }
17027 else
17028 *flagsp |= mask;
17029 }
17030 if (dir == 0)
17031 break;
17032 ++flags;
17033 }
17034 }
17035 return dir;
17036}
17037
Bram Moolenaar071d4272004-06-13 20:20:40 +000017038/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017039 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017040 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017041 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017042search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017043{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017044 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045 char_u *pat;
17046 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017047 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048 int save_p_ws = p_ws;
17049 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017050 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017051 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017052 proftime_T tm;
17053#ifdef FEAT_RELTIME
17054 long time_limit = 0;
17055#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017056 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017057 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017059 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017060 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017061 if (dir == 0)
17062 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017063 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017064 if (flags & SP_START)
17065 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017066 if (flags & SP_END)
17067 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017068 if (flags & SP_COLUMN)
17069 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017070
Bram Moolenaar76929292008-01-06 19:07:36 +000017071 /* Optional arguments: line number to stop searching and timeout. */
17072 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017073 {
17074 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17075 if (lnum_stop < 0)
17076 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017077#ifdef FEAT_RELTIME
17078 if (argvars[3].v_type != VAR_UNKNOWN)
17079 {
17080 time_limit = get_tv_number_chk(&argvars[3], NULL);
17081 if (time_limit < 0)
17082 goto theend;
17083 }
17084#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017085 }
17086
Bram Moolenaar76929292008-01-06 19:07:36 +000017087#ifdef FEAT_RELTIME
17088 /* Set the time limit, if there is one. */
17089 profile_setlimit(time_limit, &tm);
17090#endif
17091
Bram Moolenaar231334e2005-07-25 20:46:57 +000017092 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017093 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017094 * Check to make sure only those flags are set.
17095 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17096 * flags cannot be set. Check for that condition also.
17097 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017098 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017099 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017100 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017101 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017102 goto theend;
17103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017104
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017105 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017106 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017107 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017108 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017110 if (flags & SP_SUBPAT)
17111 retval = subpatnum;
17112 else
17113 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017114 if (flags & SP_SETPCMARK)
17115 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017116 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017117 if (match_pos != NULL)
17118 {
17119 /* Store the match cursor position */
17120 match_pos->lnum = pos.lnum;
17121 match_pos->col = pos.col + 1;
17122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017123 /* "/$" will put the cursor after the end of the line, may need to
17124 * correct that here */
17125 check_cursor();
17126 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017127
17128 /* If 'n' flag is used: restore cursor position. */
17129 if (flags & SP_NOMOVE)
17130 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017131 else
17132 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017133theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017135
17136 return retval;
17137}
17138
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017139#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017140
17141/*
17142 * round() is not in C90, use ceil() or floor() instead.
17143 */
17144 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017145vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017146{
17147 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17148}
17149
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017150/*
17151 * "round({float})" function
17152 */
17153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017154f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017155{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017156 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017157
17158 rettv->v_type = VAR_FLOAT;
17159 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017160 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017161 else
17162 rettv->vval.v_float = 0.0;
17163}
17164#endif
17165
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017166/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017167 * "screenattr()" function
17168 */
17169 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017170f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017171{
17172 int row;
17173 int col;
17174 int c;
17175
17176 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17177 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17178 if (row < 0 || row >= screen_Rows
17179 || col < 0 || col >= screen_Columns)
17180 c = -1;
17181 else
17182 c = ScreenAttrs[LineOffset[row] + col];
17183 rettv->vval.v_number = c;
17184}
17185
17186/*
17187 * "screenchar()" function
17188 */
17189 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017190f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017191{
17192 int row;
17193 int col;
17194 int off;
17195 int c;
17196
17197 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17198 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17199 if (row < 0 || row >= screen_Rows
17200 || col < 0 || col >= screen_Columns)
17201 c = -1;
17202 else
17203 {
17204 off = LineOffset[row] + col;
17205#ifdef FEAT_MBYTE
17206 if (enc_utf8 && ScreenLinesUC[off] != 0)
17207 c = ScreenLinesUC[off];
17208 else
17209#endif
17210 c = ScreenLines[off];
17211 }
17212 rettv->vval.v_number = c;
17213}
17214
17215/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017216 * "screencol()" function
17217 *
17218 * First column is 1 to be consistent with virtcol().
17219 */
17220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017221f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017222{
17223 rettv->vval.v_number = screen_screencol() + 1;
17224}
17225
17226/*
17227 * "screenrow()" function
17228 */
17229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017230f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017231{
17232 rettv->vval.v_number = screen_screenrow() + 1;
17233}
17234
17235/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017236 * "search()" function
17237 */
17238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017239f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017240{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017241 int flags = 0;
17242
17243 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244}
17245
Bram Moolenaar071d4272004-06-13 20:20:40 +000017246/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017247 * "searchdecl()" function
17248 */
17249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017250f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017251{
17252 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017253 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017254 int error = FALSE;
17255 char_u *name;
17256
17257 rettv->vval.v_number = 1; /* default: FAIL */
17258
17259 name = get_tv_string_chk(&argvars[0]);
17260 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017261 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017262 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017263 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17264 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17265 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017266 if (!error && name != NULL)
17267 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017268 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017269}
17270
17271/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017272 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017274 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017275searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276{
17277 char_u *spat, *mpat, *epat;
17278 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017279 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280 int dir;
17281 int flags = 0;
17282 char_u nbuf1[NUMBUFLEN];
17283 char_u nbuf2[NUMBUFLEN];
17284 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017285 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017286 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017287 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288
Bram Moolenaar071d4272004-06-13 20:20:40 +000017289 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017290 spat = get_tv_string_chk(&argvars[0]);
17291 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17292 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17293 if (spat == NULL || mpat == NULL || epat == NULL)
17294 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296 /* Handle the optional fourth argument: flags */
17297 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017298 if (dir == 0)
17299 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017300
17301 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017302 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17303 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017304 if ((flags & (SP_END | SP_SUBPAT)) != 0
17305 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017306 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017307 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017308 goto theend;
17309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017310
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017311 /* Using 'r' implies 'W', otherwise it doesn't work. */
17312 if (flags & SP_REPEAT)
17313 p_ws = FALSE;
17314
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017315 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017316 if (argvars[3].v_type == VAR_UNKNOWN
17317 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017318 skip = (char_u *)"";
17319 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017321 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017322 if (argvars[5].v_type != VAR_UNKNOWN)
17323 {
17324 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17325 if (lnum_stop < 0)
17326 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017327#ifdef FEAT_RELTIME
17328 if (argvars[6].v_type != VAR_UNKNOWN)
17329 {
17330 time_limit = get_tv_number_chk(&argvars[6], NULL);
17331 if (time_limit < 0)
17332 goto theend;
17333 }
17334#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017335 }
17336 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017337 if (skip == NULL)
17338 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017340 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017341 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017342
17343theend:
17344 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017345
17346 return retval;
17347}
17348
17349/*
17350 * "searchpair()" function
17351 */
17352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017353f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017354{
17355 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17356}
17357
17358/*
17359 * "searchpairpos()" function
17360 */
17361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017362f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017363{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017364 pos_T match_pos;
17365 int lnum = 0;
17366 int col = 0;
17367
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017368 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017369 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017370
17371 if (searchpair_cmn(argvars, &match_pos) > 0)
17372 {
17373 lnum = match_pos.lnum;
17374 col = match_pos.col;
17375 }
17376
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017377 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17378 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017379}
17380
17381/*
17382 * Search for a start/middle/end thing.
17383 * Used by searchpair(), see its documentation for the details.
17384 * Returns 0 or -1 for no match,
17385 */
17386 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017387do_searchpair(
17388 char_u *spat, /* start pattern */
17389 char_u *mpat, /* middle pattern */
17390 char_u *epat, /* end pattern */
17391 int dir, /* BACKWARD or FORWARD */
17392 char_u *skip, /* skip expression */
17393 int flags, /* SP_SETPCMARK and other SP_ values */
17394 pos_T *match_pos,
17395 linenr_T lnum_stop, /* stop at this line if not zero */
17396 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017397{
17398 char_u *save_cpo;
17399 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17400 long retval = 0;
17401 pos_T pos;
17402 pos_T firstpos;
17403 pos_T foundpos;
17404 pos_T save_cursor;
17405 pos_T save_pos;
17406 int n;
17407 int r;
17408 int nest = 1;
17409 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017410 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017411 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017412
17413 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17414 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017415 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017416
Bram Moolenaar76929292008-01-06 19:07:36 +000017417#ifdef FEAT_RELTIME
17418 /* Set the time limit, if there is one. */
17419 profile_setlimit(time_limit, &tm);
17420#endif
17421
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017422 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17423 * start/middle/end (pat3, for the top pair). */
17424 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17425 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17426 if (pat2 == NULL || pat3 == NULL)
17427 goto theend;
17428 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17429 if (*mpat == NUL)
17430 STRCPY(pat3, pat2);
17431 else
17432 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17433 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017434 if (flags & SP_START)
17435 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017436
Bram Moolenaar071d4272004-06-13 20:20:40 +000017437 save_cursor = curwin->w_cursor;
17438 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017439 clearpos(&firstpos);
17440 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017441 pat = pat3;
17442 for (;;)
17443 {
17444 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017445 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017446 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17447 /* didn't find it or found the first match again: FAIL */
17448 break;
17449
17450 if (firstpos.lnum == 0)
17451 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017452 if (equalpos(pos, foundpos))
17453 {
17454 /* Found the same position again. Can happen with a pattern that
17455 * has "\zs" at the end and searching backwards. Advance one
17456 * character and try again. */
17457 if (dir == BACKWARD)
17458 decl(&pos);
17459 else
17460 incl(&pos);
17461 }
17462 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017463
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017464 /* clear the start flag to avoid getting stuck here */
17465 options &= ~SEARCH_START;
17466
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467 /* If the skip pattern matches, ignore this match. */
17468 if (*skip != NUL)
17469 {
17470 save_pos = curwin->w_cursor;
17471 curwin->w_cursor = pos;
17472 r = eval_to_bool(skip, &err, NULL, FALSE);
17473 curwin->w_cursor = save_pos;
17474 if (err)
17475 {
17476 /* Evaluating {skip} caused an error, break here. */
17477 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017478 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479 break;
17480 }
17481 if (r)
17482 continue;
17483 }
17484
17485 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17486 {
17487 /* Found end when searching backwards or start when searching
17488 * forward: nested pair. */
17489 ++nest;
17490 pat = pat2; /* nested, don't search for middle */
17491 }
17492 else
17493 {
17494 /* Found end when searching forward or start when searching
17495 * backward: end of (nested) pair; or found middle in outer pair. */
17496 if (--nest == 1)
17497 pat = pat3; /* outer level, search for middle */
17498 }
17499
17500 if (nest == 0)
17501 {
17502 /* Found the match: return matchcount or line number. */
17503 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017504 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017505 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017506 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017507 if (flags & SP_SETPCMARK)
17508 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017509 curwin->w_cursor = pos;
17510 if (!(flags & SP_REPEAT))
17511 break;
17512 nest = 1; /* search for next unmatched */
17513 }
17514 }
17515
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017516 if (match_pos != NULL)
17517 {
17518 /* Store the match cursor position */
17519 match_pos->lnum = curwin->w_cursor.lnum;
17520 match_pos->col = curwin->w_cursor.col + 1;
17521 }
17522
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017524 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525 curwin->w_cursor = save_cursor;
17526
17527theend:
17528 vim_free(pat2);
17529 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017530 if (p_cpo == empty_option)
17531 p_cpo = save_cpo;
17532 else
17533 /* Darn, evaluating the {skip} expression changed the value. */
17534 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017535
17536 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537}
17538
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017539/*
17540 * "searchpos()" function
17541 */
17542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017543f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017544{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017545 pos_T match_pos;
17546 int lnum = 0;
17547 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017548 int n;
17549 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017550
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017551 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017552 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017553
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017554 n = search_cmn(argvars, &match_pos, &flags);
17555 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017556 {
17557 lnum = match_pos.lnum;
17558 col = match_pos.col;
17559 }
17560
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017561 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17562 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017563 if (flags & SP_SUBPAT)
17564 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017565}
17566
Bram Moolenaar0d660222005-01-07 21:51:51 +000017567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017568f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017570#ifdef FEAT_CLIENTSERVER
17571 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017572 char_u *server = get_tv_string_chk(&argvars[0]);
17573 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574
Bram Moolenaar0d660222005-01-07 21:51:51 +000017575 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017576 if (server == NULL || reply == NULL)
17577 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017578 if (check_restricted() || check_secure())
17579 return;
17580# ifdef FEAT_X11
17581 if (check_connection() == FAIL)
17582 return;
17583# endif
17584
17585 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017587 EMSG(_("E258: Unable to send to client"));
17588 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017590 rettv->vval.v_number = 0;
17591#else
17592 rettv->vval.v_number = -1;
17593#endif
17594}
17595
Bram Moolenaar0d660222005-01-07 21:51:51 +000017596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017597f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017598{
17599 char_u *r = NULL;
17600
17601#ifdef FEAT_CLIENTSERVER
17602# ifdef WIN32
17603 r = serverGetVimNames();
17604# else
17605 make_connection();
17606 if (X_DISPLAY != NULL)
17607 r = serverGetVimNames(X_DISPLAY);
17608# endif
17609#endif
17610 rettv->v_type = VAR_STRING;
17611 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612}
17613
17614/*
17615 * "setbufvar()" function
17616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017618f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619{
17620 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017623 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624 char_u nbuf[NUMBUFLEN];
17625
17626 if (check_restricted() || check_secure())
17627 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017628 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17629 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017630 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631 varp = &argvars[2];
17632
17633 if (buf != NULL && varname != NULL && varp != NULL)
17634 {
17635 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017636 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637
17638 if (*varname == '&')
17639 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017640 long numval;
17641 char_u *strval;
17642 int error = FALSE;
17643
Bram Moolenaar071d4272004-06-13 20:20:40 +000017644 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017645 numval = get_tv_number_chk(varp, &error);
17646 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017647 if (!error && strval != NULL)
17648 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017649 }
17650 else
17651 {
17652 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17653 if (bufvarname != NULL)
17654 {
17655 STRCPY(bufvarname, "b:");
17656 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017657 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017658 vim_free(bufvarname);
17659 }
17660 }
17661
17662 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665}
17666
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017668f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017669{
17670 dict_T *d;
17671 dictitem_T *di;
17672 char_u *csearch;
17673
17674 if (argvars[0].v_type != VAR_DICT)
17675 {
17676 EMSG(_(e_dictreq));
17677 return;
17678 }
17679
17680 if ((d = argvars[0].vval.v_dict) != NULL)
17681 {
17682 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17683 if (csearch != NULL)
17684 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017685#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017686 if (enc_utf8)
17687 {
17688 int pcc[MAX_MCO];
17689 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017690
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017691 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17692 }
17693 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017694#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017695 set_last_csearch(PTR2CHAR(csearch),
17696 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017697 }
17698
17699 di = dict_find(d, (char_u *)"forward", -1);
17700 if (di != NULL)
17701 set_csearch_direction(get_tv_number(&di->di_tv)
17702 ? FORWARD : BACKWARD);
17703
17704 di = dict_find(d, (char_u *)"until", -1);
17705 if (di != NULL)
17706 set_csearch_until(!!get_tv_number(&di->di_tv));
17707 }
17708}
17709
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710/*
17711 * "setcmdpos()" function
17712 */
17713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017714f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017715{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017716 int pos = (int)get_tv_number(&argvars[0]) - 1;
17717
17718 if (pos >= 0)
17719 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720}
17721
17722/*
Bram Moolenaar80492532016-03-08 17:08:53 +010017723 * "setfperm({fname}, {mode})" function
17724 */
17725 static void
17726f_setfperm(typval_T *argvars, typval_T *rettv)
17727{
17728 char_u *fname;
17729 char_u modebuf[NUMBUFLEN];
17730 char_u *mode_str;
17731 int i;
17732 int mask;
17733 int mode = 0;
17734
17735 rettv->vval.v_number = 0;
17736 fname = get_tv_string_chk(&argvars[0]);
17737 if (fname == NULL)
17738 return;
17739 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
17740 if (mode_str == NULL)
17741 return;
17742 if (STRLEN(mode_str) != 9)
17743 {
17744 EMSG2(_(e_invarg2), mode_str);
17745 return;
17746 }
17747
17748 mask = 1;
17749 for (i = 8; i >= 0; --i)
17750 {
17751 if (mode_str[i] != '-')
17752 mode |= mask;
17753 mask = mask << 1;
17754 }
17755 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
17756}
17757
17758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759 * "setline()" function
17760 */
17761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017762f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017763{
17764 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017765 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017766 list_T *l = NULL;
17767 listitem_T *li = NULL;
17768 long added = 0;
17769 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017771 lnum = get_tv_lnum(&argvars[0]);
17772 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017774 l = argvars[1].vval.v_list;
17775 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017777 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017778 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017779
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017780 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017781 for (;;)
17782 {
17783 if (l != NULL)
17784 {
17785 /* list argument, get next string */
17786 if (li == NULL)
17787 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017788 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017789 li = li->li_next;
17790 }
17791
17792 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017793 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017794 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017795
17796 /* When coming here from Insert mode, sync undo, so that this can be
17797 * undone separately from what was previously inserted. */
17798 if (u_sync_once == 2)
17799 {
17800 u_sync_once = 1; /* notify that u_sync() was called */
17801 u_sync(TRUE);
17802 }
17803
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017804 if (lnum <= curbuf->b_ml.ml_line_count)
17805 {
17806 /* existing line, replace it */
17807 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17808 {
17809 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017810 if (lnum == curwin->w_cursor.lnum)
17811 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017812 rettv->vval.v_number = 0; /* OK */
17813 }
17814 }
17815 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17816 {
17817 /* lnum is one past the last line, append the line */
17818 ++added;
17819 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17820 rettv->vval.v_number = 0; /* OK */
17821 }
17822
17823 if (l == NULL) /* only one string argument */
17824 break;
17825 ++lnum;
17826 }
17827
17828 if (added > 0)
17829 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017830}
17831
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017832static 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 +000017833
Bram Moolenaar071d4272004-06-13 20:20:40 +000017834/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017835 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017836 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017838set_qf_ll_list(
17839 win_T *wp UNUSED,
17840 typval_T *list_arg UNUSED,
17841 typval_T *action_arg UNUSED,
17842 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017843{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017844#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017845 char_u *act;
17846 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017847#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017848
Bram Moolenaar2641f772005-03-25 21:58:17 +000017849 rettv->vval.v_number = -1;
17850
17851#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017852 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017853 EMSG(_(e_listreq));
17854 else
17855 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017856 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017857
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017858 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017859 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017860 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017861 if (act == NULL)
17862 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017863 if (*act == 'a' || *act == 'r')
17864 action = *act;
17865 }
17866
Bram Moolenaar81484f42012-12-05 15:16:47 +010017867 if (l != NULL && set_errorlist(wp, l, action,
17868 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017869 rettv->vval.v_number = 0;
17870 }
17871#endif
17872}
17873
17874/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017875 * "setloclist()" function
17876 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017878f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017879{
17880 win_T *win;
17881
17882 rettv->vval.v_number = -1;
17883
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017884 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017885 if (win != NULL)
17886 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17887}
17888
17889/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017890 * "setmatches()" function
17891 */
17892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017893f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017894{
17895#ifdef FEAT_SEARCH_EXTRA
17896 list_T *l;
17897 listitem_T *li;
17898 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017899 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017900
17901 rettv->vval.v_number = -1;
17902 if (argvars[0].v_type != VAR_LIST)
17903 {
17904 EMSG(_(e_listreq));
17905 return;
17906 }
17907 if ((l = argvars[0].vval.v_list) != NULL)
17908 {
17909
17910 /* To some extent make sure that we are dealing with a list from
17911 * "getmatches()". */
17912 li = l->lv_first;
17913 while (li != NULL)
17914 {
17915 if (li->li_tv.v_type != VAR_DICT
17916 || (d = li->li_tv.vval.v_dict) == NULL)
17917 {
17918 EMSG(_(e_invarg));
17919 return;
17920 }
17921 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017922 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17923 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017924 && dict_find(d, (char_u *)"priority", -1) != NULL
17925 && dict_find(d, (char_u *)"id", -1) != NULL))
17926 {
17927 EMSG(_(e_invarg));
17928 return;
17929 }
17930 li = li->li_next;
17931 }
17932
17933 clear_matches(curwin);
17934 li = l->lv_first;
17935 while (li != NULL)
17936 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017937 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017938 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017939 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017940 char_u *group;
17941 int priority;
17942 int id;
17943 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017944
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017945 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017946 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17947 {
17948 if (s == NULL)
17949 {
17950 s = list_alloc();
17951 if (s == NULL)
17952 return;
17953 }
17954
17955 /* match from matchaddpos() */
17956 for (i = 1; i < 9; i++)
17957 {
17958 sprintf((char *)buf, (char *)"pos%d", i);
17959 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17960 {
17961 if (di->di_tv.v_type != VAR_LIST)
17962 return;
17963
17964 list_append_tv(s, &di->di_tv);
17965 s->lv_refcount++;
17966 }
17967 else
17968 break;
17969 }
17970 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017971
17972 group = get_dict_string(d, (char_u *)"group", FALSE);
17973 priority = (int)get_dict_number(d, (char_u *)"priority");
17974 id = (int)get_dict_number(d, (char_u *)"id");
17975 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17976 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17977 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017978 if (i == 0)
17979 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017980 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017981 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017982 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017983 }
17984 else
17985 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017986 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017987 list_unref(s);
17988 s = NULL;
17989 }
17990
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017991 li = li->li_next;
17992 }
17993 rettv->vval.v_number = 0;
17994 }
17995#endif
17996}
17997
17998/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017999 * "setpos()" function
18000 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018002f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018003{
18004 pos_T pos;
18005 int fnum;
18006 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018007 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018008
Bram Moolenaar08250432008-02-13 11:42:46 +000018009 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018010 name = get_tv_string_chk(argvars);
18011 if (name != NULL)
18012 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018013 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018014 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018015 if (--pos.col < 0)
18016 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018017 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018018 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018019 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018020 if (fnum == curbuf->b_fnum)
18021 {
18022 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018023 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018024 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018025 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018026 curwin->w_set_curswant = FALSE;
18027 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018028 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018029 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018030 }
18031 else
18032 EMSG(_(e_invarg));
18033 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018034 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18035 {
18036 /* set mark */
18037 if (setmark_pos(name[1], &pos, fnum) == OK)
18038 rettv->vval.v_number = 0;
18039 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018040 else
18041 EMSG(_(e_invarg));
18042 }
18043 }
18044}
18045
18046/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018047 * "setqflist()" function
18048 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018050f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018051{
18052 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18053}
18054
18055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056 * "setreg()" function
18057 */
18058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018059f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060{
18061 int regname;
18062 char_u *strregname;
18063 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018064 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018065 int append;
18066 char_u yank_type;
18067 long block_len;
18068
18069 block_len = -1;
18070 yank_type = MAUTO;
18071 append = FALSE;
18072
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018073 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018074 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018076 if (strregname == NULL)
18077 return; /* type error; errmsg already given */
18078 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079 if (regname == 0 || regname == '@')
18080 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018082 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018084 stropt = get_tv_string_chk(&argvars[2]);
18085 if (stropt == NULL)
18086 return; /* type error */
18087 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088 switch (*stropt)
18089 {
18090 case 'a': case 'A': /* append */
18091 append = TRUE;
18092 break;
18093 case 'v': case 'c': /* character-wise selection */
18094 yank_type = MCHAR;
18095 break;
18096 case 'V': case 'l': /* line-wise selection */
18097 yank_type = MLINE;
18098 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099 case 'b': case Ctrl_V: /* block-wise selection */
18100 yank_type = MBLOCK;
18101 if (VIM_ISDIGIT(stropt[1]))
18102 {
18103 ++stropt;
18104 block_len = getdigits(&stropt) - 1;
18105 --stropt;
18106 }
18107 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108 }
18109 }
18110
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018111 if (argvars[1].v_type == VAR_LIST)
18112 {
18113 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018114 char_u **allocval;
18115 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018116 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018117 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018118 int len = argvars[1].vval.v_list->lv_len;
18119 listitem_T *li;
18120
Bram Moolenaar7d647822014-04-05 21:28:56 +020018121 /* First half: use for pointers to result lines; second half: use for
18122 * pointers to allocated copies. */
18123 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018124 if (lstval == NULL)
18125 return;
18126 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018127 allocval = lstval + len + 2;
18128 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018129
18130 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18131 li = li->li_next)
18132 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018133 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018134 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018135 goto free_lstval;
18136 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018137 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018138 /* Need to make a copy, next get_tv_string_buf_chk() will
18139 * overwrite the string. */
18140 strval = vim_strsave(buf);
18141 if (strval == NULL)
18142 goto free_lstval;
18143 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018144 }
18145 *curval++ = strval;
18146 }
18147 *curval++ = NULL;
18148
18149 write_reg_contents_lst(regname, lstval, -1,
18150 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018151free_lstval:
18152 while (curallocval > allocval)
18153 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018154 vim_free(lstval);
18155 }
18156 else
18157 {
18158 strval = get_tv_string_chk(&argvars[1]);
18159 if (strval == NULL)
18160 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018161 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018162 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018163 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018164 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165}
18166
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018167/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018168 * "settabvar()" function
18169 */
18170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018171f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018172{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018173#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018174 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018175 tabpage_T *tp;
18176#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018177 char_u *varname, *tabvarname;
18178 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018179
18180 rettv->vval.v_number = 0;
18181
18182 if (check_restricted() || check_secure())
18183 return;
18184
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018185#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018186 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018187#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018188 varname = get_tv_string_chk(&argvars[1]);
18189 varp = &argvars[2];
18190
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018191 if (varname != NULL && varp != NULL
18192#ifdef FEAT_WINDOWS
18193 && tp != NULL
18194#endif
18195 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018196 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018197#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018198 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018199 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018200#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018201
18202 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18203 if (tabvarname != NULL)
18204 {
18205 STRCPY(tabvarname, "t:");
18206 STRCPY(tabvarname + 2, varname);
18207 set_var(tabvarname, varp, TRUE);
18208 vim_free(tabvarname);
18209 }
18210
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018211#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018212 /* Restore current tabpage */
18213 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018214 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018215#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018216 }
18217}
18218
18219/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018220 * "settabwinvar()" function
18221 */
18222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018223f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018224{
18225 setwinvar(argvars, rettv, 1);
18226}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018227
18228/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018229 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018230 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018232f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018234 setwinvar(argvars, rettv, 0);
18235}
18236
18237/*
18238 * "setwinvar()" and "settabwinvar()" functions
18239 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018240
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018242setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018243{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018244 win_T *win;
18245#ifdef FEAT_WINDOWS
18246 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018247 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018248 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249#endif
18250 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018251 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018252 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018253 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018254
18255 if (check_restricted() || check_secure())
18256 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018257
18258#ifdef FEAT_WINDOWS
18259 if (off == 1)
18260 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18261 else
18262 tp = curtab;
18263#endif
18264 win = find_win_by_nr(&argvars[off], tp);
18265 varname = get_tv_string_chk(&argvars[off + 1]);
18266 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018267
18268 if (win != NULL && varname != NULL && varp != NULL)
18269 {
18270#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018271 need_switch_win = !(tp == curtab && win == curwin);
18272 if (!need_switch_win
18273 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018275 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018276 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018277 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018278 long numval;
18279 char_u *strval;
18280 int error = FALSE;
18281
18282 ++varname;
18283 numval = get_tv_number_chk(varp, &error);
18284 strval = get_tv_string_buf_chk(varp, nbuf);
18285 if (!error && strval != NULL)
18286 set_option_value(varname, numval, strval, OPT_LOCAL);
18287 }
18288 else
18289 {
18290 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18291 if (winvarname != NULL)
18292 {
18293 STRCPY(winvarname, "w:");
18294 STRCPY(winvarname + 2, varname);
18295 set_var(winvarname, varp, TRUE);
18296 vim_free(winvarname);
18297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018298 }
18299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018300#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018301 if (need_switch_win)
18302 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303#endif
18304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018305}
18306
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018307#ifdef FEAT_CRYPT
18308/*
18309 * "sha256({string})" function
18310 */
18311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018312f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018313{
18314 char_u *p;
18315
18316 p = get_tv_string(&argvars[0]);
18317 rettv->vval.v_string = vim_strsave(
18318 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18319 rettv->v_type = VAR_STRING;
18320}
18321#endif /* FEAT_CRYPT */
18322
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018324 * "shellescape({string})" function
18325 */
18326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018327f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018328{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018329 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018330 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018331 rettv->v_type = VAR_STRING;
18332}
18333
18334/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018335 * shiftwidth() function
18336 */
18337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018338f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018339{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018340 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018341}
18342
18343/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018344 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018345 */
18346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018347f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018348{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018349 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018350
Bram Moolenaar0d660222005-01-07 21:51:51 +000018351 p = get_tv_string(&argvars[0]);
18352 rettv->vval.v_string = vim_strsave(p);
18353 simplify_filename(rettv->vval.v_string); /* simplify in place */
18354 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018355}
18356
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018357#ifdef FEAT_FLOAT
18358/*
18359 * "sin()" function
18360 */
18361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018362f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018363{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018364 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018365
18366 rettv->v_type = VAR_FLOAT;
18367 if (get_float_arg(argvars, &f) == OK)
18368 rettv->vval.v_float = sin(f);
18369 else
18370 rettv->vval.v_float = 0.0;
18371}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018372
18373/*
18374 * "sinh()" function
18375 */
18376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018377f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018378{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018379 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018380
18381 rettv->v_type = VAR_FLOAT;
18382 if (get_float_arg(argvars, &f) == OK)
18383 rettv->vval.v_float = sinh(f);
18384 else
18385 rettv->vval.v_float = 0.0;
18386}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018387#endif
18388
Bram Moolenaar0d660222005-01-07 21:51:51 +000018389static int
18390#ifdef __BORLANDC__
18391 _RTLENTRYF
18392#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018393 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018394static int
18395#ifdef __BORLANDC__
18396 _RTLENTRYF
18397#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018398 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018399
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018400/* struct used in the array that's given to qsort() */
18401typedef struct
18402{
18403 listitem_T *item;
18404 int idx;
18405} sortItem_T;
18406
Bram Moolenaar0b962472016-02-22 22:51:33 +010018407/* struct storing information about current sort */
18408typedef struct
18409{
18410 int item_compare_ic;
18411 int item_compare_numeric;
18412 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018413#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018414 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018415#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018416 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018417 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018418 dict_T *item_compare_selfdict;
18419 int item_compare_func_err;
18420 int item_compare_keep_zero;
18421} sortinfo_T;
18422static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018423static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018424#define ITEM_COMPARE_FAIL 999
18425
Bram Moolenaar071d4272004-06-13 20:20:40 +000018426/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018427 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018428 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018429 static int
18430#ifdef __BORLANDC__
18431_RTLENTRYF
18432#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018433item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018434{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018435 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018436 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018437 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018438 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018439 int res;
18440 char_u numbuf1[NUMBUFLEN];
18441 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018442
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018443 si1 = (sortItem_T *)s1;
18444 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018445 tv1 = &si1->item->li_tv;
18446 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018447
Bram Moolenaar0b962472016-02-22 22:51:33 +010018448 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018449 {
18450 long v1 = get_tv_number(tv1);
18451 long v2 = get_tv_number(tv2);
18452
18453 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18454 }
18455
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018456#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018457 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018458 {
18459 float_T v1 = get_tv_float(tv1);
18460 float_T v2 = get_tv_float(tv2);
18461
18462 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18463 }
18464#endif
18465
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018466 /* tv2string() puts quotes around a string and allocates memory. Don't do
18467 * that for string variables. Use a single quote when comparing with a
18468 * non-string to do what the docs promise. */
18469 if (tv1->v_type == VAR_STRING)
18470 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018471 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018472 p1 = (char_u *)"'";
18473 else
18474 p1 = tv1->vval.v_string;
18475 }
18476 else
18477 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18478 if (tv2->v_type == VAR_STRING)
18479 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018480 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018481 p2 = (char_u *)"'";
18482 else
18483 p2 = tv2->vval.v_string;
18484 }
18485 else
18486 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018487 if (p1 == NULL)
18488 p1 = (char_u *)"";
18489 if (p2 == NULL)
18490 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018491 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018492 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018493 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018494 res = STRICMP(p1, p2);
18495 else
18496 res = STRCMP(p1, p2);
18497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018499 {
18500 double n1, n2;
18501 n1 = strtod((char *)p1, (char **)&p1);
18502 n2 = strtod((char *)p2, (char **)&p2);
18503 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18504 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018505
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018506 /* When the result would be zero, compare the item indexes. Makes the
18507 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018508 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018509 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018510
Bram Moolenaar0d660222005-01-07 21:51:51 +000018511 vim_free(tofree1);
18512 vim_free(tofree2);
18513 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018514}
18515
18516 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018517#ifdef __BORLANDC__
18518_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018519#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018520item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018521{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018522 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018523 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018524 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018525 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018526 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018527 char_u *func_name;
18528 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018530 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018531 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018532 return 0;
18533
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018534 si1 = (sortItem_T *)s1;
18535 si2 = (sortItem_T *)s2;
18536
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018537 if (partial == NULL)
18538 func_name = sortinfo->item_compare_func;
18539 else
18540 func_name = partial->pt_name;
18541
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018542 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018543 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018544 copy_tv(&si1->item->li_tv, &argv[0]);
18545 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018546
18547 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018548 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018549 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018550 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018551 clear_tv(&argv[0]);
18552 clear_tv(&argv[1]);
18553
18554 if (res == FAIL)
18555 res = ITEM_COMPARE_FAIL;
18556 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018557 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18558 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018559 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018560 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018561
18562 /* When the result would be zero, compare the pointers themselves. Makes
18563 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018564 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018565 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018566
Bram Moolenaar0d660222005-01-07 21:51:51 +000018567 return res;
18568}
18569
18570/*
18571 * "sort({list})" function
18572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018574do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575{
Bram Moolenaar33570922005-01-25 22:26:29 +000018576 list_T *l;
18577 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018578 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018579 sortinfo_T *old_sortinfo;
18580 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018581 long len;
18582 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018583
Bram Moolenaar0b962472016-02-22 22:51:33 +010018584 /* Pointer to current info struct used in compare function. Save and
18585 * restore the current one for nested calls. */
18586 old_sortinfo = sortinfo;
18587 sortinfo = &info;
18588
Bram Moolenaar0d660222005-01-07 21:51:51 +000018589 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018590 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018591 else
18592 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018593 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018594 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018595 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18596 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018597 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018598 rettv->vval.v_list = l;
18599 rettv->v_type = VAR_LIST;
18600 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018601
Bram Moolenaar0d660222005-01-07 21:51:51 +000018602 len = list_len(l);
18603 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018604 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018605
Bram Moolenaar0b962472016-02-22 22:51:33 +010018606 info.item_compare_ic = FALSE;
18607 info.item_compare_numeric = FALSE;
18608 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018609#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018610 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018611#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018612 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018613 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018614 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018615 if (argvars[1].v_type != VAR_UNKNOWN)
18616 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018617 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018618 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018619 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018620 else if (argvars[1].v_type == VAR_PARTIAL)
18621 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018622 else
18623 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018624 int error = FALSE;
18625
18626 i = get_tv_number_chk(&argvars[1], &error);
18627 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018628 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018629 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018630 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010018631 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018632 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010018633 else if (i != 0)
18634 {
18635 EMSG(_(e_invarg));
18636 goto theend;
18637 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018638 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018639 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010018640 if (*info.item_compare_func == NUL)
18641 {
18642 /* empty string means default sort */
18643 info.item_compare_func = NULL;
18644 }
18645 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018646 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018647 info.item_compare_func = NULL;
18648 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018649 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018650 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018651 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018652 info.item_compare_func = NULL;
18653 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018654 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018655#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018656 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018657 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018658 info.item_compare_func = NULL;
18659 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018660 }
18661#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018662 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018663 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018664 info.item_compare_func = NULL;
18665 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018666 }
18667 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018668 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018669
18670 if (argvars[2].v_type != VAR_UNKNOWN)
18671 {
18672 /* optional third argument: {dict} */
18673 if (argvars[2].v_type != VAR_DICT)
18674 {
18675 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010018676 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018677 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018678 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018679 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681
Bram Moolenaar0d660222005-01-07 21:51:51 +000018682 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018683 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018684 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018685 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686
Bram Moolenaar327aa022014-03-25 18:24:23 +010018687 i = 0;
18688 if (sort)
18689 {
18690 /* sort(): ptrs will be the list to sort */
18691 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018692 {
18693 ptrs[i].item = li;
18694 ptrs[i].idx = i;
18695 ++i;
18696 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018697
Bram Moolenaar0b962472016-02-22 22:51:33 +010018698 info.item_compare_func_err = FALSE;
18699 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018700 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018701 if ((info.item_compare_func != NULL
18702 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018703 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018704 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018705 EMSG(_("E702: Sort compare function failed"));
18706 else
18707 {
18708 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018709 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010018710 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018711 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010018712 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018713
Bram Moolenaar0b962472016-02-22 22:51:33 +010018714 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018715 {
18716 /* Clear the List and append the items in sorted order. */
18717 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18718 l->lv_len = 0;
18719 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018720 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018721 }
18722 }
18723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018724 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018725 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018726 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018727
18728 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018729 info.item_compare_func_err = FALSE;
18730 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018731 item_compare_func_ptr = info.item_compare_func != NULL
18732 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010018733 ? item_compare2 : item_compare;
18734
18735 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18736 li = li->li_next)
18737 {
18738 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18739 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018740 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018741 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018742 {
18743 EMSG(_("E882: Uniq compare function failed"));
18744 break;
18745 }
18746 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018747
Bram Moolenaar0b962472016-02-22 22:51:33 +010018748 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018749 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018750 while (--i >= 0)
18751 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018752 li = ptrs[i].item->li_next;
18753 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018754 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018755 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018756 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018757 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018758 list_fix_watch(l, li);
18759 listitem_free(li);
18760 l->lv_len--;
18761 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018762 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018763 }
18764
18765 vim_free(ptrs);
18766 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018767theend:
18768 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018769}
18770
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018771/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018772 * "sort({list})" function
18773 */
18774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018775f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018776{
18777 do_sort_uniq(argvars, rettv, TRUE);
18778}
18779
18780/*
18781 * "uniq({list})" function
18782 */
18783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018784f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018785{
18786 do_sort_uniq(argvars, rettv, FALSE);
18787}
18788
18789/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018790 * "soundfold({word})" function
18791 */
18792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018793f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018794{
18795 char_u *s;
18796
18797 rettv->v_type = VAR_STRING;
18798 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018799#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018800 rettv->vval.v_string = eval_soundfold(s);
18801#else
18802 rettv->vval.v_string = vim_strsave(s);
18803#endif
18804}
18805
18806/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018807 * "spellbadword()" function
18808 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018810f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018811{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018812 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018813 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018814 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018815
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018816 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018817 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018818
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018819#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018820 if (argvars[0].v_type == VAR_UNKNOWN)
18821 {
18822 /* Find the start and length of the badly spelled word. */
18823 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18824 if (len != 0)
18825 word = ml_get_cursor();
18826 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018827 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018828 {
18829 char_u *str = get_tv_string_chk(&argvars[0]);
18830 int capcol = -1;
18831
18832 if (str != NULL)
18833 {
18834 /* Check the argument for spelling. */
18835 while (*str != NUL)
18836 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018837 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018838 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018839 {
18840 word = str;
18841 break;
18842 }
18843 str += len;
18844 }
18845 }
18846 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018847#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018848
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018849 list_append_string(rettv->vval.v_list, word, len);
18850 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018851 attr == HLF_SPB ? "bad" :
18852 attr == HLF_SPR ? "rare" :
18853 attr == HLF_SPL ? "local" :
18854 attr == HLF_SPC ? "caps" :
18855 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018856}
18857
18858/*
18859 * "spellsuggest()" function
18860 */
18861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018862f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018863{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018864#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018865 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018866 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018867 int maxcount;
18868 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018869 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018870 listitem_T *li;
18871 int need_capital = FALSE;
18872#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018873
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018874 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018875 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018876
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018877#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018878 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018879 {
18880 str = get_tv_string(&argvars[0]);
18881 if (argvars[1].v_type != VAR_UNKNOWN)
18882 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018883 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018884 if (maxcount <= 0)
18885 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018886 if (argvars[2].v_type != VAR_UNKNOWN)
18887 {
18888 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18889 if (typeerr)
18890 return;
18891 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018892 }
18893 else
18894 maxcount = 25;
18895
Bram Moolenaar4770d092006-01-12 23:22:24 +000018896 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018897
18898 for (i = 0; i < ga.ga_len; ++i)
18899 {
18900 str = ((char_u **)ga.ga_data)[i];
18901
18902 li = listitem_alloc();
18903 if (li == NULL)
18904 vim_free(str);
18905 else
18906 {
18907 li->li_tv.v_type = VAR_STRING;
18908 li->li_tv.v_lock = 0;
18909 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018910 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018911 }
18912 }
18913 ga_clear(&ga);
18914 }
18915#endif
18916}
18917
Bram Moolenaar0d660222005-01-07 21:51:51 +000018918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018919f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018920{
18921 char_u *str;
18922 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018923 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018924 regmatch_T regmatch;
18925 char_u patbuf[NUMBUFLEN];
18926 char_u *save_cpo;
18927 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018928 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018929 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018930 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018931
18932 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18933 save_cpo = p_cpo;
18934 p_cpo = (char_u *)"";
18935
18936 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018937 if (argvars[1].v_type != VAR_UNKNOWN)
18938 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018939 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18940 if (pat == NULL)
18941 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018942 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018943 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018944 }
18945 if (pat == NULL || *pat == NUL)
18946 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018947
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018948 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018950 if (typeerr)
18951 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018952
Bram Moolenaar0d660222005-01-07 21:51:51 +000018953 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18954 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018955 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018956 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018957 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018958 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018959 if (*str == NUL)
18960 match = FALSE; /* empty item at the end */
18961 else
18962 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018963 if (match)
18964 end = regmatch.startp[0];
18965 else
18966 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018967 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18968 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018969 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018970 if (list_append_string(rettv->vval.v_list, str,
18971 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018972 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018973 }
18974 if (!match)
18975 break;
18976 /* Advance to just after the match. */
18977 if (regmatch.endp[0] > str)
18978 col = 0;
18979 else
18980 {
18981 /* Don't get stuck at the same match. */
18982#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018983 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018984#else
18985 col = 1;
18986#endif
18987 }
18988 str = regmatch.endp[0];
18989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990
Bram Moolenaar473de612013-06-08 18:19:48 +020018991 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993
Bram Moolenaar0d660222005-01-07 21:51:51 +000018994 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018995}
18996
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018997#ifdef FEAT_FLOAT
18998/*
18999 * "sqrt()" function
19000 */
19001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019002f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019003{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019004 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019005
19006 rettv->v_type = VAR_FLOAT;
19007 if (get_float_arg(argvars, &f) == OK)
19008 rettv->vval.v_float = sqrt(f);
19009 else
19010 rettv->vval.v_float = 0.0;
19011}
19012
19013/*
19014 * "str2float()" function
19015 */
19016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019017f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019018{
19019 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19020
19021 if (*p == '+')
19022 p = skipwhite(p + 1);
19023 (void)string2float(p, &rettv->vval.v_float);
19024 rettv->v_type = VAR_FLOAT;
19025}
19026#endif
19027
Bram Moolenaar2c932302006-03-18 21:42:09 +000019028/*
19029 * "str2nr()" function
19030 */
19031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019032f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019033{
19034 int base = 10;
19035 char_u *p;
19036 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019037 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019038
19039 if (argvars[1].v_type != VAR_UNKNOWN)
19040 {
19041 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019042 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019043 {
19044 EMSG(_(e_invarg));
19045 return;
19046 }
19047 }
19048
19049 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019050 if (*p == '+')
19051 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019052 switch (base)
19053 {
19054 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19055 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19056 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19057 default: what = 0;
19058 }
19059 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019060 rettv->vval.v_number = n;
19061}
19062
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063#ifdef HAVE_STRFTIME
19064/*
19065 * "strftime({format}[, {time}])" function
19066 */
19067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019068f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069{
19070 char_u result_buf[256];
19071 struct tm *curtime;
19072 time_t seconds;
19073 char_u *p;
19074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019075 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019077 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019078 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079 seconds = time(NULL);
19080 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019081 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082 curtime = localtime(&seconds);
19083 /* MSVC returns NULL for an invalid value of seconds. */
19084 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019085 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019086 else
19087 {
19088# ifdef FEAT_MBYTE
19089 vimconv_T conv;
19090 char_u *enc;
19091
19092 conv.vc_type = CONV_NONE;
19093 enc = enc_locale();
19094 convert_setup(&conv, p_enc, enc);
19095 if (conv.vc_type != CONV_NONE)
19096 p = string_convert(&conv, p, NULL);
19097# endif
19098 if (p != NULL)
19099 (void)strftime((char *)result_buf, sizeof(result_buf),
19100 (char *)p, curtime);
19101 else
19102 result_buf[0] = NUL;
19103
19104# ifdef FEAT_MBYTE
19105 if (conv.vc_type != CONV_NONE)
19106 vim_free(p);
19107 convert_setup(&conv, enc, p_enc);
19108 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019109 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019110 else
19111# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019112 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019113
19114# ifdef FEAT_MBYTE
19115 /* Release conversion descriptors */
19116 convert_setup(&conv, NULL, NULL);
19117 vim_free(enc);
19118# endif
19119 }
19120}
19121#endif
19122
19123/*
19124 * "stridx()" function
19125 */
19126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019127f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128{
19129 char_u buf[NUMBUFLEN];
19130 char_u *needle;
19131 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019132 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019134 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019136 needle = get_tv_string_chk(&argvars[1]);
19137 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019138 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019139 if (needle == NULL || haystack == NULL)
19140 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019141
Bram Moolenaar33570922005-01-25 22:26:29 +000019142 if (argvars[2].v_type != VAR_UNKNOWN)
19143 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019144 int error = FALSE;
19145
19146 start_idx = get_tv_number_chk(&argvars[2], &error);
19147 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019148 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019149 if (start_idx >= 0)
19150 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019151 }
19152
19153 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19154 if (pos != NULL)
19155 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019156}
19157
19158/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019159 * "string()" function
19160 */
19161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019162f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019163{
19164 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019165 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019166
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019167 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019168 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019169 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019170 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019171 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019172}
19173
19174/*
19175 * "strlen()" function
19176 */
19177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019178f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019180 rettv->vval.v_number = (varnumber_T)(STRLEN(
19181 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019182}
19183
19184/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019185 * "strchars()" function
19186 */
19187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019188f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019189{
19190 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019191 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019192#ifdef FEAT_MBYTE
19193 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019194 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019195#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019196
19197 if (argvars[1].v_type != VAR_UNKNOWN)
19198 skipcc = get_tv_number_chk(&argvars[1], NULL);
19199 if (skipcc < 0 || skipcc > 1)
19200 EMSG(_(e_invarg));
19201 else
19202 {
19203#ifdef FEAT_MBYTE
19204 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19205 while (*s != NUL)
19206 {
19207 func_mb_ptr2char_adv(&s);
19208 ++len;
19209 }
19210 rettv->vval.v_number = len;
19211#else
19212 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19213#endif
19214 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019215}
19216
19217/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019218 * "strdisplaywidth()" function
19219 */
19220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019221f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019222{
19223 char_u *s = get_tv_string(&argvars[0]);
19224 int col = 0;
19225
19226 if (argvars[1].v_type != VAR_UNKNOWN)
19227 col = get_tv_number(&argvars[1]);
19228
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019229 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019230}
19231
19232/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019233 * "strwidth()" function
19234 */
19235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019236f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019237{
19238 char_u *s = get_tv_string(&argvars[0]);
19239
19240 rettv->vval.v_number = (varnumber_T)(
19241#ifdef FEAT_MBYTE
19242 mb_string2cells(s, -1)
19243#else
19244 STRLEN(s)
19245#endif
19246 );
19247}
19248
19249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019250 * "strpart()" function
19251 */
19252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019253f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019254{
19255 char_u *p;
19256 int n;
19257 int len;
19258 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019259 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019260
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019261 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019262 slen = (int)STRLEN(p);
19263
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019264 n = get_tv_number_chk(&argvars[1], &error);
19265 if (error)
19266 len = 0;
19267 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019268 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019269 else
19270 len = slen - n; /* default len: all bytes that are available. */
19271
19272 /*
19273 * Only return the overlap between the specified part and the actual
19274 * string.
19275 */
19276 if (n < 0)
19277 {
19278 len += n;
19279 n = 0;
19280 }
19281 else if (n > slen)
19282 n = slen;
19283 if (len < 0)
19284 len = 0;
19285 else if (n + len > slen)
19286 len = slen - n;
19287
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019288 rettv->v_type = VAR_STRING;
19289 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290}
19291
19292/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019293 * "strridx()" function
19294 */
19295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019296f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019297{
19298 char_u buf[NUMBUFLEN];
19299 char_u *needle;
19300 char_u *haystack;
19301 char_u *rest;
19302 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019303 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019305 needle = get_tv_string_chk(&argvars[1]);
19306 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019307
19308 rettv->vval.v_number = -1;
19309 if (needle == NULL || haystack == NULL)
19310 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019311
19312 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019313 if (argvars[2].v_type != VAR_UNKNOWN)
19314 {
19315 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019316 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019317 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019318 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019319 }
19320 else
19321 end_idx = haystack_len;
19322
Bram Moolenaar0d660222005-01-07 21:51:51 +000019323 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019324 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019325 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019326 lastmatch = haystack + end_idx;
19327 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019328 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019329 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019330 for (rest = haystack; *rest != '\0'; ++rest)
19331 {
19332 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019333 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019334 break;
19335 lastmatch = rest;
19336 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019337 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019338
19339 if (lastmatch == NULL)
19340 rettv->vval.v_number = -1;
19341 else
19342 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19343}
19344
19345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019346 * "strtrans()" function
19347 */
19348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019349f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019350{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019351 rettv->v_type = VAR_STRING;
19352 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019353}
19354
19355/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019356 * "submatch()" function
19357 */
19358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019359f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019360{
Bram Moolenaar41571762014-04-02 19:00:58 +020019361 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019362 int no;
19363 int retList = 0;
19364
19365 no = (int)get_tv_number_chk(&argvars[0], &error);
19366 if (error)
19367 return;
19368 error = FALSE;
19369 if (argvars[1].v_type != VAR_UNKNOWN)
19370 retList = get_tv_number_chk(&argvars[1], &error);
19371 if (error)
19372 return;
19373
19374 if (retList == 0)
19375 {
19376 rettv->v_type = VAR_STRING;
19377 rettv->vval.v_string = reg_submatch(no);
19378 }
19379 else
19380 {
19381 rettv->v_type = VAR_LIST;
19382 rettv->vval.v_list = reg_submatch_list(no);
19383 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019384}
19385
19386/*
19387 * "substitute()" function
19388 */
19389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019390f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019391{
19392 char_u patbuf[NUMBUFLEN];
19393 char_u subbuf[NUMBUFLEN];
19394 char_u flagsbuf[NUMBUFLEN];
19395
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019396 char_u *str = get_tv_string_chk(&argvars[0]);
19397 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19398 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19399 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19400
Bram Moolenaar0d660222005-01-07 21:51:51 +000019401 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019402 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19403 rettv->vval.v_string = NULL;
19404 else
19405 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019406}
19407
19408/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019409 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019412f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019413{
19414 int id = 0;
19415#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019416 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019417 long col;
19418 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019419 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019421 lnum = get_tv_lnum(argvars); /* -1 on type error */
19422 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19423 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019424
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019425 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019426 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019427 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019428#endif
19429
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019430 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019431}
19432
19433/*
19434 * "synIDattr(id, what [, mode])" function
19435 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019437f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019438{
19439 char_u *p = NULL;
19440#ifdef FEAT_SYN_HL
19441 int id;
19442 char_u *what;
19443 char_u *mode;
19444 char_u modebuf[NUMBUFLEN];
19445 int modec;
19446
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019447 id = get_tv_number(&argvars[0]);
19448 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019449 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019450 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019451 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019452 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019453 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454 modec = 0; /* replace invalid with current */
19455 }
19456 else
19457 {
19458#ifdef FEAT_GUI
19459 if (gui.in_use)
19460 modec = 'g';
19461 else
19462#endif
19463 if (t_colors > 1)
19464 modec = 'c';
19465 else
19466 modec = 't';
19467 }
19468
19469
19470 switch (TOLOWER_ASC(what[0]))
19471 {
19472 case 'b':
19473 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19474 p = highlight_color(id, what, modec);
19475 else /* bold */
19476 p = highlight_has_attr(id, HL_BOLD, modec);
19477 break;
19478
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019479 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019480 p = highlight_color(id, what, modec);
19481 break;
19482
19483 case 'i':
19484 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19485 p = highlight_has_attr(id, HL_INVERSE, modec);
19486 else /* italic */
19487 p = highlight_has_attr(id, HL_ITALIC, modec);
19488 break;
19489
19490 case 'n': /* name */
19491 p = get_highlight_name(NULL, id - 1);
19492 break;
19493
19494 case 'r': /* reverse */
19495 p = highlight_has_attr(id, HL_INVERSE, modec);
19496 break;
19497
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019498 case 's':
19499 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19500 p = highlight_color(id, what, modec);
19501 else /* standout */
19502 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019503 break;
19504
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019505 case 'u':
19506 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19507 /* underline */
19508 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19509 else
19510 /* undercurl */
19511 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019512 break;
19513 }
19514
19515 if (p != NULL)
19516 p = vim_strsave(p);
19517#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019518 rettv->v_type = VAR_STRING;
19519 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019520}
19521
19522/*
19523 * "synIDtrans(id)" function
19524 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019526f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527{
19528 int id;
19529
19530#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019531 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532
19533 if (id > 0)
19534 id = syn_get_final_id(id);
19535 else
19536#endif
19537 id = 0;
19538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019539 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019540}
19541
19542/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019543 * "synconcealed(lnum, col)" function
19544 */
19545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019546f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019547{
19548#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19549 long lnum;
19550 long col;
19551 int syntax_flags = 0;
19552 int cchar;
19553 int matchid = 0;
19554 char_u str[NUMBUFLEN];
19555#endif
19556
19557 rettv->v_type = VAR_LIST;
19558 rettv->vval.v_list = NULL;
19559
19560#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19561 lnum = get_tv_lnum(argvars); /* -1 on type error */
19562 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19563
19564 vim_memset(str, NUL, sizeof(str));
19565
19566 if (rettv_list_alloc(rettv) != FAIL)
19567 {
19568 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19569 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19570 && curwin->w_p_cole > 0)
19571 {
19572 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19573 syntax_flags = get_syntax_info(&matchid);
19574
19575 /* get the conceal character */
19576 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19577 {
19578 cchar = syn_get_sub_char();
19579 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19580 cchar = lcs_conceal;
19581 if (cchar != NUL)
19582 {
19583# ifdef FEAT_MBYTE
19584 if (has_mbyte)
19585 (*mb_char2bytes)(cchar, str);
19586 else
19587# endif
19588 str[0] = cchar;
19589 }
19590 }
19591 }
19592
19593 list_append_number(rettv->vval.v_list,
19594 (syntax_flags & HL_CONCEAL) != 0);
19595 /* -1 to auto-determine strlen */
19596 list_append_string(rettv->vval.v_list, str, -1);
19597 list_append_number(rettv->vval.v_list, matchid);
19598 }
19599#endif
19600}
19601
19602/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019603 * "synstack(lnum, col)" function
19604 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019606f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019607{
19608#ifdef FEAT_SYN_HL
19609 long lnum;
19610 long col;
19611 int i;
19612 int id;
19613#endif
19614
19615 rettv->v_type = VAR_LIST;
19616 rettv->vval.v_list = NULL;
19617
19618#ifdef FEAT_SYN_HL
19619 lnum = get_tv_lnum(argvars); /* -1 on type error */
19620 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19621
19622 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019623 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019624 && rettv_list_alloc(rettv) != FAIL)
19625 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019626 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019627 for (i = 0; ; ++i)
19628 {
19629 id = syn_get_stack_item(i);
19630 if (id < 0)
19631 break;
19632 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19633 break;
19634 }
19635 }
19636#endif
19637}
19638
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019640get_cmd_output_as_rettv(
19641 typval_T *argvars,
19642 typval_T *rettv,
19643 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019645 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019647 char_u *infile = NULL;
19648 char_u buf[NUMBUFLEN];
19649 int err = FALSE;
19650 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019651 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019652 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019654 rettv->v_type = VAR_STRING;
19655 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019656 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019657 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019658
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019659 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019660 {
19661 /*
19662 * Write the string to a temp file, to be used for input of the shell
19663 * command.
19664 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019665 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019666 {
19667 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019668 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019669 }
19670
19671 fd = mch_fopen((char *)infile, WRITEBIN);
19672 if (fd == NULL)
19673 {
19674 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019675 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019676 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019677 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019678 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019679 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19680 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019681 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019682 else
19683 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019684 size_t len;
19685
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019686 p = get_tv_string_buf_chk(&argvars[1], buf);
19687 if (p == NULL)
19688 {
19689 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019690 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019691 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019692 len = STRLEN(p);
19693 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019694 err = TRUE;
19695 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019696 if (fclose(fd) != 0)
19697 err = TRUE;
19698 if (err)
19699 {
19700 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019701 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019702 }
19703 }
19704
Bram Moolenaar52a72462014-08-29 15:53:52 +020019705 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19706 * echoes typeahead, that messes up the display. */
19707 if (!msg_silent)
19708 flags += SHELL_COOKED;
19709
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019710 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019711 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019712 int len;
19713 listitem_T *li;
19714 char_u *s = NULL;
19715 char_u *start;
19716 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019717 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718
Bram Moolenaar52a72462014-08-29 15:53:52 +020019719 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019720 if (res == NULL)
19721 goto errret;
19722
19723 list = list_alloc();
19724 if (list == NULL)
19725 goto errret;
19726
19727 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019728 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019729 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019730 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019731 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019732 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019733
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019734 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019735 if (s == NULL)
19736 goto errret;
19737
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019738 for (p = s; start < end; ++p, ++start)
19739 *p = *start == NUL ? NL : *start;
19740 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019741
19742 li = listitem_alloc();
19743 if (li == NULL)
19744 {
19745 vim_free(s);
19746 goto errret;
19747 }
19748 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019749 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019750 li->li_tv.vval.v_string = s;
19751 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019753
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019754 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019755 rettv->v_type = VAR_LIST;
19756 rettv->vval.v_list = list;
19757 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019759 else
19760 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019761 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019762#ifdef USE_CR
19763 /* translate <CR> into <NL> */
19764 if (res != NULL)
19765 {
19766 char_u *s;
19767
19768 for (s = res; *s; ++s)
19769 {
19770 if (*s == CAR)
19771 *s = NL;
19772 }
19773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774#else
19775# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019776 /* translate <CR><NL> into <NL> */
19777 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019778 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019779 char_u *s, *d;
19780
19781 d = res;
19782 for (s = res; *s; ++s)
19783 {
19784 if (s[0] == CAR && s[1] == NL)
19785 ++s;
19786 *d++ = *s;
19787 }
19788 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790# endif
19791#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019792 rettv->vval.v_string = res;
19793 res = NULL;
19794 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019795
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019796errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019797 if (infile != NULL)
19798 {
19799 mch_remove(infile);
19800 vim_free(infile);
19801 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019802 if (res != NULL)
19803 vim_free(res);
19804 if (list != NULL)
19805 list_free(list, TRUE);
19806}
19807
19808/*
19809 * "system()" function
19810 */
19811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019812f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019813{
19814 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19815}
19816
19817/*
19818 * "systemlist()" function
19819 */
19820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019821f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019822{
19823 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019824}
19825
19826/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019827 * "tabpagebuflist()" function
19828 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019830f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019831{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019832#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019833 tabpage_T *tp;
19834 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019835
19836 if (argvars[0].v_type == VAR_UNKNOWN)
19837 wp = firstwin;
19838 else
19839 {
19840 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19841 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019842 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019843 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019844 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019845 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019846 for (; wp != NULL; wp = wp->w_next)
19847 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019848 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019849 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019850 }
19851#endif
19852}
19853
19854
19855/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019856 * "tabpagenr()" function
19857 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019859f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019860{
19861 int nr = 1;
19862#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019863 char_u *arg;
19864
19865 if (argvars[0].v_type != VAR_UNKNOWN)
19866 {
19867 arg = get_tv_string_chk(&argvars[0]);
19868 nr = 0;
19869 if (arg != NULL)
19870 {
19871 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019872 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019873 else
19874 EMSG2(_(e_invexpr2), arg);
19875 }
19876 }
19877 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019878 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019879#endif
19880 rettv->vval.v_number = nr;
19881}
19882
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019883
19884#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019885static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019886
19887/*
19888 * Common code for tabpagewinnr() and winnr().
19889 */
19890 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019891get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019892{
19893 win_T *twin;
19894 int nr = 1;
19895 win_T *wp;
19896 char_u *arg;
19897
19898 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19899 if (argvar->v_type != VAR_UNKNOWN)
19900 {
19901 arg = get_tv_string_chk(argvar);
19902 if (arg == NULL)
19903 nr = 0; /* type error; errmsg already given */
19904 else if (STRCMP(arg, "$") == 0)
19905 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19906 else if (STRCMP(arg, "#") == 0)
19907 {
19908 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19909 if (twin == NULL)
19910 nr = 0;
19911 }
19912 else
19913 {
19914 EMSG2(_(e_invexpr2), arg);
19915 nr = 0;
19916 }
19917 }
19918
19919 if (nr > 0)
19920 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19921 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019922 {
19923 if (wp == NULL)
19924 {
19925 /* didn't find it in this tabpage */
19926 nr = 0;
19927 break;
19928 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019929 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019930 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019931 return nr;
19932}
19933#endif
19934
19935/*
19936 * "tabpagewinnr()" function
19937 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019939f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019940{
19941 int nr = 1;
19942#ifdef FEAT_WINDOWS
19943 tabpage_T *tp;
19944
19945 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19946 if (tp == NULL)
19947 nr = 0;
19948 else
19949 nr = get_winnr(tp, &argvars[1]);
19950#endif
19951 rettv->vval.v_number = nr;
19952}
19953
19954
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019955/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019956 * "tagfiles()" function
19957 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019959f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019960{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019961 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019962 tagname_T tn;
19963 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019964
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019965 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019966 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019967 fname = alloc(MAXPATHL);
19968 if (fname == NULL)
19969 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019970
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019971 for (first = TRUE; ; first = FALSE)
19972 if (get_tagfname(&tn, first, fname) == FAIL
19973 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019974 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019975 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019976 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019977}
19978
19979/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019980 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019981 */
19982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019983f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019984{
19985 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019986
19987 tag_pattern = get_tv_string(&argvars[0]);
19988
19989 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019990 if (*tag_pattern == NUL)
19991 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019992
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019993 if (rettv_list_alloc(rettv) == OK)
19994 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019995}
19996
19997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019998 * "tempname()" function
19999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020001f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020002{
20003 static int x = 'A';
20004
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020005 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020006 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020007
20008 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20009 * names. Skip 'I' and 'O', they are used for shell redirection. */
20010 do
20011 {
20012 if (x == 'Z')
20013 x = '0';
20014 else if (x == '9')
20015 x = 'A';
20016 else
20017 {
20018#ifdef EBCDIC
20019 if (x == 'I')
20020 x = 'J';
20021 else if (x == 'R')
20022 x = 'S';
20023 else
20024#endif
20025 ++x;
20026 }
20027 } while (x == 'I' || x == 'O');
20028}
20029
20030/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020031 * "test(list)" function: Just checking the walls...
20032 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020034f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020035{
20036 /* Used for unit testing. Change the code below to your liking. */
20037#if 0
20038 listitem_T *li;
20039 list_T *l;
20040 char_u *bad, *good;
20041
20042 if (argvars[0].v_type != VAR_LIST)
20043 return;
20044 l = argvars[0].vval.v_list;
20045 if (l == NULL)
20046 return;
20047 li = l->lv_first;
20048 if (li == NULL)
20049 return;
20050 bad = get_tv_string(&li->li_tv);
20051 li = li->li_next;
20052 if (li == NULL)
20053 return;
20054 good = get_tv_string(&li->li_tv);
20055 rettv->vval.v_number = test_edit_score(bad, good);
20056#endif
20057}
20058
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020059#ifdef FEAT_FLOAT
20060/*
20061 * "tan()" function
20062 */
20063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020064f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020065{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020066 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020067
20068 rettv->v_type = VAR_FLOAT;
20069 if (get_float_arg(argvars, &f) == OK)
20070 rettv->vval.v_float = tan(f);
20071 else
20072 rettv->vval.v_float = 0.0;
20073}
20074
20075/*
20076 * "tanh()" function
20077 */
20078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020079f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020080{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020081 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020082
20083 rettv->v_type = VAR_FLOAT;
20084 if (get_float_arg(argvars, &f) == OK)
20085 rettv->vval.v_float = tanh(f);
20086 else
20087 rettv->vval.v_float = 0.0;
20088}
20089#endif
20090
Bram Moolenaar975b5272016-03-15 23:10:59 +010020091#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20092/*
20093 * Get a callback from "arg". It can be a Funcref or a function name.
20094 * When "arg" is zero return an empty string.
20095 * Return NULL for an invalid argument.
20096 */
20097 char_u *
20098get_callback(typval_T *arg, partial_T **pp)
20099{
20100 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20101 {
20102 *pp = arg->vval.v_partial;
20103 return (*pp)->pt_name;
20104 }
20105 *pp = NULL;
20106 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20107 return arg->vval.v_string;
20108 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20109 return (char_u *)"";
20110 EMSG(_("E921: Invalid callback argument"));
20111 return NULL;
20112}
20113#endif
20114
20115#ifdef FEAT_TIMERS
20116/*
20117 * "timer_start(time, callback [, options])" function
20118 */
20119 static void
20120f_timer_start(typval_T *argvars, typval_T *rettv)
20121{
20122 long msec = get_tv_number(&argvars[0]);
20123 timer_T *timer;
20124 int repeat = 0;
20125 char_u *callback;
20126 dict_T *dict;
20127
20128 if (argvars[2].v_type != VAR_UNKNOWN)
20129 {
20130 if (argvars[2].v_type != VAR_DICT
20131 || (dict = argvars[2].vval.v_dict) == NULL)
20132 {
20133 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20134 return;
20135 }
20136 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20137 repeat = get_dict_number(dict, (char_u *)"repeat");
20138 }
20139
20140 timer = create_timer(msec, repeat);
20141 callback = get_callback(&argvars[1], &timer->tr_partial);
20142 if (callback == NULL)
20143 {
20144 stop_timer(timer);
20145 rettv->vval.v_number = -1;
20146 }
20147 else
20148 {
20149 timer->tr_callback = vim_strsave(callback);
20150 rettv->vval.v_number = timer->tr_id;
20151 }
20152}
20153
20154/*
20155 * "timer_stop(timer)" function
20156 */
20157 static void
20158f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20159{
20160 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20161
20162 if (timer != NULL)
20163 stop_timer(timer);
20164}
20165#endif
20166
Bram Moolenaard52d9742005-08-21 22:20:28 +000020167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020168 * "tolower(string)" function
20169 */
20170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020171f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020172{
20173 char_u *p;
20174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020175 p = vim_strsave(get_tv_string(&argvars[0]));
20176 rettv->v_type = VAR_STRING;
20177 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178
20179 if (p != NULL)
20180 while (*p != NUL)
20181 {
20182#ifdef FEAT_MBYTE
20183 int l;
20184
20185 if (enc_utf8)
20186 {
20187 int c, lc;
20188
20189 c = utf_ptr2char(p);
20190 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020191 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020192 /* TODO: reallocate string when byte count changes. */
20193 if (utf_char2len(lc) == l)
20194 utf_char2bytes(lc, p);
20195 p += l;
20196 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020197 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020198 p += l; /* skip multi-byte character */
20199 else
20200#endif
20201 {
20202 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20203 ++p;
20204 }
20205 }
20206}
20207
20208/*
20209 * "toupper(string)" function
20210 */
20211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020212f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020214 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020215 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020216}
20217
20218/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020219 * "tr(string, fromstr, tostr)" function
20220 */
20221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020222f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020223{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020224 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020225 char_u *fromstr;
20226 char_u *tostr;
20227 char_u *p;
20228#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020229 int inlen;
20230 int fromlen;
20231 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020232 int idx;
20233 char_u *cpstr;
20234 int cplen;
20235 int first = TRUE;
20236#endif
20237 char_u buf[NUMBUFLEN];
20238 char_u buf2[NUMBUFLEN];
20239 garray_T ga;
20240
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020241 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020242 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20243 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020244
20245 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020246 rettv->v_type = VAR_STRING;
20247 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020248 if (fromstr == NULL || tostr == NULL)
20249 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020250 ga_init2(&ga, (int)sizeof(char), 80);
20251
20252#ifdef FEAT_MBYTE
20253 if (!has_mbyte)
20254#endif
20255 /* not multi-byte: fromstr and tostr must be the same length */
20256 if (STRLEN(fromstr) != STRLEN(tostr))
20257 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020258#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020259error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020260#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020261 EMSG2(_(e_invarg2), fromstr);
20262 ga_clear(&ga);
20263 return;
20264 }
20265
20266 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020267 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020268 {
20269#ifdef FEAT_MBYTE
20270 if (has_mbyte)
20271 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020272 inlen = (*mb_ptr2len)(in_str);
20273 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020274 cplen = inlen;
20275 idx = 0;
20276 for (p = fromstr; *p != NUL; p += fromlen)
20277 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020278 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020279 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020280 {
20281 for (p = tostr; *p != NUL; p += tolen)
20282 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020283 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020284 if (idx-- == 0)
20285 {
20286 cplen = tolen;
20287 cpstr = p;
20288 break;
20289 }
20290 }
20291 if (*p == NUL) /* tostr is shorter than fromstr */
20292 goto error;
20293 break;
20294 }
20295 ++idx;
20296 }
20297
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020298 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020299 {
20300 /* Check that fromstr and tostr have the same number of
20301 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020302 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020303 first = FALSE;
20304 for (p = tostr; *p != NUL; p += tolen)
20305 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020306 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020307 --idx;
20308 }
20309 if (idx != 0)
20310 goto error;
20311 }
20312
Bram Moolenaarcde88542015-08-11 19:14:00 +020020313 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020314 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020315 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020316
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020317 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020318 }
20319 else
20320#endif
20321 {
20322 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020323 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020324 if (p != NULL)
20325 ga_append(&ga, tostr[p - fromstr]);
20326 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020327 ga_append(&ga, *in_str);
20328 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020329 }
20330 }
20331
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020332 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020333 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020334 ga_append(&ga, NUL);
20335
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020336 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020337}
20338
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020339#ifdef FEAT_FLOAT
20340/*
20341 * "trunc({float})" function
20342 */
20343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020344f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020345{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020346 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020347
20348 rettv->v_type = VAR_FLOAT;
20349 if (get_float_arg(argvars, &f) == OK)
20350 /* trunc() is not in C90, use floor() or ceil() instead. */
20351 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20352 else
20353 rettv->vval.v_float = 0.0;
20354}
20355#endif
20356
Bram Moolenaar8299df92004-07-10 09:47:34 +000020357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358 * "type(expr)" function
20359 */
20360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020361f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020363 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020364
20365 switch (argvars[0].v_type)
20366 {
20367 case VAR_NUMBER: n = 0; break;
20368 case VAR_STRING: n = 1; break;
20369 case VAR_FUNC: n = 2; break;
20370 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020371 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020372 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020373 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020374 if (argvars[0].vval.v_number == VVAL_FALSE
20375 || argvars[0].vval.v_number == VVAL_TRUE)
20376 n = 6;
20377 else
20378 n = 7;
20379 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020380 case VAR_JOB: n = 8; break;
20381 case VAR_CHANNEL: n = 9; break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010020382 case VAR_PARTIAL: n = 10; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020383 case VAR_UNKNOWN:
20384 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20385 n = -1;
20386 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020387 }
20388 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020389}
20390
20391/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020392 * "undofile(name)" function
20393 */
20394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020395f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020396{
20397 rettv->v_type = VAR_STRING;
20398#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020399 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020400 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020401
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020402 if (*fname == NUL)
20403 {
20404 /* If there is no file name there will be no undo file. */
20405 rettv->vval.v_string = NULL;
20406 }
20407 else
20408 {
20409 char_u *ffname = FullName_save(fname, FALSE);
20410
20411 if (ffname != NULL)
20412 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20413 vim_free(ffname);
20414 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020415 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020416#else
20417 rettv->vval.v_string = NULL;
20418#endif
20419}
20420
20421/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020422 * "undotree()" function
20423 */
20424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020425f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020426{
20427 if (rettv_dict_alloc(rettv) == OK)
20428 {
20429 dict_T *dict = rettv->vval.v_dict;
20430 list_T *list;
20431
Bram Moolenaar730cde92010-06-27 05:18:54 +020020432 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020433 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020434 dict_add_nr_str(dict, "save_last",
20435 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020436 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20437 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020438 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020439
20440 list = list_alloc();
20441 if (list != NULL)
20442 {
20443 u_eval_tree(curbuf->b_u_oldhead, list);
20444 dict_add_list(dict, "entries", list);
20445 }
20446 }
20447}
20448
20449/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020450 * "values(dict)" function
20451 */
20452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020453f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020454{
20455 dict_list(argvars, rettv, 1);
20456}
20457
20458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020459 * "virtcol(string)" function
20460 */
20461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020462f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463{
20464 colnr_T vcol = 0;
20465 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020466 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020468 fp = var2fpos(&argvars[0], FALSE, &fnum);
20469 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20470 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020471 {
20472 getvvcol(curwin, fp, NULL, NULL, &vcol);
20473 ++vcol;
20474 }
20475
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020476 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020477}
20478
20479/*
20480 * "visualmode()" function
20481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020482 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010020483f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020484{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020485 char_u str[2];
20486
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020487 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020488 str[0] = curbuf->b_visual_mode_eval;
20489 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020490 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020491
20492 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020493 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020494 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020495}
20496
20497/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020498 * "wildmenumode()" function
20499 */
20500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020501f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020502{
20503#ifdef FEAT_WILDMENU
20504 if (wild_menu_showing)
20505 rettv->vval.v_number = 1;
20506#endif
20507}
20508
20509/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020510 * "winbufnr(nr)" function
20511 */
20512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020513f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514{
20515 win_T *wp;
20516
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020517 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020518 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020519 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020521 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020522}
20523
20524/*
20525 * "wincol()" function
20526 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020528f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020529{
20530 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020531 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020532}
20533
20534/*
20535 * "winheight(nr)" function
20536 */
20537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020538f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539{
20540 win_T *wp;
20541
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020542 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020543 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020544 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020545 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020546 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020547}
20548
20549/*
20550 * "winline()" function
20551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020553f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020554{
20555 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020556 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020557}
20558
20559/*
20560 * "winnr()" function
20561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020563f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564{
20565 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020566
Bram Moolenaar071d4272004-06-13 20:20:40 +000020567#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020568 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020570 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020571}
20572
20573/*
20574 * "winrestcmd()" function
20575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020577f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020578{
20579#ifdef FEAT_WINDOWS
20580 win_T *wp;
20581 int winnr = 1;
20582 garray_T ga;
20583 char_u buf[50];
20584
20585 ga_init2(&ga, (int)sizeof(char), 70);
20586 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20587 {
20588 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20589 ga_concat(&ga, buf);
20590# ifdef FEAT_VERTSPLIT
20591 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20592 ga_concat(&ga, buf);
20593# endif
20594 ++winnr;
20595 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020596 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020597
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020598 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020599#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020600 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020601#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020602 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020603}
20604
20605/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020606 * "winrestview()" function
20607 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020609f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020610{
20611 dict_T *dict;
20612
20613 if (argvars[0].v_type != VAR_DICT
20614 || (dict = argvars[0].vval.v_dict) == NULL)
20615 EMSG(_(e_invarg));
20616 else
20617 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020618 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20619 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20620 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20621 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020622#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020623 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20624 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020625#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020626 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20627 {
20628 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20629 curwin->w_set_curswant = FALSE;
20630 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020631
Bram Moolenaar82c25852014-05-28 16:47:16 +020020632 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20633 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020634#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020635 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20636 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020637#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020638 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20639 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20640 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20641 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020642
20643 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020644 win_new_height(curwin, curwin->w_height);
20645# ifdef FEAT_VERTSPLIT
20646 win_new_width(curwin, W_WIDTH(curwin));
20647# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020648 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020649
Bram Moolenaarb851a962014-10-31 15:45:52 +010020650 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020651 curwin->w_topline = 1;
20652 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20653 curwin->w_topline = curbuf->b_ml.ml_line_count;
20654#ifdef FEAT_DIFF
20655 check_topfill(curwin, TRUE);
20656#endif
20657 }
20658}
20659
20660/*
20661 * "winsaveview()" function
20662 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020664f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020665{
20666 dict_T *dict;
20667
Bram Moolenaara800b422010-06-27 01:15:55 +020020668 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020669 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020670 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020671
20672 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20673 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20674#ifdef FEAT_VIRTUALEDIT
20675 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20676#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020677 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020678 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20679
20680 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20681#ifdef FEAT_DIFF
20682 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20683#endif
20684 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20685 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20686}
20687
20688/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020689 * "winwidth(nr)" function
20690 */
20691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020692f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020693{
20694 win_T *wp;
20695
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020696 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020697 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020698 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020699 else
20700#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020701 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020703 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020704#endif
20705}
20706
Bram Moolenaar071d4272004-06-13 20:20:40 +000020707/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020708 * "wordcount()" function
20709 */
20710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020711f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020712{
20713 if (rettv_dict_alloc(rettv) == FAIL)
20714 return;
20715 cursor_pos_info(rettv->vval.v_dict);
20716}
20717
20718/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020719 * Write list of strings to file
20720 */
20721 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020722write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020723{
20724 listitem_T *li;
20725 int c;
20726 int ret = OK;
20727 char_u *s;
20728
20729 for (li = list->lv_first; li != NULL; li = li->li_next)
20730 {
20731 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20732 {
20733 if (*s == '\n')
20734 c = putc(NUL, fd);
20735 else
20736 c = putc(*s, fd);
20737 if (c == EOF)
20738 {
20739 ret = FAIL;
20740 break;
20741 }
20742 }
20743 if (!binary || li->li_next != NULL)
20744 if (putc('\n', fd) == EOF)
20745 {
20746 ret = FAIL;
20747 break;
20748 }
20749 if (ret == FAIL)
20750 {
20751 EMSG(_(e_write));
20752 break;
20753 }
20754 }
20755 return ret;
20756}
20757
20758/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020759 * "writefile()" function
20760 */
20761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020762f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020763{
20764 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020765 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020766 char_u *fname;
20767 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020768 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020769
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020770 if (check_restricted() || check_secure())
20771 return;
20772
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020773 if (argvars[0].v_type != VAR_LIST)
20774 {
20775 EMSG2(_(e_listarg), "writefile()");
20776 return;
20777 }
20778 if (argvars[0].vval.v_list == NULL)
20779 return;
20780
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020781 if (argvars[2].v_type != VAR_UNKNOWN)
20782 {
20783 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20784 binary = TRUE;
20785 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20786 append = TRUE;
20787 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020788
20789 /* Always open the file in binary mode, library functions have a mind of
20790 * their own about CR-LF conversion. */
20791 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020792 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20793 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020794 {
20795 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20796 ret = -1;
20797 }
20798 else
20799 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020800 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20801 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020802 fclose(fd);
20803 }
20804
20805 rettv->vval.v_number = ret;
20806}
20807
20808/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020809 * "xor(expr, expr)" function
20810 */
20811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020812f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020813{
20814 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20815 ^ get_tv_number_chk(&argvars[1], NULL);
20816}
20817
20818
20819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020820 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020821 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020822 */
20823 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020824var2fpos(
20825 typval_T *varp,
20826 int dollar_lnum, /* TRUE when $ is last line */
20827 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020828{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020829 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020830 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020831 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020832
Bram Moolenaara5525202006-03-02 22:52:09 +000020833 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020834 if (varp->v_type == VAR_LIST)
20835 {
20836 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020837 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020838 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020839 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020840
20841 l = varp->vval.v_list;
20842 if (l == NULL)
20843 return NULL;
20844
20845 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020846 pos.lnum = list_find_nr(l, 0L, &error);
20847 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020848 return NULL; /* invalid line number */
20849
20850 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020851 pos.col = list_find_nr(l, 1L, &error);
20852 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020853 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020854 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020855
20856 /* We accept "$" for the column number: last column. */
20857 li = list_find(l, 1L);
20858 if (li != NULL && li->li_tv.v_type == VAR_STRING
20859 && li->li_tv.vval.v_string != NULL
20860 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20861 pos.col = len + 1;
20862
Bram Moolenaara5525202006-03-02 22:52:09 +000020863 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020864 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020865 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020866 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020867
Bram Moolenaara5525202006-03-02 22:52:09 +000020868#ifdef FEAT_VIRTUALEDIT
20869 /* Get the virtual offset. Defaults to zero. */
20870 pos.coladd = list_find_nr(l, 2L, &error);
20871 if (error)
20872 pos.coladd = 0;
20873#endif
20874
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020875 return &pos;
20876 }
20877
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020878 name = get_tv_string_chk(varp);
20879 if (name == NULL)
20880 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020881 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020882 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020883 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20884 {
20885 if (VIsual_active)
20886 return &VIsual;
20887 return &curwin->w_cursor;
20888 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020889 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020890 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020891 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020892 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20893 return NULL;
20894 return pp;
20895 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020896
20897#ifdef FEAT_VIRTUALEDIT
20898 pos.coladd = 0;
20899#endif
20900
Bram Moolenaar477933c2007-07-17 14:32:23 +000020901 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020902 {
20903 pos.col = 0;
20904 if (name[1] == '0') /* "w0": first visible line */
20905 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020906 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020907 pos.lnum = curwin->w_topline;
20908 return &pos;
20909 }
20910 else if (name[1] == '$') /* "w$": last visible line */
20911 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020912 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020913 pos.lnum = curwin->w_botline - 1;
20914 return &pos;
20915 }
20916 }
20917 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020918 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020919 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020920 {
20921 pos.lnum = curbuf->b_ml.ml_line_count;
20922 pos.col = 0;
20923 }
20924 else
20925 {
20926 pos.lnum = curwin->w_cursor.lnum;
20927 pos.col = (colnr_T)STRLEN(ml_get_curline());
20928 }
20929 return &pos;
20930 }
20931 return NULL;
20932}
20933
20934/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020935 * Convert list in "arg" into a position and optional file number.
20936 * When "fnump" is NULL there is no file number, only 3 items.
20937 * Note that the column is passed on as-is, the caller may want to decrement
20938 * it to use 1 for the first column.
20939 * Return FAIL when conversion is not possible, doesn't check the position for
20940 * validity.
20941 */
20942 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020943list2fpos(
20944 typval_T *arg,
20945 pos_T *posp,
20946 int *fnump,
20947 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020948{
20949 list_T *l = arg->vval.v_list;
20950 long i = 0;
20951 long n;
20952
Bram Moolenaar493c1782014-05-28 14:34:46 +020020953 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20954 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020955 if (arg->v_type != VAR_LIST
20956 || l == NULL
20957 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020958 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020959 return FAIL;
20960
20961 if (fnump != NULL)
20962 {
20963 n = list_find_nr(l, i++, NULL); /* fnum */
20964 if (n < 0)
20965 return FAIL;
20966 if (n == 0)
20967 n = curbuf->b_fnum; /* current buffer */
20968 *fnump = n;
20969 }
20970
20971 n = list_find_nr(l, i++, NULL); /* lnum */
20972 if (n < 0)
20973 return FAIL;
20974 posp->lnum = n;
20975
20976 n = list_find_nr(l, i++, NULL); /* col */
20977 if (n < 0)
20978 return FAIL;
20979 posp->col = n;
20980
20981#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020982 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020983 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020984 posp->coladd = 0;
20985 else
20986 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020987#endif
20988
Bram Moolenaar493c1782014-05-28 14:34:46 +020020989 if (curswantp != NULL)
20990 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20991
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020992 return OK;
20993}
20994
20995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020996 * Get the length of an environment variable name.
20997 * Advance "arg" to the first character after the name.
20998 * Return 0 for error.
20999 */
21000 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021001get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021002{
21003 char_u *p;
21004 int len;
21005
21006 for (p = *arg; vim_isIDc(*p); ++p)
21007 ;
21008 if (p == *arg) /* no name found */
21009 return 0;
21010
21011 len = (int)(p - *arg);
21012 *arg = p;
21013 return len;
21014}
21015
21016/*
21017 * Get the length of the name of a function or internal variable.
21018 * "arg" is advanced to the first non-white character after the name.
21019 * Return 0 if something is wrong.
21020 */
21021 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021022get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021023{
21024 char_u *p;
21025 int len;
21026
21027 /* Find the end of the name. */
21028 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021029 {
21030 if (*p == ':')
21031 {
21032 /* "s:" is start of "s:var", but "n:" is not and can be used in
21033 * slice "[n:]". Also "xx:" is not a namespace. */
21034 len = (int)(p - *arg);
21035 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21036 || len > 1)
21037 break;
21038 }
21039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021040 if (p == *arg) /* no name found */
21041 return 0;
21042
21043 len = (int)(p - *arg);
21044 *arg = skipwhite(p);
21045
21046 return len;
21047}
21048
21049/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021050 * Get the length of the name of a variable or function.
21051 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021052 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021053 * Return -1 if curly braces expansion failed.
21054 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021055 * If the name contains 'magic' {}'s, expand them and return the
21056 * expanded name in an allocated string via 'alias' - caller must free.
21057 */
21058 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021059get_name_len(
21060 char_u **arg,
21061 char_u **alias,
21062 int evaluate,
21063 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021064{
21065 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021066 char_u *p;
21067 char_u *expr_start;
21068 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021069
21070 *alias = NULL; /* default to no alias */
21071
21072 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21073 && (*arg)[2] == (int)KE_SNR)
21074 {
21075 /* hard coded <SNR>, already translated */
21076 *arg += 3;
21077 return get_id_len(arg) + 3;
21078 }
21079 len = eval_fname_script(*arg);
21080 if (len > 0)
21081 {
21082 /* literal "<SID>", "s:" or "<SNR>" */
21083 *arg += len;
21084 }
21085
Bram Moolenaar071d4272004-06-13 20:20:40 +000021086 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021087 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021089 p = find_name_end(*arg, &expr_start, &expr_end,
21090 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021091 if (expr_start != NULL)
21092 {
21093 char_u *temp_string;
21094
21095 if (!evaluate)
21096 {
21097 len += (int)(p - *arg);
21098 *arg = skipwhite(p);
21099 return len;
21100 }
21101
21102 /*
21103 * Include any <SID> etc in the expanded string:
21104 * Thus the -len here.
21105 */
21106 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21107 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021108 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021109 *alias = temp_string;
21110 *arg = skipwhite(p);
21111 return (int)STRLEN(temp_string);
21112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021113
21114 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021115 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021116 EMSG2(_(e_invexpr2), *arg);
21117
21118 return len;
21119}
21120
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021121/*
21122 * Find the end of a variable or function name, taking care of magic braces.
21123 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21124 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021125 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021126 * Return a pointer to just after the name. Equal to "arg" if there is no
21127 * valid name.
21128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021130find_name_end(
21131 char_u *arg,
21132 char_u **expr_start,
21133 char_u **expr_end,
21134 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021135{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021136 int mb_nest = 0;
21137 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021138 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021139 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021141 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021142 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021143 *expr_start = NULL;
21144 *expr_end = NULL;
21145 }
21146
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021147 /* Quick check for valid starting character. */
21148 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21149 return arg;
21150
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021151 for (p = arg; *p != NUL
21152 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021153 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021154 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021155 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021156 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021157 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021158 if (*p == '\'')
21159 {
21160 /* skip over 'string' to avoid counting [ and ] inside it. */
21161 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21162 ;
21163 if (*p == NUL)
21164 break;
21165 }
21166 else if (*p == '"')
21167 {
21168 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21169 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21170 if (*p == '\\' && p[1] != NUL)
21171 ++p;
21172 if (*p == NUL)
21173 break;
21174 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021175 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21176 {
21177 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021178 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021179 len = (int)(p - arg);
21180 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021181 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021182 break;
21183 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021185 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021186 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021187 if (*p == '[')
21188 ++br_nest;
21189 else if (*p == ']')
21190 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021193 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021194 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021195 if (*p == '{')
21196 {
21197 mb_nest++;
21198 if (expr_start != NULL && *expr_start == NULL)
21199 *expr_start = p;
21200 }
21201 else if (*p == '}')
21202 {
21203 mb_nest--;
21204 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21205 *expr_end = p;
21206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021208 }
21209
21210 return p;
21211}
21212
21213/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021214 * Expands out the 'magic' {}'s in a variable/function name.
21215 * Note that this can call itself recursively, to deal with
21216 * constructs like foo{bar}{baz}{bam}
21217 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21218 * "in_start" ^
21219 * "expr_start" ^
21220 * "expr_end" ^
21221 * "in_end" ^
21222 *
21223 * Returns a new allocated string, which the caller must free.
21224 * Returns NULL for failure.
21225 */
21226 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021227make_expanded_name(
21228 char_u *in_start,
21229 char_u *expr_start,
21230 char_u *expr_end,
21231 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021232{
21233 char_u c1;
21234 char_u *retval = NULL;
21235 char_u *temp_result;
21236 char_u *nextcmd = NULL;
21237
21238 if (expr_end == NULL || in_end == NULL)
21239 return NULL;
21240 *expr_start = NUL;
21241 *expr_end = NUL;
21242 c1 = *in_end;
21243 *in_end = NUL;
21244
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021245 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021246 if (temp_result != NULL && nextcmd == NULL)
21247 {
21248 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21249 + (in_end - expr_end) + 1));
21250 if (retval != NULL)
21251 {
21252 STRCPY(retval, in_start);
21253 STRCAT(retval, temp_result);
21254 STRCAT(retval, expr_end + 1);
21255 }
21256 }
21257 vim_free(temp_result);
21258
21259 *in_end = c1; /* put char back for error messages */
21260 *expr_start = '{';
21261 *expr_end = '}';
21262
21263 if (retval != NULL)
21264 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021265 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021266 if (expr_start != NULL)
21267 {
21268 /* Further expansion! */
21269 temp_result = make_expanded_name(retval, expr_start,
21270 expr_end, temp_result);
21271 vim_free(retval);
21272 retval = temp_result;
21273 }
21274 }
21275
21276 return retval;
21277}
21278
21279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021280 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021281 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282 */
21283 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021284eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021285{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021286 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21287}
21288
21289/*
21290 * Return TRUE if character "c" can be used as the first character in a
21291 * variable or function name (excluding '{' and '}').
21292 */
21293 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021294eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021295{
21296 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297}
21298
21299/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021300 * Set number v: variable to "val".
21301 */
21302 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021303set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021305 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306}
21307
21308/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021309 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021310 */
21311 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021312get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021313{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021314 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021315}
21316
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021317/*
21318 * Get string v: variable value. Uses a static buffer, can only be used once.
21319 */
21320 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021321get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021322{
21323 return get_tv_string(&vimvars[idx].vv_tv);
21324}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021325
Bram Moolenaar071d4272004-06-13 20:20:40 +000021326/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021327 * Get List v: variable value. Caller must take care of reference count when
21328 * needed.
21329 */
21330 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021331get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021332{
21333 return vimvars[idx].vv_list;
21334}
21335
21336/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021337 * Set v:char to character "c".
21338 */
21339 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021340set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021341{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021342 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021343
21344#ifdef FEAT_MBYTE
21345 if (has_mbyte)
21346 buf[(*mb_char2bytes)(c, buf)] = NUL;
21347 else
21348#endif
21349 {
21350 buf[0] = c;
21351 buf[1] = NUL;
21352 }
21353 set_vim_var_string(VV_CHAR, buf, -1);
21354}
21355
21356/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021357 * Set v:count to "count" and v:count1 to "count1".
21358 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 */
21360 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021361set_vcount(
21362 long count,
21363 long count1,
21364 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021366 if (set_prevcount)
21367 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021368 vimvars[VV_COUNT].vv_nr = count;
21369 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021370}
21371
21372/*
21373 * Set string v: variable to a copy of "val".
21374 */
21375 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021376set_vim_var_string(
21377 int idx,
21378 char_u *val,
21379 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021380{
Bram Moolenaara542c682016-01-31 16:28:04 +010021381 clear_tv(&vimvars[idx].vv_di.di_tv);
21382 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021384 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021386 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021387 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021388 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021389}
21390
21391/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021392 * Set List v: variable to "val".
21393 */
21394 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021395set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021396{
Bram Moolenaara542c682016-01-31 16:28:04 +010021397 clear_tv(&vimvars[idx].vv_di.di_tv);
21398 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021399 vimvars[idx].vv_list = val;
21400 if (val != NULL)
21401 ++val->lv_refcount;
21402}
21403
21404/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021405 * Set Dictionary v: variable to "val".
21406 */
21407 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021408set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021409{
21410 int todo;
21411 hashitem_T *hi;
21412
Bram Moolenaara542c682016-01-31 16:28:04 +010021413 clear_tv(&vimvars[idx].vv_di.di_tv);
21414 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021415 vimvars[idx].vv_dict = val;
21416 if (val != NULL)
21417 {
21418 ++val->dv_refcount;
21419
21420 /* Set readonly */
21421 todo = (int)val->dv_hashtab.ht_used;
21422 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21423 {
21424 if (HASHITEM_EMPTY(hi))
21425 continue;
21426 --todo;
21427 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21428 }
21429 }
21430}
21431
21432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 * Set v:register if needed.
21434 */
21435 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021436set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021437{
21438 char_u regname;
21439
21440 if (c == 0 || c == ' ')
21441 regname = '"';
21442 else
21443 regname = c;
21444 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021445 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021446 set_vim_var_string(VV_REG, &regname, 1);
21447}
21448
21449/*
21450 * Get or set v:exception. If "oldval" == NULL, return the current value.
21451 * Otherwise, restore the value to "oldval" and return NULL.
21452 * Must always be called in pairs to save and restore v:exception! Does not
21453 * take care of memory allocations.
21454 */
21455 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021456v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457{
21458 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021459 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021460
Bram Moolenaare9a41262005-01-15 22:18:47 +000021461 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021462 return NULL;
21463}
21464
21465/*
21466 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21467 * Otherwise, restore the value to "oldval" and return NULL.
21468 * Must always be called in pairs to save and restore v:throwpoint! Does not
21469 * take care of memory allocations.
21470 */
21471 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021472v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021473{
21474 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021475 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476
Bram Moolenaare9a41262005-01-15 22:18:47 +000021477 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021478 return NULL;
21479}
21480
21481#if defined(FEAT_AUTOCMD) || defined(PROTO)
21482/*
21483 * Set v:cmdarg.
21484 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21485 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21486 * Must always be called in pairs!
21487 */
21488 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021489set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021490{
21491 char_u *oldval;
21492 char_u *newval;
21493 unsigned len;
21494
Bram Moolenaare9a41262005-01-15 22:18:47 +000021495 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021496 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021497 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021498 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021499 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021500 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021501 }
21502
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021503 if (eap->force_bin == FORCE_BIN)
21504 len = 6;
21505 else if (eap->force_bin == FORCE_NOBIN)
21506 len = 8;
21507 else
21508 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021509
21510 if (eap->read_edit)
21511 len += 7;
21512
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021513 if (eap->force_ff != 0)
21514 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21515# ifdef FEAT_MBYTE
21516 if (eap->force_enc != 0)
21517 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021518 if (eap->bad_char != 0)
21519 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021520# endif
21521
21522 newval = alloc(len + 1);
21523 if (newval == NULL)
21524 return NULL;
21525
21526 if (eap->force_bin == FORCE_BIN)
21527 sprintf((char *)newval, " ++bin");
21528 else if (eap->force_bin == FORCE_NOBIN)
21529 sprintf((char *)newval, " ++nobin");
21530 else
21531 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021532
21533 if (eap->read_edit)
21534 STRCAT(newval, " ++edit");
21535
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021536 if (eap->force_ff != 0)
21537 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21538 eap->cmd + eap->force_ff);
21539# ifdef FEAT_MBYTE
21540 if (eap->force_enc != 0)
21541 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21542 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021543 if (eap->bad_char == BAD_KEEP)
21544 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21545 else if (eap->bad_char == BAD_DROP)
21546 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21547 else if (eap->bad_char != 0)
21548 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021549# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021550 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021551 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021552}
21553#endif
21554
21555/*
21556 * Get the value of internal variable "name".
21557 * Return OK or FAIL.
21558 */
21559 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021560get_var_tv(
21561 char_u *name,
21562 int len, /* length of "name" */
21563 typval_T *rettv, /* NULL when only checking existence */
21564 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21565 int verbose, /* may give error message */
21566 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567{
21568 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021569 typval_T *tv = NULL;
21570 typval_T atv;
21571 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021572 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021573
21574 /* truncate the name, so that we can use strcmp() */
21575 cc = name[len];
21576 name[len] = NUL;
21577
21578 /*
21579 * Check for "b:changedtick".
21580 */
21581 if (STRCMP(name, "b:changedtick") == 0)
21582 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021583 atv.v_type = VAR_NUMBER;
21584 atv.vval.v_number = curbuf->b_changedtick;
21585 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021586 }
21587
21588 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021589 * Check for user-defined variables.
21590 */
21591 else
21592 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021593 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021594 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021595 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021596 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021597 if (dip != NULL)
21598 *dip = v;
21599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021600 }
21601
Bram Moolenaare9a41262005-01-15 22:18:47 +000021602 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021603 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021604 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021605 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021606 ret = FAIL;
21607 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021608 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021609 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021610
21611 name[len] = cc;
21612
21613 return ret;
21614}
21615
21616/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021617 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21618 * Also handle function call with Funcref variable: func(expr)
21619 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21620 */
21621 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021622handle_subscript(
21623 char_u **arg,
21624 typval_T *rettv,
21625 int evaluate, /* do more than finding the end */
21626 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021627{
21628 int ret = OK;
21629 dict_T *selfdict = NULL;
21630 char_u *s;
21631 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021632 typval_T functv;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021633 partial_T *pt = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021634
21635 while (ret == OK
21636 && (**arg == '['
21637 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021638 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
21639 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021640 && !vim_iswhite(*(*arg - 1)))
21641 {
21642 if (**arg == '(')
21643 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021644 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021645 if (evaluate)
21646 {
21647 functv = *rettv;
21648 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021649
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021650 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021651 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021652 {
21653 pt = functv.vval.v_partial;
21654 s = pt->pt_name;
21655 }
21656 else
21657 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021658 }
21659 else
21660 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021661 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021662 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021663 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000021664
21665 /* Clear the funcref afterwards, so that deleting it while
21666 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021667 if (evaluate)
21668 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021669
21670 /* Stop the expression evaluation when immediately aborting on
21671 * error, or when an interrupt occurred or an exception was thrown
21672 * but not caught. */
21673 if (aborting())
21674 {
21675 if (ret == OK)
21676 clear_tv(rettv);
21677 ret = FAIL;
21678 }
21679 dict_unref(selfdict);
21680 selfdict = NULL;
21681 }
21682 else /* **arg == '[' || **arg == '.' */
21683 {
21684 dict_unref(selfdict);
21685 if (rettv->v_type == VAR_DICT)
21686 {
21687 selfdict = rettv->vval.v_dict;
21688 if (selfdict != NULL)
21689 ++selfdict->dv_refcount;
21690 }
21691 else
21692 selfdict = NULL;
21693 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21694 {
21695 clear_tv(rettv);
21696 ret = FAIL;
21697 }
21698 }
21699 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021700
21701 if (rettv->v_type == VAR_FUNC && selfdict != NULL)
21702 {
21703 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
21704
21705 /* Turn "dict.Func" into a partial for "Func" with "dict". */
21706 if (pt != NULL)
21707 {
Bram Moolenaar7a5c46a2016-03-16 20:41:21 +010021708 pt->pt_refcount = 1;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021709 pt->pt_dict = selfdict;
21710 selfdict = NULL;
21711 pt->pt_name = rettv->vval.v_string;
21712 func_ref(pt->pt_name);
21713 rettv->v_type = VAR_PARTIAL;
21714 rettv->vval.v_partial = pt;
21715 }
21716 }
21717
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021718 dict_unref(selfdict);
21719 return ret;
21720}
21721
21722/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021723 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021724 * value).
21725 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021726 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021727alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021728{
Bram Moolenaar33570922005-01-25 22:26:29 +000021729 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021730}
21731
21732/*
21733 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734 * The string "s" must have been allocated, it is consumed.
21735 * Return NULL for out of memory, the variable otherwise.
21736 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021737 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021738alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021739{
Bram Moolenaar33570922005-01-25 22:26:29 +000021740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021741
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021742 rettv = alloc_tv();
21743 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021744 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021745 rettv->v_type = VAR_STRING;
21746 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021747 }
21748 else
21749 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021750 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021751}
21752
21753/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021754 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021755 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021756 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021757free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021758{
21759 if (varp != NULL)
21760 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021761 switch (varp->v_type)
21762 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021763 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021764 func_unref(varp->vval.v_string);
21765 /*FALLTHROUGH*/
21766 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021767 vim_free(varp->vval.v_string);
21768 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021769 case VAR_PARTIAL:
21770 partial_unref(varp->vval.v_partial);
21771 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021772 case VAR_LIST:
21773 list_unref(varp->vval.v_list);
21774 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021775 case VAR_DICT:
21776 dict_unref(varp->vval.v_dict);
21777 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021778 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021779#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021780 job_unref(varp->vval.v_job);
21781 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021782#endif
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 channel_unref(varp->vval.v_channel);
21786 break;
21787#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021788 case VAR_NUMBER:
21789 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021790 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021791 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021792 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021794 vim_free(varp);
21795 }
21796}
21797
21798/*
21799 * Free the memory for a variable value and set the value to NULL or 0.
21800 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021801 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021802clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021803{
21804 if (varp != NULL)
21805 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021806 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021807 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021808 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021809 func_unref(varp->vval.v_string);
21810 /*FALLTHROUGH*/
21811 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021812 vim_free(varp->vval.v_string);
21813 varp->vval.v_string = NULL;
21814 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021815 case VAR_PARTIAL:
21816 partial_unref(varp->vval.v_partial);
21817 varp->vval.v_partial = NULL;
21818 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021819 case VAR_LIST:
21820 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021821 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021822 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021823 case VAR_DICT:
21824 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021825 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021826 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021827 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021828 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021829 varp->vval.v_number = 0;
21830 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021831 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021832#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021833 varp->vval.v_float = 0.0;
21834 break;
21835#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021836 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021837#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021838 job_unref(varp->vval.v_job);
21839 varp->vval.v_job = NULL;
21840#endif
21841 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021842 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021843#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021844 channel_unref(varp->vval.v_channel);
21845 varp->vval.v_channel = NULL;
21846#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021847 case VAR_UNKNOWN:
21848 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021849 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021850 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021851 }
21852}
21853
21854/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021855 * Set the value of a variable to NULL without freeing items.
21856 */
21857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021858init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021859{
21860 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021861 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021862}
21863
21864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021865 * Get the number value of a variable.
21866 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021867 * For incompatible types, return 0.
21868 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21869 * caller of incompatible types: it sets *denote to TRUE if "denote"
21870 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021871 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010021872 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021873get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021875 int error = FALSE;
21876
21877 return get_tv_number_chk(varp, &error); /* return 0L on error */
21878}
21879
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021880 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021881get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021882{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021883 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021885 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021887 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021888 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021889 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021890#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021891 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021892 break;
21893#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021894 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021895 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021896 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021897 break;
21898 case VAR_STRING:
21899 if (varp->vval.v_string != NULL)
21900 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021901 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021902 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021903 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021904 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021905 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021906 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021907 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021908 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021909 case VAR_SPECIAL:
21910 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21911 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021912 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021913#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021914 EMSG(_("E910: Using a Job as a Number"));
21915 break;
21916#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021917 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021918#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021919 EMSG(_("E913: Using a Channel as a Number"));
21920 break;
21921#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021922 case VAR_UNKNOWN:
21923 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021924 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021925 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021926 if (denote == NULL) /* useful for values that must be unsigned */
21927 n = -1;
21928 else
21929 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021930 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021931}
21932
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021933#ifdef FEAT_FLOAT
21934 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021935get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021936{
21937 switch (varp->v_type)
21938 {
21939 case VAR_NUMBER:
21940 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021941 case VAR_FLOAT:
21942 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021943 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021944 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021945 EMSG(_("E891: Using a Funcref as a Float"));
21946 break;
21947 case VAR_STRING:
21948 EMSG(_("E892: Using a String as a Float"));
21949 break;
21950 case VAR_LIST:
21951 EMSG(_("E893: Using a List as a Float"));
21952 break;
21953 case VAR_DICT:
21954 EMSG(_("E894: Using a Dictionary as a Float"));
21955 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021956 case VAR_SPECIAL:
21957 EMSG(_("E907: Using a special value as a Float"));
21958 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021959 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021960# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021961 EMSG(_("E911: Using a Job as a Float"));
21962 break;
21963# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021964 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021965# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021966 EMSG(_("E914: Using a Channel as a Float"));
21967 break;
21968# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021969 case VAR_UNKNOWN:
21970 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021971 break;
21972 }
21973 return 0;
21974}
21975#endif
21976
Bram Moolenaar071d4272004-06-13 20:20:40 +000021977/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021978 * Get the lnum from the first argument.
21979 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021980 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021981 */
21982 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021983get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021984{
Bram Moolenaar33570922005-01-25 22:26:29 +000021985 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021986 linenr_T lnum;
21987
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021988 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989 if (lnum == 0) /* no valid number, try using line() */
21990 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021991 rettv.v_type = VAR_NUMBER;
21992 f_line(argvars, &rettv);
21993 lnum = rettv.vval.v_number;
21994 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021995 }
21996 return lnum;
21997}
21998
21999/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022000 * Get the lnum from the first argument.
22001 * Also accepts "$", then "buf" is used.
22002 * Returns 0 on error.
22003 */
22004 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022005get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022006{
22007 if (argvars[0].v_type == VAR_STRING
22008 && argvars[0].vval.v_string != NULL
22009 && argvars[0].vval.v_string[0] == '$'
22010 && buf != NULL)
22011 return buf->b_ml.ml_line_count;
22012 return get_tv_number_chk(&argvars[0], NULL);
22013}
22014
22015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022016 * Get the string value of a variable.
22017 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022018 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22019 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022020 * If the String variable has never been set, return an empty string.
22021 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022022 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22023 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022024 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022025 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022026get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022027{
22028 static char_u mybuf[NUMBUFLEN];
22029
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022030 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022031}
22032
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022033 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022034get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022035{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022036 char_u *res = get_tv_string_buf_chk(varp, buf);
22037
22038 return res != NULL ? res : (char_u *)"";
22039}
22040
Bram Moolenaar7d647822014-04-05 21:28:56 +020022041/*
22042 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22043 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022044 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022045get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022046{
22047 static char_u mybuf[NUMBUFLEN];
22048
22049 return get_tv_string_buf_chk(varp, mybuf);
22050}
22051
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022052 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022053get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022054{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022055 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022056 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022057 case VAR_NUMBER:
22058 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22059 return buf;
22060 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022061 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022062 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022063 break;
22064 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022065 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022066 break;
22067 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022068 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022069 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022070 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022071#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022072 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022073 break;
22074#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022075 case VAR_STRING:
22076 if (varp->vval.v_string != NULL)
22077 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022078 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022079 case VAR_SPECIAL:
22080 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22081 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022082 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022083#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022084 {
22085 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022086 char *status;
22087
22088 if (job == NULL)
22089 return (char_u *)"no process";
22090 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022091 : job->jv_status == JOB_ENDED ? "dead"
22092 : "run";
22093# ifdef UNIX
22094 vim_snprintf((char *)buf, NUMBUFLEN,
22095 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022096# elif defined(WIN32)
22097 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022098 "process %ld %s",
22099 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022100 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022101# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022102 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022103 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22104# endif
22105 return buf;
22106 }
22107#endif
22108 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022109 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022110#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022111 {
22112 channel_T *channel = varp->vval.v_channel;
22113 char *status = channel_status(channel);
22114
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022115 if (channel == NULL)
22116 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22117 else
22118 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022119 "channel %d %s", channel->ch_id, status);
22120 return buf;
22121 }
22122#endif
22123 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022124 case VAR_UNKNOWN:
22125 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022126 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022127 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022128 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022129}
22130
22131/*
22132 * Find variable "name" in the list of variables.
22133 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022134 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022135 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022136 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022137 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022138 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022139find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022140{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022141 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022142 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022143
Bram Moolenaara7043832005-01-21 11:56:39 +000022144 ht = find_var_ht(name, &varname);
22145 if (htp != NULL)
22146 *htp = ht;
22147 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022148 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022149 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022150}
22151
22152/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022153 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022154 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022156 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022157find_var_in_ht(
22158 hashtab_T *ht,
22159 int htname,
22160 char_u *varname,
22161 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022162{
Bram Moolenaar33570922005-01-25 22:26:29 +000022163 hashitem_T *hi;
22164
22165 if (*varname == NUL)
22166 {
22167 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022168 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022169 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022170 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022171 case 'g': return &globvars_var;
22172 case 'v': return &vimvars_var;
22173 case 'b': return &curbuf->b_bufvar;
22174 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022175#ifdef FEAT_WINDOWS
22176 case 't': return &curtab->tp_winvar;
22177#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022178 case 'l': return current_funccal == NULL
22179 ? NULL : &current_funccal->l_vars_var;
22180 case 'a': return current_funccal == NULL
22181 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022182 }
22183 return NULL;
22184 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022185
22186 hi = hash_find(ht, varname);
22187 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022188 {
22189 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022190 * worked find the variable again. Don't auto-load a script if it was
22191 * loaded already, otherwise it would be loaded every time when
22192 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022193 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022194 {
22195 /* Note: script_autoload() may make "hi" invalid. It must either
22196 * be obtained again or not used. */
22197 if (!script_autoload(varname, FALSE) || aborting())
22198 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022199 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022200 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022201 if (HASHITEM_EMPTY(hi))
22202 return NULL;
22203 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022204 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022205}
22206
22207/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022208 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022209 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022210 * Set "varname" to the start of name without ':'.
22211 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022212 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022213find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022214{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022215 hashitem_T *hi;
22216
Bram Moolenaar73627d02015-08-11 15:46:09 +020022217 if (name[0] == NUL)
22218 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022219 if (name[1] != ':')
22220 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022221 /* The name must not start with a colon or #. */
22222 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022223 return NULL;
22224 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022225
22226 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022227 hi = hash_find(&compat_hashtab, name);
22228 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022229 return &compat_hashtab;
22230
Bram Moolenaar071d4272004-06-13 20:20:40 +000022231 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022232 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022233 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022234 }
22235 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022236 if (*name == 'g') /* global variable */
22237 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022238 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22239 */
22240 if (vim_strchr(name + 2, ':') != NULL
22241 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022242 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022243 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022244 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022245 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022246 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022247#ifdef FEAT_WINDOWS
22248 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022249 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022250#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022251 if (*name == 'v') /* v: variable */
22252 return &vimvarht;
22253 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022254 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022255 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022256 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022257 if (*name == 's' /* script variable */
22258 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22259 return &SCRIPT_VARS(current_SID);
22260 return NULL;
22261}
22262
22263/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022264 * Get function call environment based on bactrace debug level
22265 */
22266 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022267get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022268{
22269 int i;
22270 funccall_T *funccal;
22271 funccall_T *temp_funccal;
22272
22273 funccal = current_funccal;
22274 if (debug_backtrace_level > 0)
22275 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022276 for (i = 0; i < debug_backtrace_level; i++)
22277 {
22278 temp_funccal = funccal->caller;
22279 if (temp_funccal)
22280 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022281 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022282 /* backtrace level overflow. reset to max */
22283 debug_backtrace_level = i;
22284 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022285 }
22286 return funccal;
22287}
22288
22289/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022290 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022291 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022292 * Returns NULL when it doesn't exist.
22293 */
22294 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022295get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022296{
Bram Moolenaar33570922005-01-25 22:26:29 +000022297 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022298
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022299 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022300 if (v == NULL)
22301 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022302 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022303}
22304
22305/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022306 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022307 * sourcing this script and when executing functions defined in the script.
22308 */
22309 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022310new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022311{
Bram Moolenaara7043832005-01-21 11:56:39 +000022312 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022313 hashtab_T *ht;
22314 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022315
Bram Moolenaar071d4272004-06-13 20:20:40 +000022316 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22317 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022318 /* Re-allocating ga_data means that an ht_array pointing to
22319 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022320 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022321 for (i = 1; i <= ga_scripts.ga_len; ++i)
22322 {
22323 ht = &SCRIPT_VARS(i);
22324 if (ht->ht_mask == HT_INIT_SIZE - 1)
22325 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022326 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022327 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022328 }
22329
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330 while (ga_scripts.ga_len < id)
22331 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022332 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022333 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022334 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022335 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022336 }
22337 }
22338}
22339
22340/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022341 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22342 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022343 */
22344 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022345init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022346{
Bram Moolenaar33570922005-01-25 22:26:29 +000022347 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022348 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022349 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022350 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022351 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022352 dict_var->di_tv.vval.v_dict = dict;
22353 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022354 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022355 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22356 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022357}
22358
22359/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022360 * Unreference a dictionary initialized by init_var_dict().
22361 */
22362 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022363unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022364{
22365 /* Now the dict needs to be freed if no one else is using it, go back to
22366 * normal reference counting. */
22367 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22368 dict_unref(dict);
22369}
22370
22371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022373 * Frees all allocated variables and the value they contain.
22374 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022375 */
22376 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022377vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022378{
22379 vars_clear_ext(ht, TRUE);
22380}
22381
22382/*
22383 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22384 */
22385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022386vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022387{
Bram Moolenaara7043832005-01-21 11:56:39 +000022388 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022389 hashitem_T *hi;
22390 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022391
Bram Moolenaar33570922005-01-25 22:26:29 +000022392 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022393 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022394 for (hi = ht->ht_array; todo > 0; ++hi)
22395 {
22396 if (!HASHITEM_EMPTY(hi))
22397 {
22398 --todo;
22399
Bram Moolenaar33570922005-01-25 22:26:29 +000022400 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022401 * ht_array might change then. hash_clear() takes care of it
22402 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022403 v = HI2DI(hi);
22404 if (free_val)
22405 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022406 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022407 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022408 }
22409 }
22410 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022411 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022412}
22413
Bram Moolenaara7043832005-01-21 11:56:39 +000022414/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022415 * Delete a variable from hashtab "ht" at item "hi".
22416 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022417 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022419delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022420{
Bram Moolenaar33570922005-01-25 22:26:29 +000022421 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022422
22423 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022424 clear_tv(&di->di_tv);
22425 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426}
22427
22428/*
22429 * List the value of one internal variable.
22430 */
22431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022432list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022433{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022434 char_u *tofree;
22435 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022436 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022437
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022438 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022439 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022440 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022441 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022442}
22443
Bram Moolenaar071d4272004-06-13 20:20:40 +000022444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022445list_one_var_a(
22446 char_u *prefix,
22447 char_u *name,
22448 int type,
22449 char_u *string,
22450 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022451{
Bram Moolenaar31859182007-08-14 20:41:13 +000022452 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22453 msg_start();
22454 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022455 if (name != NULL) /* "a:" vars don't have a name stored */
22456 msg_puts(name);
22457 msg_putchar(' ');
22458 msg_advance(22);
22459 if (type == VAR_NUMBER)
22460 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022461 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022462 msg_putchar('*');
22463 else if (type == VAR_LIST)
22464 {
22465 msg_putchar('[');
22466 if (*string == '[')
22467 ++string;
22468 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022469 else if (type == VAR_DICT)
22470 {
22471 msg_putchar('{');
22472 if (*string == '{')
22473 ++string;
22474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022475 else
22476 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022477
Bram Moolenaar071d4272004-06-13 20:20:40 +000022478 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022479
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022480 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022481 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022482 if (*first)
22483 {
22484 msg_clr_eos();
22485 *first = FALSE;
22486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022487}
22488
22489/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022490 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022491 * If the variable already exists, the value is updated.
22492 * Otherwise the variable is created.
22493 */
22494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022495set_var(
22496 char_u *name,
22497 typval_T *tv,
22498 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022499{
Bram Moolenaar33570922005-01-25 22:26:29 +000022500 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022501 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022502 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022503
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022504 ht = find_var_ht(name, &varname);
22505 if (ht == NULL || *varname == NUL)
22506 {
22507 EMSG2(_(e_illvar), name);
22508 return;
22509 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022510 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022511
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022512 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
22513 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022514 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022515
Bram Moolenaar33570922005-01-25 22:26:29 +000022516 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022518 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022519 if (var_check_ro(v->di_flags, name, FALSE)
22520 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022521 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022522
22523 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022524 * Handle setting internal v: variables separately where needed to
22525 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022526 */
22527 if (ht == &vimvarht)
22528 {
22529 if (v->di_tv.v_type == VAR_STRING)
22530 {
22531 vim_free(v->di_tv.vval.v_string);
22532 if (copy || tv->v_type != VAR_STRING)
22533 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22534 else
22535 {
22536 /* Take over the string to avoid an extra alloc/free. */
22537 v->di_tv.vval.v_string = tv->vval.v_string;
22538 tv->vval.v_string = NULL;
22539 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022540 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022541 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022542 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022543 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022544 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022545 if (STRCMP(varname, "searchforward") == 0)
22546 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022547#ifdef FEAT_SEARCH_EXTRA
22548 else if (STRCMP(varname, "hlsearch") == 0)
22549 {
22550 no_hlsearch = !v->di_tv.vval.v_number;
22551 redraw_all_later(SOME_VALID);
22552 }
22553#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022554 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022555 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022556 else if (v->di_tv.v_type != tv->v_type)
22557 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022558 }
22559
22560 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022561 }
22562 else /* add a new variable */
22563 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022564 /* Can't add "v:" variable. */
22565 if (ht == &vimvarht)
22566 {
22567 EMSG2(_(e_illvar), name);
22568 return;
22569 }
22570
Bram Moolenaar92124a32005-06-17 22:03:40 +000022571 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022572 if (!valid_varname(varname))
22573 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022574
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022575 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22576 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022577 if (v == NULL)
22578 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022579 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022580 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022581 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022582 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022583 return;
22584 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022585 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022586 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022587
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022588 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022589 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022590 else
22591 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022592 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022593 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022594 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022596}
22597
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022598/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022599 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022600 * Also give an error message.
22601 */
22602 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022603var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022604{
22605 if (flags & DI_FLAGS_RO)
22606 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022607 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022608 return TRUE;
22609 }
22610 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22611 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022612 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022613 return TRUE;
22614 }
22615 return FALSE;
22616}
22617
22618/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022619 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22620 * Also give an error message.
22621 */
22622 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022623var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022624{
22625 if (flags & DI_FLAGS_FIX)
22626 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022627 EMSG2(_("E795: Cannot delete variable %s"),
22628 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022629 return TRUE;
22630 }
22631 return FALSE;
22632}
22633
22634/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022635 * Check if a funcref is assigned to a valid variable name.
22636 * Return TRUE and give an error if not.
22637 */
22638 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022639var_check_func_name(
22640 char_u *name, /* points to start of variable name */
22641 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022642{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022643 /* Allow for w: b: s: and t:. */
22644 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022645 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22646 ? name[2] : name[0]))
22647 {
22648 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22649 name);
22650 return TRUE;
22651 }
22652 /* Don't allow hiding a function. When "v" is not NULL we might be
22653 * assigning another function to the same var, the type is checked
22654 * below. */
22655 if (new_var && function_exists(name))
22656 {
22657 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22658 name);
22659 return TRUE;
22660 }
22661 return FALSE;
22662}
22663
22664/*
22665 * Check if a variable name is valid.
22666 * Return FALSE and give an error if not.
22667 */
22668 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022669valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022670{
22671 char_u *p;
22672
22673 for (p = varname; *p != NUL; ++p)
22674 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22675 && *p != AUTOLOAD_CHAR)
22676 {
22677 EMSG2(_(e_illvar), varname);
22678 return FALSE;
22679 }
22680 return TRUE;
22681}
22682
22683/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022684 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022685 * Also give an error message, using "name" or _("name") when use_gettext is
22686 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022687 */
22688 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022689tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022690{
22691 if (lock & VAR_LOCKED)
22692 {
22693 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022694 name == NULL ? (char_u *)_("Unknown")
22695 : use_gettext ? (char_u *)_(name)
22696 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022697 return TRUE;
22698 }
22699 if (lock & VAR_FIXED)
22700 {
22701 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022702 name == NULL ? (char_u *)_("Unknown")
22703 : use_gettext ? (char_u *)_(name)
22704 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022705 return TRUE;
22706 }
22707 return FALSE;
22708}
22709
22710/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022711 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022712 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022713 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022714 * It is OK for "from" and "to" to point to the same item. This is used to
22715 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022716 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022717 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022718copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022719{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022720 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022721 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022722 switch (from->v_type)
22723 {
22724 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022725 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022726 to->vval.v_number = from->vval.v_number;
22727 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022728 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022729#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022730 to->vval.v_float = from->vval.v_float;
22731 break;
22732#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022733 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022734#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022735 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010022736 if (to->vval.v_job != NULL)
22737 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022738 break;
22739#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022740 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022741#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022742 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022743 if (to->vval.v_channel != NULL)
22744 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022745 break;
22746#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022747 case VAR_STRING:
22748 case VAR_FUNC:
22749 if (from->vval.v_string == NULL)
22750 to->vval.v_string = NULL;
22751 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022752 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022753 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022754 if (from->v_type == VAR_FUNC)
22755 func_ref(to->vval.v_string);
22756 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022757 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022758 case VAR_PARTIAL:
22759 if (from->vval.v_partial == NULL)
22760 to->vval.v_partial = NULL;
22761 else
22762 {
22763 to->vval.v_partial = from->vval.v_partial;
22764 ++to->vval.v_partial->pt_refcount;
22765 }
22766 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022767 case VAR_LIST:
22768 if (from->vval.v_list == NULL)
22769 to->vval.v_list = NULL;
22770 else
22771 {
22772 to->vval.v_list = from->vval.v_list;
22773 ++to->vval.v_list->lv_refcount;
22774 }
22775 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022776 case VAR_DICT:
22777 if (from->vval.v_dict == NULL)
22778 to->vval.v_dict = NULL;
22779 else
22780 {
22781 to->vval.v_dict = from->vval.v_dict;
22782 ++to->vval.v_dict->dv_refcount;
22783 }
22784 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022785 case VAR_UNKNOWN:
22786 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022787 break;
22788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022789}
22790
22791/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022792 * Make a copy of an item.
22793 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022794 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22795 * reference to an already copied list/dict can be used.
22796 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022797 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022798 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022799item_copy(
22800 typval_T *from,
22801 typval_T *to,
22802 int deep,
22803 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022804{
22805 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022806 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022807
Bram Moolenaar33570922005-01-25 22:26:29 +000022808 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022809 {
22810 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022811 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022812 }
22813 ++recurse;
22814
22815 switch (from->v_type)
22816 {
22817 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022818 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022819 case VAR_STRING:
22820 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022821 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010022822 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022823 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022824 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022825 copy_tv(from, to);
22826 break;
22827 case VAR_LIST:
22828 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022829 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022830 if (from->vval.v_list == NULL)
22831 to->vval.v_list = NULL;
22832 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22833 {
22834 /* use the copy made earlier */
22835 to->vval.v_list = from->vval.v_list->lv_copylist;
22836 ++to->vval.v_list->lv_refcount;
22837 }
22838 else
22839 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22840 if (to->vval.v_list == NULL)
22841 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022842 break;
22843 case VAR_DICT:
22844 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022845 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022846 if (from->vval.v_dict == NULL)
22847 to->vval.v_dict = NULL;
22848 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22849 {
22850 /* use the copy made earlier */
22851 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22852 ++to->vval.v_dict->dv_refcount;
22853 }
22854 else
22855 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22856 if (to->vval.v_dict == NULL)
22857 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022858 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022859 case VAR_UNKNOWN:
22860 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022861 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022862 }
22863 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022864 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022865}
22866
22867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868 * ":echo expr1 ..." print each argument separated with a space, add a
22869 * newline at the end.
22870 * ":echon expr1 ..." print each argument plain.
22871 */
22872 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022873ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874{
22875 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022876 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022877 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022878 char_u *p;
22879 int needclr = TRUE;
22880 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022881 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022882
22883 if (eap->skip)
22884 ++emsg_skip;
22885 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22886 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022887 /* If eval1() causes an error message the text from the command may
22888 * still need to be cleared. E.g., "echo 22,44". */
22889 need_clr_eos = needclr;
22890
Bram Moolenaar071d4272004-06-13 20:20:40 +000022891 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022892 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893 {
22894 /*
22895 * Report the invalid expression unless the expression evaluation
22896 * has been cancelled due to an aborting error, an interrupt, or an
22897 * exception.
22898 */
22899 if (!aborting())
22900 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022901 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022902 break;
22903 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022904 need_clr_eos = FALSE;
22905
Bram Moolenaar071d4272004-06-13 20:20:40 +000022906 if (!eap->skip)
22907 {
22908 if (atstart)
22909 {
22910 atstart = FALSE;
22911 /* Call msg_start() after eval1(), evaluating the expression
22912 * may cause a message to appear. */
22913 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022914 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022915 /* Mark the saved text as finishing the line, so that what
22916 * follows is displayed on a new line when scrolling back
22917 * at the more prompt. */
22918 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921 }
22922 else if (eap->cmdidx == CMD_echo)
22923 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022924 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022925 if (p != NULL)
22926 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022927 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022928 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022929 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022930 if (*p != TAB && needclr)
22931 {
22932 /* remove any text still there from the command */
22933 msg_clr_eos();
22934 needclr = FALSE;
22935 }
22936 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022937 }
22938 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022939 {
22940#ifdef FEAT_MBYTE
22941 if (has_mbyte)
22942 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022943 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022944
22945 (void)msg_outtrans_len_attr(p, i, echo_attr);
22946 p += i - 1;
22947 }
22948 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022949#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022950 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022952 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022953 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022954 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022955 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022956 arg = skipwhite(arg);
22957 }
22958 eap->nextcmd = check_nextcmd(arg);
22959
22960 if (eap->skip)
22961 --emsg_skip;
22962 else
22963 {
22964 /* remove text that may still be there from the command */
22965 if (needclr)
22966 msg_clr_eos();
22967 if (eap->cmdidx == CMD_echo)
22968 msg_end();
22969 }
22970}
22971
22972/*
22973 * ":echohl {name}".
22974 */
22975 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022976ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022977{
22978 int id;
22979
22980 id = syn_name2id(eap->arg);
22981 if (id == 0)
22982 echo_attr = 0;
22983 else
22984 echo_attr = syn_id2attr(id);
22985}
22986
22987/*
22988 * ":execute expr1 ..." execute the result of an expression.
22989 * ":echomsg expr1 ..." Print a message
22990 * ":echoerr expr1 ..." Print an error
22991 * Each gets spaces around each argument and a newline at the end for
22992 * echo commands
22993 */
22994 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022995ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996{
22997 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022998 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022999 int ret = OK;
23000 char_u *p;
23001 garray_T ga;
23002 int len;
23003 int save_did_emsg;
23004
23005 ga_init2(&ga, 1, 80);
23006
23007 if (eap->skip)
23008 ++emsg_skip;
23009 while (*arg != NUL && *arg != '|' && *arg != '\n')
23010 {
23011 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023012 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023013 {
23014 /*
23015 * Report the invalid expression unless the expression evaluation
23016 * has been cancelled due to an aborting error, an interrupt, or an
23017 * exception.
23018 */
23019 if (!aborting())
23020 EMSG2(_(e_invexpr2), p);
23021 ret = FAIL;
23022 break;
23023 }
23024
23025 if (!eap->skip)
23026 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023027 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023028 len = (int)STRLEN(p);
23029 if (ga_grow(&ga, len + 2) == FAIL)
23030 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023031 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023032 ret = FAIL;
23033 break;
23034 }
23035 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023036 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023037 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023038 ga.ga_len += len;
23039 }
23040
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023041 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023042 arg = skipwhite(arg);
23043 }
23044
23045 if (ret != FAIL && ga.ga_data != NULL)
23046 {
23047 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023048 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023049 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023050 out_flush();
23051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023052 else if (eap->cmdidx == CMD_echoerr)
23053 {
23054 /* We don't want to abort following commands, restore did_emsg. */
23055 save_did_emsg = did_emsg;
23056 EMSG((char_u *)ga.ga_data);
23057 if (!force_abort)
23058 did_emsg = save_did_emsg;
23059 }
23060 else if (eap->cmdidx == CMD_execute)
23061 do_cmdline((char_u *)ga.ga_data,
23062 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23063 }
23064
23065 ga_clear(&ga);
23066
23067 if (eap->skip)
23068 --emsg_skip;
23069
23070 eap->nextcmd = check_nextcmd(arg);
23071}
23072
23073/*
23074 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23075 * "arg" points to the "&" or '+' when called, to "option" when returning.
23076 * Returns NULL when no option name found. Otherwise pointer to the char
23077 * after the option name.
23078 */
23079 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023080find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023081{
23082 char_u *p = *arg;
23083
23084 ++p;
23085 if (*p == 'g' && p[1] == ':')
23086 {
23087 *opt_flags = OPT_GLOBAL;
23088 p += 2;
23089 }
23090 else if (*p == 'l' && p[1] == ':')
23091 {
23092 *opt_flags = OPT_LOCAL;
23093 p += 2;
23094 }
23095 else
23096 *opt_flags = 0;
23097
23098 if (!ASCII_ISALPHA(*p))
23099 return NULL;
23100 *arg = p;
23101
23102 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23103 p += 4; /* termcap option */
23104 else
23105 while (ASCII_ISALPHA(*p))
23106 ++p;
23107 return p;
23108}
23109
23110/*
23111 * ":function"
23112 */
23113 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023114ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023115{
23116 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023117 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023118 int j;
23119 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023120 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023121 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023122 char_u *name = NULL;
23123 char_u *p;
23124 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023125 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023126 garray_T newargs;
23127 garray_T newlines;
23128 int varargs = FALSE;
23129 int mustend = FALSE;
23130 int flags = 0;
23131 ufunc_T *fp;
23132 int indent;
23133 int nesting;
23134 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023135 dictitem_T *v;
23136 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023137 static int func_nr = 0; /* number for nameless function */
23138 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023139 hashtab_T *ht;
23140 int todo;
23141 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023142 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023143
23144 /*
23145 * ":function" without argument: list functions.
23146 */
23147 if (ends_excmd(*eap->arg))
23148 {
23149 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023150 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023151 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023152 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023153 {
23154 if (!HASHITEM_EMPTY(hi))
23155 {
23156 --todo;
23157 fp = HI2UF(hi);
23158 if (!isdigit(*fp->uf_name))
23159 list_func_head(fp, FALSE);
23160 }
23161 }
23162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023163 eap->nextcmd = check_nextcmd(eap->arg);
23164 return;
23165 }
23166
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023167 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023168 * ":function /pat": list functions matching pattern.
23169 */
23170 if (*eap->arg == '/')
23171 {
23172 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23173 if (!eap->skip)
23174 {
23175 regmatch_T regmatch;
23176
23177 c = *p;
23178 *p = NUL;
23179 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23180 *p = c;
23181 if (regmatch.regprog != NULL)
23182 {
23183 regmatch.rm_ic = p_ic;
23184
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023185 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023186 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23187 {
23188 if (!HASHITEM_EMPTY(hi))
23189 {
23190 --todo;
23191 fp = HI2UF(hi);
23192 if (!isdigit(*fp->uf_name)
23193 && vim_regexec(&regmatch, fp->uf_name, 0))
23194 list_func_head(fp, FALSE);
23195 }
23196 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023197 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023198 }
23199 }
23200 if (*p == '/')
23201 ++p;
23202 eap->nextcmd = check_nextcmd(p);
23203 return;
23204 }
23205
23206 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023207 * Get the function name. There are these situations:
23208 * func normal function name
23209 * "name" == func, "fudi.fd_dict" == NULL
23210 * dict.func new dictionary entry
23211 * "name" == NULL, "fudi.fd_dict" set,
23212 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23213 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023214 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023215 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23216 * dict.func existing dict entry that's not a Funcref
23217 * "name" == NULL, "fudi.fd_dict" set,
23218 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023219 * s:func script-local function name
23220 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023221 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023222 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023223 name = trans_function_name(&p, eap->skip, 0, &fudi);
23224 paren = (vim_strchr(p, '(') != NULL);
23225 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023226 {
23227 /*
23228 * Return on an invalid expression in braces, unless the expression
23229 * evaluation has been cancelled due to an aborting error, an
23230 * interrupt, or an exception.
23231 */
23232 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023233 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023234 if (!eap->skip && fudi.fd_newkey != NULL)
23235 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023236 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023237 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023239 else
23240 eap->skip = TRUE;
23241 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023242
Bram Moolenaar071d4272004-06-13 20:20:40 +000023243 /* An error in a function call during evaluation of an expression in magic
23244 * braces should not cause the function not to be defined. */
23245 saved_did_emsg = did_emsg;
23246 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023247
23248 /*
23249 * ":function func" with only function name: list function.
23250 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023251 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023252 {
23253 if (!ends_excmd(*skipwhite(p)))
23254 {
23255 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023256 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023257 }
23258 eap->nextcmd = check_nextcmd(p);
23259 if (eap->nextcmd != NULL)
23260 *p = NUL;
23261 if (!eap->skip && !got_int)
23262 {
23263 fp = find_func(name);
23264 if (fp != NULL)
23265 {
23266 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023267 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023268 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023269 if (FUNCLINE(fp, j) == NULL)
23270 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023271 msg_putchar('\n');
23272 msg_outnum((long)(j + 1));
23273 if (j < 9)
23274 msg_putchar(' ');
23275 if (j < 99)
23276 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023277 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023278 out_flush(); /* show a line at a time */
23279 ui_breakcheck();
23280 }
23281 if (!got_int)
23282 {
23283 msg_putchar('\n');
23284 msg_puts((char_u *)" endfunction");
23285 }
23286 }
23287 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023288 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023289 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023290 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023291 }
23292
23293 /*
23294 * ":function name(arg1, arg2)" Define function.
23295 */
23296 p = skipwhite(p);
23297 if (*p != '(')
23298 {
23299 if (!eap->skip)
23300 {
23301 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023302 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023303 }
23304 /* attempt to continue by skipping some text */
23305 if (vim_strchr(p, '(') != NULL)
23306 p = vim_strchr(p, '(');
23307 }
23308 p = skipwhite(p + 1);
23309
23310 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23311 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23312
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023313 if (!eap->skip)
23314 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023315 /* Check the name of the function. Unless it's a dictionary function
23316 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023317 if (name != NULL)
23318 arg = name;
23319 else
23320 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023321 if (arg != NULL && (fudi.fd_di == NULL
23322 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023323 {
23324 if (*arg == K_SPECIAL)
23325 j = 3;
23326 else
23327 j = 0;
23328 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23329 : eval_isnamec(arg[j])))
23330 ++j;
23331 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023332 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023333 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023334 /* Disallow using the g: dict. */
23335 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23336 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023337 }
23338
Bram Moolenaar071d4272004-06-13 20:20:40 +000023339 /*
23340 * Isolate the arguments: "arg1, arg2, ...)"
23341 */
23342 while (*p != ')')
23343 {
23344 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23345 {
23346 varargs = TRUE;
23347 p += 3;
23348 mustend = TRUE;
23349 }
23350 else
23351 {
23352 arg = p;
23353 while (ASCII_ISALNUM(*p) || *p == '_')
23354 ++p;
23355 if (arg == p || isdigit(*arg)
23356 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23357 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23358 {
23359 if (!eap->skip)
23360 EMSG2(_("E125: Illegal argument: %s"), arg);
23361 break;
23362 }
23363 if (ga_grow(&newargs, 1) == FAIL)
23364 goto erret;
23365 c = *p;
23366 *p = NUL;
23367 arg = vim_strsave(arg);
23368 if (arg == NULL)
23369 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023370
23371 /* Check for duplicate argument name. */
23372 for (i = 0; i < newargs.ga_len; ++i)
23373 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23374 {
23375 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023376 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023377 goto erret;
23378 }
23379
Bram Moolenaar071d4272004-06-13 20:20:40 +000023380 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23381 *p = c;
23382 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023383 if (*p == ',')
23384 ++p;
23385 else
23386 mustend = TRUE;
23387 }
23388 p = skipwhite(p);
23389 if (mustend && *p != ')')
23390 {
23391 if (!eap->skip)
23392 EMSG2(_(e_invarg2), eap->arg);
23393 break;
23394 }
23395 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023396 if (*p != ')')
23397 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023398 ++p; /* skip the ')' */
23399
Bram Moolenaare9a41262005-01-15 22:18:47 +000023400 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023401 for (;;)
23402 {
23403 p = skipwhite(p);
23404 if (STRNCMP(p, "range", 5) == 0)
23405 {
23406 flags |= FC_RANGE;
23407 p += 5;
23408 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023409 else if (STRNCMP(p, "dict", 4) == 0)
23410 {
23411 flags |= FC_DICT;
23412 p += 4;
23413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023414 else if (STRNCMP(p, "abort", 5) == 0)
23415 {
23416 flags |= FC_ABORT;
23417 p += 5;
23418 }
23419 else
23420 break;
23421 }
23422
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023423 /* When there is a line break use what follows for the function body.
23424 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23425 if (*p == '\n')
23426 line_arg = p + 1;
23427 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023428 EMSG(_(e_trailing));
23429
23430 /*
23431 * Read the body of the function, until ":endfunction" is found.
23432 */
23433 if (KeyTyped)
23434 {
23435 /* Check if the function already exists, don't let the user type the
23436 * whole function before telling him it doesn't work! For a script we
23437 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023438 if (!eap->skip && !eap->forceit)
23439 {
23440 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23441 EMSG(_(e_funcdict));
23442 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023443 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023445
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023446 if (!eap->skip && did_emsg)
23447 goto erret;
23448
Bram Moolenaar071d4272004-06-13 20:20:40 +000023449 msg_putchar('\n'); /* don't overwrite the function name */
23450 cmdline_row = msg_row;
23451 }
23452
23453 indent = 2;
23454 nesting = 0;
23455 for (;;)
23456 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023457 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023458 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023459 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023460 saved_wait_return = FALSE;
23461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023462 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023463 sourcing_lnum_off = sourcing_lnum;
23464
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023465 if (line_arg != NULL)
23466 {
23467 /* Use eap->arg, split up in parts by line breaks. */
23468 theline = line_arg;
23469 p = vim_strchr(theline, '\n');
23470 if (p == NULL)
23471 line_arg += STRLEN(line_arg);
23472 else
23473 {
23474 *p = NUL;
23475 line_arg = p + 1;
23476 }
23477 }
23478 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023479 theline = getcmdline(':', 0L, indent);
23480 else
23481 theline = eap->getline(':', eap->cookie, indent);
23482 if (KeyTyped)
23483 lines_left = Rows - 1;
23484 if (theline == NULL)
23485 {
23486 EMSG(_("E126: Missing :endfunction"));
23487 goto erret;
23488 }
23489
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023490 /* Detect line continuation: sourcing_lnum increased more than one. */
23491 if (sourcing_lnum > sourcing_lnum_off + 1)
23492 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23493 else
23494 sourcing_lnum_off = 0;
23495
Bram Moolenaar071d4272004-06-13 20:20:40 +000023496 if (skip_until != NULL)
23497 {
23498 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23499 * don't check for ":endfunc". */
23500 if (STRCMP(theline, skip_until) == 0)
23501 {
23502 vim_free(skip_until);
23503 skip_until = NULL;
23504 }
23505 }
23506 else
23507 {
23508 /* skip ':' and blanks*/
23509 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23510 ;
23511
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023512 /* Check for "endfunction". */
23513 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023514 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023515 if (line_arg == NULL)
23516 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023517 break;
23518 }
23519
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023520 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023521 * at "end". */
23522 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23523 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023524 else if (STRNCMP(p, "if", 2) == 0
23525 || STRNCMP(p, "wh", 2) == 0
23526 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023527 || STRNCMP(p, "try", 3) == 0)
23528 indent += 2;
23529
23530 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023531 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023532 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023533 if (*p == '!')
23534 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023535 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023536 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23537 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023538 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023539 ++nesting;
23540 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541 }
23542 }
23543
23544 /* Check for ":append" or ":insert". */
23545 p = skip_range(p, NULL);
23546 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23547 || (p[0] == 'i'
23548 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23549 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23550 skip_until = vim_strsave((char_u *)".");
23551
23552 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23553 arg = skipwhite(skiptowhite(p));
23554 if (arg[0] == '<' && arg[1] =='<'
23555 && ((p[0] == 'p' && p[1] == 'y'
23556 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23557 || (p[0] == 'p' && p[1] == 'e'
23558 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23559 || (p[0] == 't' && p[1] == 'c'
23560 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023561 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23562 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023563 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23564 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023565 || (p[0] == 'm' && p[1] == 'z'
23566 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023567 ))
23568 {
23569 /* ":python <<" continues until a dot, like ":append" */
23570 p = skipwhite(arg + 2);
23571 if (*p == NUL)
23572 skip_until = vim_strsave((char_u *)".");
23573 else
23574 skip_until = vim_strsave(p);
23575 }
23576 }
23577
23578 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023579 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023580 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023581 if (line_arg == NULL)
23582 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023583 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023584 }
23585
23586 /* Copy the line to newly allocated memory. get_one_sourceline()
23587 * allocates 250 bytes per line, this saves 80% on average. The cost
23588 * is an extra alloc/free. */
23589 p = vim_strsave(theline);
23590 if (p != NULL)
23591 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023592 if (line_arg == NULL)
23593 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023594 theline = p;
23595 }
23596
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023597 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23598
23599 /* Add NULL lines for continuation lines, so that the line count is
23600 * equal to the index in the growarray. */
23601 while (sourcing_lnum_off-- > 0)
23602 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023603
23604 /* Check for end of eap->arg. */
23605 if (line_arg != NULL && *line_arg == NUL)
23606 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023607 }
23608
23609 /* Don't define the function when skipping commands or when an error was
23610 * detected. */
23611 if (eap->skip || did_emsg)
23612 goto erret;
23613
23614 /*
23615 * If there are no errors, add the function
23616 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023617 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023618 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023619 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023620 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023621 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023622 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023623 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023624 goto erret;
23625 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023626
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023627 fp = find_func(name);
23628 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023629 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023630 if (!eap->forceit)
23631 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023632 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023633 goto erret;
23634 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023635 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023636 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023637 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023638 name);
23639 goto erret;
23640 }
23641 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023642 ga_clear_strings(&(fp->uf_args));
23643 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023644 vim_free(name);
23645 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023647 }
23648 else
23649 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023650 char numbuf[20];
23651
23652 fp = NULL;
23653 if (fudi.fd_newkey == NULL && !eap->forceit)
23654 {
23655 EMSG(_(e_funcdict));
23656 goto erret;
23657 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023658 if (fudi.fd_di == NULL)
23659 {
23660 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023661 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023662 goto erret;
23663 }
23664 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023665 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023666 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023667
23668 /* Give the function a sequential number. Can only be used with a
23669 * Funcref! */
23670 vim_free(name);
23671 sprintf(numbuf, "%d", ++func_nr);
23672 name = vim_strsave((char_u *)numbuf);
23673 if (name == NULL)
23674 goto erret;
23675 }
23676
23677 if (fp == NULL)
23678 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023679 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023680 {
23681 int slen, plen;
23682 char_u *scriptname;
23683
23684 /* Check that the autoload name matches the script name. */
23685 j = FAIL;
23686 if (sourcing_name != NULL)
23687 {
23688 scriptname = autoload_name(name);
23689 if (scriptname != NULL)
23690 {
23691 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023692 plen = (int)STRLEN(p);
23693 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023694 if (slen > plen && fnamecmp(p,
23695 sourcing_name + slen - plen) == 0)
23696 j = OK;
23697 vim_free(scriptname);
23698 }
23699 }
23700 if (j == FAIL)
23701 {
23702 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23703 goto erret;
23704 }
23705 }
23706
23707 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023708 if (fp == NULL)
23709 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023710
23711 if (fudi.fd_dict != NULL)
23712 {
23713 if (fudi.fd_di == NULL)
23714 {
23715 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023716 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023717 if (fudi.fd_di == NULL)
23718 {
23719 vim_free(fp);
23720 goto erret;
23721 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023722 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23723 {
23724 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023725 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023726 goto erret;
23727 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023728 }
23729 else
23730 /* overwrite existing dict entry */
23731 clear_tv(&fudi.fd_di->di_tv);
23732 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023733 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023734 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023735 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023736
23737 /* behave like "dict" was used */
23738 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023739 }
23740
Bram Moolenaar071d4272004-06-13 20:20:40 +000023741 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023742 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023743 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23744 {
23745 vim_free(fp);
23746 goto erret;
23747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023748 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023749 fp->uf_args = newargs;
23750 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023751#ifdef FEAT_PROFILE
23752 fp->uf_tml_count = NULL;
23753 fp->uf_tml_total = NULL;
23754 fp->uf_tml_self = NULL;
23755 fp->uf_profiling = FALSE;
23756 if (prof_def_func())
23757 func_do_profile(fp);
23758#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023759 fp->uf_varargs = varargs;
23760 fp->uf_flags = flags;
23761 fp->uf_calls = 0;
23762 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023763 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023764
23765erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023766 ga_clear_strings(&newargs);
23767 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023768ret_free:
23769 vim_free(skip_until);
23770 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023771 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023772 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023773 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023774}
23775
23776/*
23777 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023778 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023779 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023780 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023781 * TFN_INT: internal function name OK
23782 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023783 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023784 * Advances "pp" to just after the function name (if no error).
23785 */
23786 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023787trans_function_name(
23788 char_u **pp,
23789 int skip, /* only find the end, don't evaluate */
23790 int flags,
23791 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023792{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023793 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023794 char_u *start;
23795 char_u *end;
23796 int lead;
23797 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023798 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023799 lval_T lv;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023800 partial_T *partial;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023801
23802 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023803 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023804 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023805
23806 /* Check for hard coded <SNR>: already translated function ID (from a user
23807 * command). */
23808 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23809 && (*pp)[2] == (int)KE_SNR)
23810 {
23811 *pp += 3;
23812 len = get_id_len(pp) + 3;
23813 return vim_strnsave(start, len);
23814 }
23815
23816 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23817 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023818 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023819 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023820 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023821
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023822 /* Note that TFN_ flags use the same values as GLV_ flags. */
23823 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023824 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023825 if (end == start)
23826 {
23827 if (!skip)
23828 EMSG(_("E129: Function name required"));
23829 goto theend;
23830 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023831 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023832 {
23833 /*
23834 * Report an invalid expression in braces, unless the expression
23835 * evaluation has been cancelled due to an aborting error, an
23836 * interrupt, or an exception.
23837 */
23838 if (!aborting())
23839 {
23840 if (end != NULL)
23841 EMSG2(_(e_invarg2), start);
23842 }
23843 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023844 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023845 goto theend;
23846 }
23847
23848 if (lv.ll_tv != NULL)
23849 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023850 if (fdp != NULL)
23851 {
23852 fdp->fd_dict = lv.ll_dict;
23853 fdp->fd_newkey = lv.ll_newkey;
23854 lv.ll_newkey = NULL;
23855 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023856 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023857 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23858 {
23859 name = vim_strsave(lv.ll_tv->vval.v_string);
23860 *pp = end;
23861 }
23862 else
23863 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023864 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23865 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023866 EMSG(_(e_funcref));
23867 else
23868 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023869 name = NULL;
23870 }
23871 goto theend;
23872 }
23873
23874 if (lv.ll_name == NULL)
23875 {
23876 /* Error found, but continue after the function name. */
23877 *pp = end;
23878 goto theend;
23879 }
23880
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023881 /* Check if the name is a Funcref. If so, use the value. */
23882 if (lv.ll_exp_name != NULL)
23883 {
23884 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023885 name = deref_func_name(lv.ll_exp_name, &len, &partial,
23886 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023887 if (name == lv.ll_exp_name)
23888 name = NULL;
23889 }
23890 else
23891 {
23892 len = (int)(end - *pp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023893 name = deref_func_name(*pp, &len, &partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023894 if (name == *pp)
23895 name = NULL;
23896 }
23897 if (name != NULL)
23898 {
23899 name = vim_strsave(name);
23900 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023901 if (STRNCMP(name, "<SNR>", 5) == 0)
23902 {
23903 /* Change "<SNR>" to the byte sequence. */
23904 name[0] = K_SPECIAL;
23905 name[1] = KS_EXTRA;
23906 name[2] = (int)KE_SNR;
23907 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23908 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023909 goto theend;
23910 }
23911
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023912 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023913 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023914 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023915 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23916 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23917 {
23918 /* When there was "s:" already or the name expanded to get a
23919 * leading "s:" then remove it. */
23920 lv.ll_name += 2;
23921 len -= 2;
23922 lead = 2;
23923 }
23924 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023925 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023926 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023927 /* skip over "s:" and "g:" */
23928 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023929 lv.ll_name += 2;
23930 len = (int)(end - lv.ll_name);
23931 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023932
23933 /*
23934 * Copy the function name to allocated memory.
23935 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23936 * Accept <SNR>123_name() outside a script.
23937 */
23938 if (skip)
23939 lead = 0; /* do nothing */
23940 else if (lead > 0)
23941 {
23942 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023943 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23944 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023945 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023946 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023947 if (current_SID <= 0)
23948 {
23949 EMSG(_(e_usingsid));
23950 goto theend;
23951 }
23952 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23953 lead += (int)STRLEN(sid_buf);
23954 }
23955 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023956 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023957 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023958 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023959 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023960 goto theend;
23961 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023962 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023963 {
23964 char_u *cp = vim_strchr(lv.ll_name, ':');
23965
23966 if (cp != NULL && cp < end)
23967 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023968 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023969 goto theend;
23970 }
23971 }
23972
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023973 name = alloc((unsigned)(len + lead + 1));
23974 if (name != NULL)
23975 {
23976 if (lead > 0)
23977 {
23978 name[0] = K_SPECIAL;
23979 name[1] = KS_EXTRA;
23980 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023981 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023982 STRCPY(name + 3, sid_buf);
23983 }
23984 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023985 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023986 }
23987 *pp = end;
23988
23989theend:
23990 clear_lval(&lv);
23991 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023992}
23993
23994/*
23995 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23996 * Return 2 if "p" starts with "s:".
23997 * Return 0 otherwise.
23998 */
23999 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024000eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024001{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024002 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24003 * the standard library function. */
24004 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24005 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024006 return 5;
24007 if (p[0] == 's' && p[1] == ':')
24008 return 2;
24009 return 0;
24010}
24011
24012/*
24013 * Return TRUE if "p" starts with "<SID>" or "s:".
24014 * Only works if eval_fname_script() returned non-zero for "p"!
24015 */
24016 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024017eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024018{
24019 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24020}
24021
24022/*
24023 * List the head of the function: "name(arg1, arg2)".
24024 */
24025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024026list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024027{
24028 int j;
24029
24030 msg_start();
24031 if (indent)
24032 MSG_PUTS(" ");
24033 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024034 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024035 {
24036 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024037 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024038 }
24039 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024040 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024041 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024042 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024043 {
24044 if (j)
24045 MSG_PUTS(", ");
24046 msg_puts(FUNCARG(fp, j));
24047 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024048 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024049 {
24050 if (j)
24051 MSG_PUTS(", ");
24052 MSG_PUTS("...");
24053 }
24054 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024055 if (fp->uf_flags & FC_ABORT)
24056 MSG_PUTS(" abort");
24057 if (fp->uf_flags & FC_RANGE)
24058 MSG_PUTS(" range");
24059 if (fp->uf_flags & FC_DICT)
24060 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024061 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024062 if (p_verbose > 0)
24063 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024064}
24065
24066/*
24067 * Find a function by name, return pointer to it in ufuncs.
24068 * Return NULL for unknown function.
24069 */
24070 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024071find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024072{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024073 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024074
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024075 hi = hash_find(&func_hashtab, name);
24076 if (!HASHITEM_EMPTY(hi))
24077 return HI2UF(hi);
24078 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024079}
24080
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024081#if defined(EXITFREE) || defined(PROTO)
24082 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024083free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024084{
24085 hashitem_T *hi;
24086
24087 /* Need to start all over every time, because func_free() may change the
24088 * hash table. */
24089 while (func_hashtab.ht_used > 0)
24090 for (hi = func_hashtab.ht_array; ; ++hi)
24091 if (!HASHITEM_EMPTY(hi))
24092 {
24093 func_free(HI2UF(hi));
24094 break;
24095 }
24096}
24097#endif
24098
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024099 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024100translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024101{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024102 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024103 return find_internal_func(name) >= 0;
24104 return find_func(name) != NULL;
24105}
24106
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024107/*
24108 * Return TRUE if a function "name" exists.
24109 */
24110 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024111function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024112{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024113 char_u *nm = name;
24114 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024115 int n = FALSE;
24116
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024117 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
24118 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024119 nm = skipwhite(nm);
24120
24121 /* Only accept "funcname", "funcname ", "funcname (..." and
24122 * "funcname(...", not "funcname!...". */
24123 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024124 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024125 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024126 return n;
24127}
24128
Bram Moolenaara1544c02013-05-30 12:35:52 +020024129 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024130get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024131{
24132 char_u *nm = name;
24133 char_u *p;
24134
24135 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
24136
24137 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024138 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024139 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024140
Bram Moolenaara1544c02013-05-30 12:35:52 +020024141 vim_free(p);
24142 return NULL;
24143}
24144
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024145/*
24146 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024147 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24148 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024149 */
24150 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024151builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024152{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024153 char_u *p;
24154
24155 if (!ASCII_ISLOWER(name[0]))
24156 return FALSE;
24157 p = vim_strchr(name, AUTOLOAD_CHAR);
24158 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024159}
24160
Bram Moolenaar05159a02005-02-26 23:04:13 +000024161#if defined(FEAT_PROFILE) || defined(PROTO)
24162/*
24163 * Start profiling function "fp".
24164 */
24165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024166func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024167{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024168 int len = fp->uf_lines.ga_len;
24169
24170 if (len == 0)
24171 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024172 fp->uf_tm_count = 0;
24173 profile_zero(&fp->uf_tm_self);
24174 profile_zero(&fp->uf_tm_total);
24175 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024176 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024177 if (fp->uf_tml_total == NULL)
24178 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024179 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024180 if (fp->uf_tml_self == NULL)
24181 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024182 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024183 fp->uf_tml_idx = -1;
24184 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24185 || fp->uf_tml_self == NULL)
24186 return; /* out of memory */
24187
24188 fp->uf_profiling = TRUE;
24189}
24190
24191/*
24192 * Dump the profiling results for all functions in file "fd".
24193 */
24194 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024195func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024196{
24197 hashitem_T *hi;
24198 int todo;
24199 ufunc_T *fp;
24200 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024201 ufunc_T **sorttab;
24202 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024203
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024204 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024205 if (todo == 0)
24206 return; /* nothing to dump */
24207
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024208 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024209
Bram Moolenaar05159a02005-02-26 23:04:13 +000024210 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24211 {
24212 if (!HASHITEM_EMPTY(hi))
24213 {
24214 --todo;
24215 fp = HI2UF(hi);
24216 if (fp->uf_profiling)
24217 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024218 if (sorttab != NULL)
24219 sorttab[st_len++] = fp;
24220
Bram Moolenaar05159a02005-02-26 23:04:13 +000024221 if (fp->uf_name[0] == K_SPECIAL)
24222 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24223 else
24224 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24225 if (fp->uf_tm_count == 1)
24226 fprintf(fd, "Called 1 time\n");
24227 else
24228 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24229 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24230 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24231 fprintf(fd, "\n");
24232 fprintf(fd, "count total (s) self (s)\n");
24233
24234 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24235 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024236 if (FUNCLINE(fp, i) == NULL)
24237 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024238 prof_func_line(fd, fp->uf_tml_count[i],
24239 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024240 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24241 }
24242 fprintf(fd, "\n");
24243 }
24244 }
24245 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024246
24247 if (sorttab != NULL && st_len > 0)
24248 {
24249 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24250 prof_total_cmp);
24251 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24252 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24253 prof_self_cmp);
24254 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24255 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024256
24257 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024258}
Bram Moolenaar73830342005-02-28 22:48:19 +000024259
24260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024261prof_sort_list(
24262 FILE *fd,
24263 ufunc_T **sorttab,
24264 int st_len,
24265 char *title,
24266 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024267{
24268 int i;
24269 ufunc_T *fp;
24270
24271 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24272 fprintf(fd, "count total (s) self (s) function\n");
24273 for (i = 0; i < 20 && i < st_len; ++i)
24274 {
24275 fp = sorttab[i];
24276 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24277 prefer_self);
24278 if (fp->uf_name[0] == K_SPECIAL)
24279 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24280 else
24281 fprintf(fd, " %s()\n", fp->uf_name);
24282 }
24283 fprintf(fd, "\n");
24284}
24285
24286/*
24287 * Print the count and times for one function or function line.
24288 */
24289 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024290prof_func_line(
24291 FILE *fd,
24292 int count,
24293 proftime_T *total,
24294 proftime_T *self,
24295 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024296{
24297 if (count > 0)
24298 {
24299 fprintf(fd, "%5d ", count);
24300 if (prefer_self && profile_equal(total, self))
24301 fprintf(fd, " ");
24302 else
24303 fprintf(fd, "%s ", profile_msg(total));
24304 if (!prefer_self && profile_equal(total, self))
24305 fprintf(fd, " ");
24306 else
24307 fprintf(fd, "%s ", profile_msg(self));
24308 }
24309 else
24310 fprintf(fd, " ");
24311}
24312
24313/*
24314 * Compare function for total time sorting.
24315 */
24316 static int
24317#ifdef __BORLANDC__
24318_RTLENTRYF
24319#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024320prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024321{
24322 ufunc_T *p1, *p2;
24323
24324 p1 = *(ufunc_T **)s1;
24325 p2 = *(ufunc_T **)s2;
24326 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24327}
24328
24329/*
24330 * Compare function for self time sorting.
24331 */
24332 static int
24333#ifdef __BORLANDC__
24334_RTLENTRYF
24335#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024336prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024337{
24338 ufunc_T *p1, *p2;
24339
24340 p1 = *(ufunc_T **)s1;
24341 p2 = *(ufunc_T **)s2;
24342 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24343}
24344
Bram Moolenaar05159a02005-02-26 23:04:13 +000024345#endif
24346
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024347/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024348 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024349 * Return TRUE if a package was loaded.
24350 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024351 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024352script_autoload(
24353 char_u *name,
24354 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024355{
24356 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024357 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024358 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024359 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024360
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024361 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024362 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024363 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024364 return FALSE;
24365
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024366 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024367
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024368 /* Find the name in the list of previously loaded package names. Skip
24369 * "autoload/", it's always the same. */
24370 for (i = 0; i < ga_loaded.ga_len; ++i)
24371 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24372 break;
24373 if (!reload && i < ga_loaded.ga_len)
24374 ret = FALSE; /* was loaded already */
24375 else
24376 {
24377 /* Remember the name if it wasn't loaded already. */
24378 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24379 {
24380 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24381 tofree = NULL;
24382 }
24383
24384 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024385 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024386 ret = TRUE;
24387 }
24388
24389 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024390 return ret;
24391}
24392
24393/*
24394 * Return the autoload script name for a function or variable name.
24395 * Returns NULL when out of memory.
24396 */
24397 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024398autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024399{
24400 char_u *p;
24401 char_u *scriptname;
24402
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024403 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024404 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24405 if (scriptname == NULL)
24406 return FALSE;
24407 STRCPY(scriptname, "autoload/");
24408 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024409 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024410 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024411 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024412 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024413 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024414}
24415
Bram Moolenaar071d4272004-06-13 20:20:40 +000024416#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24417
24418/*
24419 * Function given to ExpandGeneric() to obtain the list of user defined
24420 * function names.
24421 */
24422 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024423get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024424{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024425 static long_u done;
24426 static hashitem_T *hi;
24427 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024428
24429 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024430 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024431 done = 0;
24432 hi = func_hashtab.ht_array;
24433 }
24434 if (done < func_hashtab.ht_used)
24435 {
24436 if (done++ > 0)
24437 ++hi;
24438 while (HASHITEM_EMPTY(hi))
24439 ++hi;
24440 fp = HI2UF(hi);
24441
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024442 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024443 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024444
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024445 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24446 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024447
24448 cat_func_name(IObuff, fp);
24449 if (xp->xp_context != EXPAND_USER_FUNC)
24450 {
24451 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024452 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024453 STRCAT(IObuff, ")");
24454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024455 return IObuff;
24456 }
24457 return NULL;
24458}
24459
24460#endif /* FEAT_CMDL_COMPL */
24461
24462/*
24463 * Copy the function name of "fp" to buffer "buf".
24464 * "buf" must be able to hold the function name plus three bytes.
24465 * Takes care of script-local function names.
24466 */
24467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024468cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024469{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024470 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024471 {
24472 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024473 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024474 }
24475 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024476 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024477}
24478
24479/*
24480 * ":delfunction {name}"
24481 */
24482 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024483ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024484{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024485 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024486 char_u *p;
24487 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024488 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024489
24490 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024491 name = trans_function_name(&p, eap->skip, 0, &fudi);
24492 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024493 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024494 {
24495 if (fudi.fd_dict != NULL && !eap->skip)
24496 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024497 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024499 if (!ends_excmd(*skipwhite(p)))
24500 {
24501 vim_free(name);
24502 EMSG(_(e_trailing));
24503 return;
24504 }
24505 eap->nextcmd = check_nextcmd(p);
24506 if (eap->nextcmd != NULL)
24507 *p = NUL;
24508
24509 if (!eap->skip)
24510 fp = find_func(name);
24511 vim_free(name);
24512
24513 if (!eap->skip)
24514 {
24515 if (fp == NULL)
24516 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024517 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024518 return;
24519 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024520 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024521 {
24522 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24523 return;
24524 }
24525
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024526 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024527 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024528 /* Delete the dict item that refers to the function, it will
24529 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024530 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024531 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024532 else
24533 func_free(fp);
24534 }
24535}
24536
24537/*
24538 * Free a function and remove it from the list of functions.
24539 */
24540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024541func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024542{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024543 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024544
24545 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024546 ga_clear_strings(&(fp->uf_args));
24547 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024548#ifdef FEAT_PROFILE
24549 vim_free(fp->uf_tml_count);
24550 vim_free(fp->uf_tml_total);
24551 vim_free(fp->uf_tml_self);
24552#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024553
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024554 /* remove the function from the function hashtable */
24555 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24556 if (HASHITEM_EMPTY(hi))
24557 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024558 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024559 hash_remove(&func_hashtab, hi);
24560
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024561 vim_free(fp);
24562}
24563
24564/*
24565 * Unreference a Function: decrement the reference count and free it when it
24566 * becomes zero. Only for numbered functions.
24567 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024568 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024569func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024570{
24571 ufunc_T *fp;
24572
24573 if (name != NULL && isdigit(*name))
24574 {
24575 fp = find_func(name);
24576 if (fp == NULL)
24577 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024578 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024579 {
24580 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024581 * when "uf_calls" becomes zero. */
24582 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024583 func_free(fp);
24584 }
24585 }
24586}
24587
24588/*
24589 * Count a reference to a Function.
24590 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024591 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024592func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024593{
24594 ufunc_T *fp;
24595
24596 if (name != NULL && isdigit(*name))
24597 {
24598 fp = find_func(name);
24599 if (fp == NULL)
24600 EMSG2(_(e_intern2), "func_ref()");
24601 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024602 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024603 }
24604}
24605
24606/*
24607 * Call a user function.
24608 */
24609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024610call_user_func(
24611 ufunc_T *fp, /* pointer to function */
24612 int argcount, /* nr of args */
24613 typval_T *argvars, /* arguments */
24614 typval_T *rettv, /* return value */
24615 linenr_T firstline, /* first line of range */
24616 linenr_T lastline, /* last line of range */
24617 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024618{
Bram Moolenaar33570922005-01-25 22:26:29 +000024619 char_u *save_sourcing_name;
24620 linenr_T save_sourcing_lnum;
24621 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024622 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024623 int save_did_emsg;
24624 static int depth = 0;
24625 dictitem_T *v;
24626 int fixvar_idx = 0; /* index in fixvar[] */
24627 int i;
24628 int ai;
24629 char_u numbuf[NUMBUFLEN];
24630 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024631 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024632#ifdef FEAT_PROFILE
24633 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024634 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024636
24637 /* If depth of calling is getting too high, don't execute the function */
24638 if (depth >= p_mfd)
24639 {
24640 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024641 rettv->v_type = VAR_NUMBER;
24642 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024643 return;
24644 }
24645 ++depth;
24646
24647 line_breakcheck(); /* check for CTRL-C hit */
24648
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024649 fc = (funccall_T *)alloc(sizeof(funccall_T));
24650 fc->caller = current_funccal;
24651 current_funccal = fc;
24652 fc->func = fp;
24653 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024654 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024655 fc->linenr = 0;
24656 fc->returned = FALSE;
24657 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024658 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024659 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24660 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024661
Bram Moolenaar33570922005-01-25 22:26:29 +000024662 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024663 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024664 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24665 * each argument variable and saves a lot of time.
24666 */
24667 /*
24668 * Init l: variables.
24669 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024670 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024671 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024672 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024673 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24674 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024675 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024676 name = v->di_key;
24677 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024678 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024679 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024680 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024681 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024682 v->di_tv.vval.v_dict = selfdict;
24683 ++selfdict->dv_refcount;
24684 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024685
Bram Moolenaar33570922005-01-25 22:26:29 +000024686 /*
24687 * Init a: variables.
24688 * Set a:0 to "argcount".
24689 * Set a:000 to a list with room for the "..." arguments.
24690 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024691 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024692 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024693 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024694 /* Use "name" to avoid a warning from some compiler that checks the
24695 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024696 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024697 name = v->di_key;
24698 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024699 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024700 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024701 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024702 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024703 v->di_tv.vval.v_list = &fc->l_varlist;
24704 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24705 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24706 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024707
24708 /*
24709 * Set a:firstline to "firstline" and a:lastline to "lastline".
24710 * Set a:name to named arguments.
24711 * Set a:N to the "..." arguments.
24712 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024713 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024714 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024715 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024716 (varnumber_T)lastline);
24717 for (i = 0; i < argcount; ++i)
24718 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024719 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024720 if (ai < 0)
24721 /* named argument a:name */
24722 name = FUNCARG(fp, i);
24723 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024724 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024725 /* "..." argument a:1, a:2, etc. */
24726 sprintf((char *)numbuf, "%d", ai + 1);
24727 name = numbuf;
24728 }
24729 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24730 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024731 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024732 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24733 }
24734 else
24735 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024736 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24737 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024738 if (v == NULL)
24739 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024740 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024741 }
24742 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024743 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024744
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024745 /* Note: the values are copied directly to avoid alloc/free.
24746 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024747 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024748 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024749
24750 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24751 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024752 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24753 fc->l_listitems[ai].li_tv = argvars[i];
24754 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024755 }
24756 }
24757
Bram Moolenaar071d4272004-06-13 20:20:40 +000024758 /* Don't redraw while executing the function. */
24759 ++RedrawingDisabled;
24760 save_sourcing_name = sourcing_name;
24761 save_sourcing_lnum = sourcing_lnum;
24762 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024763 /* need space for function name + ("function " + 3) or "[number]" */
24764 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24765 + STRLEN(fp->uf_name) + 20;
24766 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024767 if (sourcing_name != NULL)
24768 {
24769 if (save_sourcing_name != NULL
24770 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024771 sprintf((char *)sourcing_name, "%s[%d]..",
24772 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024773 else
24774 STRCPY(sourcing_name, "function ");
24775 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24776
24777 if (p_verbose >= 12)
24778 {
24779 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024780 verbose_enter_scroll();
24781
Bram Moolenaar555b2802005-05-19 21:08:39 +000024782 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024783 if (p_verbose >= 14)
24784 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024785 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024786 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024787 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024788 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024789
24790 msg_puts((char_u *)"(");
24791 for (i = 0; i < argcount; ++i)
24792 {
24793 if (i > 0)
24794 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024795 if (argvars[i].v_type == VAR_NUMBER)
24796 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024797 else
24798 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024799 /* Do not want errors such as E724 here. */
24800 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024801 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024802 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024803 if (s != NULL)
24804 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024805 if (vim_strsize(s) > MSG_BUF_CLEN)
24806 {
24807 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24808 s = buf;
24809 }
24810 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024811 vim_free(tofree);
24812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024813 }
24814 }
24815 msg_puts((char_u *)")");
24816 }
24817 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024818
24819 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024820 --no_wait_return;
24821 }
24822 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024823#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024824 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024825 {
24826 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24827 func_do_profile(fp);
24828 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024829 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024830 {
24831 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024832 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024833 profile_zero(&fp->uf_tm_children);
24834 }
24835 script_prof_save(&wait_start);
24836 }
24837#endif
24838
Bram Moolenaar071d4272004-06-13 20:20:40 +000024839 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024840 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024841 save_did_emsg = did_emsg;
24842 did_emsg = FALSE;
24843
24844 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024845 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024846 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24847
24848 --RedrawingDisabled;
24849
24850 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024851 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024852 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024853 clear_tv(rettv);
24854 rettv->v_type = VAR_NUMBER;
24855 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024856 }
24857
Bram Moolenaar05159a02005-02-26 23:04:13 +000024858#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024859 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024860 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024861 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024862 profile_end(&call_start);
24863 profile_sub_wait(&wait_start, &call_start);
24864 profile_add(&fp->uf_tm_total, &call_start);
24865 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024866 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024867 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024868 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24869 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024870 }
24871 }
24872#endif
24873
Bram Moolenaar071d4272004-06-13 20:20:40 +000024874 /* when being verbose, mention the return value */
24875 if (p_verbose >= 12)
24876 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024877 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024878 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024879
Bram Moolenaar071d4272004-06-13 20:20:40 +000024880 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024881 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024882 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024883 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024884 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024885 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024886 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024887 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024888 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024889 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024890 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024891
Bram Moolenaar555b2802005-05-19 21:08:39 +000024892 /* The value may be very long. Skip the middle part, so that we
24893 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024894 * truncate it at the end. Don't want errors such as E724 here. */
24895 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024896 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024897 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024898 if (s != NULL)
24899 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024900 if (vim_strsize(s) > MSG_BUF_CLEN)
24901 {
24902 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24903 s = buf;
24904 }
24905 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024906 vim_free(tofree);
24907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024908 }
24909 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024910
24911 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024912 --no_wait_return;
24913 }
24914
24915 vim_free(sourcing_name);
24916 sourcing_name = save_sourcing_name;
24917 sourcing_lnum = save_sourcing_lnum;
24918 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024919#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024920 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024921 script_prof_restore(&wait_start);
24922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024923
24924 if (p_verbose >= 12 && sourcing_name != NULL)
24925 {
24926 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024927 verbose_enter_scroll();
24928
Bram Moolenaar555b2802005-05-19 21:08:39 +000024929 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024930 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024931
24932 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024933 --no_wait_return;
24934 }
24935
24936 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024937 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024938 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024939
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024940 /* If the a:000 list and the l: and a: dicts are not referenced we can
24941 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024942 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24943 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24944 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24945 {
24946 free_funccal(fc, FALSE);
24947 }
24948 else
24949 {
24950 hashitem_T *hi;
24951 listitem_T *li;
24952 int todo;
24953
24954 /* "fc" is still in use. This can happen when returning "a:000" or
24955 * assigning "l:" to a global variable.
24956 * Link "fc" in the list for garbage collection later. */
24957 fc->caller = previous_funccal;
24958 previous_funccal = fc;
24959
24960 /* Make a copy of the a: variables, since we didn't do that above. */
24961 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24962 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24963 {
24964 if (!HASHITEM_EMPTY(hi))
24965 {
24966 --todo;
24967 v = HI2DI(hi);
24968 copy_tv(&v->di_tv, &v->di_tv);
24969 }
24970 }
24971
24972 /* Make a copy of the a:000 items, since we didn't do that above. */
24973 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24974 copy_tv(&li->li_tv, &li->li_tv);
24975 }
24976}
24977
24978/*
24979 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024980 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024981 */
24982 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024983can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024984{
24985 return (fc->l_varlist.lv_copyID != copyID
24986 && fc->l_vars.dv_copyID != copyID
24987 && fc->l_avars.dv_copyID != copyID);
24988}
24989
24990/*
24991 * Free "fc" and what it contains.
24992 */
24993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024994free_funccal(
24995 funccall_T *fc,
24996 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024997{
24998 listitem_T *li;
24999
25000 /* The a: variables typevals may not have been allocated, only free the
25001 * allocated variables. */
25002 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25003
25004 /* free all l: variables */
25005 vars_clear(&fc->l_vars.dv_hashtab);
25006
25007 /* Free the a:000 variables if they were allocated. */
25008 if (free_val)
25009 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25010 clear_tv(&li->li_tv);
25011
25012 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025013}
25014
25015/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025016 * Add a number variable "name" to dict "dp" with value "nr".
25017 */
25018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025019add_nr_var(
25020 dict_T *dp,
25021 dictitem_T *v,
25022 char *name,
25023 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025024{
25025 STRCPY(v->di_key, name);
25026 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25027 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25028 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025029 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025030 v->di_tv.vval.v_number = nr;
25031}
25032
25033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025034 * ":return [expr]"
25035 */
25036 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025037ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025038{
25039 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025040 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025041 int returning = FALSE;
25042
25043 if (current_funccal == NULL)
25044 {
25045 EMSG(_("E133: :return not inside a function"));
25046 return;
25047 }
25048
25049 if (eap->skip)
25050 ++emsg_skip;
25051
25052 eap->nextcmd = NULL;
25053 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025054 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025055 {
25056 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025057 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025058 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025059 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025060 }
25061 /* It's safer to return also on error. */
25062 else if (!eap->skip)
25063 {
25064 /*
25065 * Return unless the expression evaluation has been cancelled due to an
25066 * aborting error, an interrupt, or an exception.
25067 */
25068 if (!aborting())
25069 returning = do_return(eap, FALSE, TRUE, NULL);
25070 }
25071
25072 /* When skipping or the return gets pending, advance to the next command
25073 * in this line (!returning). Otherwise, ignore the rest of the line.
25074 * Following lines will be ignored by get_func_line(). */
25075 if (returning)
25076 eap->nextcmd = NULL;
25077 else if (eap->nextcmd == NULL) /* no argument */
25078 eap->nextcmd = check_nextcmd(arg);
25079
25080 if (eap->skip)
25081 --emsg_skip;
25082}
25083
25084/*
25085 * Return from a function. Possibly makes the return pending. Also called
25086 * for a pending return at the ":endtry" or after returning from an extra
25087 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025088 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025089 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025090 * FALSE when the return gets pending.
25091 */
25092 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025093do_return(
25094 exarg_T *eap,
25095 int reanimate,
25096 int is_cmd,
25097 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025098{
25099 int idx;
25100 struct condstack *cstack = eap->cstack;
25101
25102 if (reanimate)
25103 /* Undo the return. */
25104 current_funccal->returned = FALSE;
25105
25106 /*
25107 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25108 * not in its finally clause (which then is to be executed next) is found.
25109 * In this case, make the ":return" pending for execution at the ":endtry".
25110 * Otherwise, return normally.
25111 */
25112 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25113 if (idx >= 0)
25114 {
25115 cstack->cs_pending[idx] = CSTP_RETURN;
25116
25117 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025118 /* A pending return again gets pending. "rettv" points to an
25119 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025120 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025121 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025122 else
25123 {
25124 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025125 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025126 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025127 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025128
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025129 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025130 {
25131 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025132 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025133 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025134 else
25135 EMSG(_(e_outofmem));
25136 }
25137 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025138 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025139
25140 if (reanimate)
25141 {
25142 /* The pending return value could be overwritten by a ":return"
25143 * without argument in a finally clause; reset the default
25144 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025145 current_funccal->rettv->v_type = VAR_NUMBER;
25146 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025147 }
25148 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025149 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025150 }
25151 else
25152 {
25153 current_funccal->returned = TRUE;
25154
25155 /* If the return is carried out now, store the return value. For
25156 * a return immediately after reanimation, the value is already
25157 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025158 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025159 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025160 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025161 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025162 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025163 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025164 }
25165 }
25166
25167 return idx < 0;
25168}
25169
25170/*
25171 * Free the variable with a pending return value.
25172 */
25173 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025174discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025175{
Bram Moolenaar33570922005-01-25 22:26:29 +000025176 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025177}
25178
25179/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025180 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025181 * is an allocated string. Used by report_pending() for verbose messages.
25182 */
25183 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025184get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025185{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025186 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025187 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025188 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025189
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025190 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025191 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025192 if (s == NULL)
25193 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025194
25195 STRCPY(IObuff, ":return ");
25196 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25197 if (STRLEN(s) + 8 >= IOSIZE)
25198 STRCPY(IObuff + IOSIZE - 4, "...");
25199 vim_free(tofree);
25200 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025201}
25202
25203/*
25204 * Get next function line.
25205 * Called by do_cmdline() to get the next line.
25206 * Returns allocated string, or NULL for end of function.
25207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025208 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025209get_func_line(
25210 int c UNUSED,
25211 void *cookie,
25212 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025213{
Bram Moolenaar33570922005-01-25 22:26:29 +000025214 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025215 ufunc_T *fp = fcp->func;
25216 char_u *retval;
25217 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025218
25219 /* If breakpoints have been added/deleted need to check for it. */
25220 if (fcp->dbg_tick != debug_tick)
25221 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025222 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025223 sourcing_lnum);
25224 fcp->dbg_tick = debug_tick;
25225 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025226#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025227 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025228 func_line_end(cookie);
25229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025230
Bram Moolenaar05159a02005-02-26 23:04:13 +000025231 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025232 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25233 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025234 retval = NULL;
25235 else
25236 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025237 /* Skip NULL lines (continuation lines). */
25238 while (fcp->linenr < gap->ga_len
25239 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25240 ++fcp->linenr;
25241 if (fcp->linenr >= gap->ga_len)
25242 retval = NULL;
25243 else
25244 {
25245 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25246 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025247#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025248 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025249 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025250#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025252 }
25253
25254 /* Did we encounter a breakpoint? */
25255 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25256 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025257 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025258 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025259 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025260 sourcing_lnum);
25261 fcp->dbg_tick = debug_tick;
25262 }
25263
25264 return retval;
25265}
25266
Bram Moolenaar05159a02005-02-26 23:04:13 +000025267#if defined(FEAT_PROFILE) || defined(PROTO)
25268/*
25269 * Called when starting to read a function line.
25270 * "sourcing_lnum" must be correct!
25271 * When skipping lines it may not actually be executed, but we won't find out
25272 * until later and we need to store the time now.
25273 */
25274 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025275func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025276{
25277 funccall_T *fcp = (funccall_T *)cookie;
25278 ufunc_T *fp = fcp->func;
25279
25280 if (fp->uf_profiling && sourcing_lnum >= 1
25281 && sourcing_lnum <= fp->uf_lines.ga_len)
25282 {
25283 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025284 /* Skip continuation lines. */
25285 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25286 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025287 fp->uf_tml_execed = FALSE;
25288 profile_start(&fp->uf_tml_start);
25289 profile_zero(&fp->uf_tml_children);
25290 profile_get_wait(&fp->uf_tml_wait);
25291 }
25292}
25293
25294/*
25295 * Called when actually executing a function line.
25296 */
25297 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025298func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025299{
25300 funccall_T *fcp = (funccall_T *)cookie;
25301 ufunc_T *fp = fcp->func;
25302
25303 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25304 fp->uf_tml_execed = TRUE;
25305}
25306
25307/*
25308 * Called when done with a function line.
25309 */
25310 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025311func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025312{
25313 funccall_T *fcp = (funccall_T *)cookie;
25314 ufunc_T *fp = fcp->func;
25315
25316 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25317 {
25318 if (fp->uf_tml_execed)
25319 {
25320 ++fp->uf_tml_count[fp->uf_tml_idx];
25321 profile_end(&fp->uf_tml_start);
25322 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025323 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025324 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25325 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025326 }
25327 fp->uf_tml_idx = -1;
25328 }
25329}
25330#endif
25331
Bram Moolenaar071d4272004-06-13 20:20:40 +000025332/*
25333 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025334 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025335 */
25336 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025337func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025338{
Bram Moolenaar33570922005-01-25 22:26:29 +000025339 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025340
25341 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25342 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025343 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025344 || fcp->returned);
25345}
25346
25347/*
25348 * return TRUE if cookie indicates a function which "abort"s on errors.
25349 */
25350 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025351func_has_abort(
25352 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025353{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025354 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025355}
25356
25357#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25358typedef enum
25359{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025360 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25361 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25362 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025363} var_flavour_T;
25364
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025365static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025366
25367 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025368var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025369{
25370 char_u *p = varname;
25371
25372 if (ASCII_ISUPPER(*p))
25373 {
25374 while (*(++p))
25375 if (ASCII_ISLOWER(*p))
25376 return VAR_FLAVOUR_SESSION;
25377 return VAR_FLAVOUR_VIMINFO;
25378 }
25379 else
25380 return VAR_FLAVOUR_DEFAULT;
25381}
25382#endif
25383
25384#if defined(FEAT_VIMINFO) || defined(PROTO)
25385/*
25386 * Restore global vars that start with a capital from the viminfo file
25387 */
25388 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025389read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025390{
25391 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025392 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025393 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025394 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025395
25396 if (!writing && (find_viminfo_parameter('!') != NULL))
25397 {
25398 tab = vim_strchr(virp->vir_line + 1, '\t');
25399 if (tab != NULL)
25400 {
25401 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025402 switch (*tab)
25403 {
25404 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025405#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025406 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025407#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025408 case 'D': type = VAR_DICT; break;
25409 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025410 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025412
25413 tab = vim_strchr(tab, '\t');
25414 if (tab != NULL)
25415 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025416 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025417 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025418 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025419 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025420#ifdef FEAT_FLOAT
25421 else if (type == VAR_FLOAT)
25422 (void)string2float(tab + 1, &tv.vval.v_float);
25423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025424 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025425 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025426 if (type == VAR_DICT || type == VAR_LIST)
25427 {
25428 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25429
25430 if (etv == NULL)
25431 /* Failed to parse back the dict or list, use it as a
25432 * string. */
25433 tv.v_type = VAR_STRING;
25434 else
25435 {
25436 vim_free(tv.vval.v_string);
25437 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025438 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025439 }
25440 }
25441
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025442 /* when in a function use global variables */
25443 save_funccal = current_funccal;
25444 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025445 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025446 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025447
25448 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025449 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025450 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25451 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025452 }
25453 }
25454 }
25455
25456 return viminfo_readline(virp);
25457}
25458
25459/*
25460 * Write global vars that start with a capital to the viminfo file
25461 */
25462 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025463write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025464{
Bram Moolenaar33570922005-01-25 22:26:29 +000025465 hashitem_T *hi;
25466 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025467 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025468 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025469 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025470 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025471 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025472
25473 if (find_viminfo_parameter('!') == NULL)
25474 return;
25475
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025476 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025477
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025478 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025479 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025480 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025481 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025482 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025483 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025484 this_var = HI2DI(hi);
25485 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025486 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025487 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025488 {
25489 case VAR_STRING: s = "STR"; break;
25490 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025491 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025492 case VAR_DICT: s = "DIC"; break;
25493 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025494 case VAR_SPECIAL: s = "XPL"; break;
25495
25496 case VAR_UNKNOWN:
25497 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025498 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025499 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025500 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025501 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025502 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025503 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025504 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025505 if (p != NULL)
25506 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025507 vim_free(tofree);
25508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025509 }
25510 }
25511}
25512#endif
25513
25514#if defined(FEAT_SESSION) || defined(PROTO)
25515 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025516store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025517{
Bram Moolenaar33570922005-01-25 22:26:29 +000025518 hashitem_T *hi;
25519 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025520 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025521 char_u *p, *t;
25522
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025523 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025524 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025525 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025526 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025527 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025528 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025529 this_var = HI2DI(hi);
25530 if ((this_var->di_tv.v_type == VAR_NUMBER
25531 || this_var->di_tv.v_type == VAR_STRING)
25532 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025533 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025534 /* Escape special characters with a backslash. Turn a LF and
25535 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025536 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025537 (char_u *)"\\\"\n\r");
25538 if (p == NULL) /* out of memory */
25539 break;
25540 for (t = p; *t != NUL; ++t)
25541 if (*t == '\n')
25542 *t = 'n';
25543 else if (*t == '\r')
25544 *t = 'r';
25545 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025546 this_var->di_key,
25547 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25548 : ' ',
25549 p,
25550 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25551 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025552 || put_eol(fd) == FAIL)
25553 {
25554 vim_free(p);
25555 return FAIL;
25556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025557 vim_free(p);
25558 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025559#ifdef FEAT_FLOAT
25560 else if (this_var->di_tv.v_type == VAR_FLOAT
25561 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25562 {
25563 float_T f = this_var->di_tv.vval.v_float;
25564 int sign = ' ';
25565
25566 if (f < 0)
25567 {
25568 f = -f;
25569 sign = '-';
25570 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025571 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025572 this_var->di_key, sign, f) < 0)
25573 || put_eol(fd) == FAIL)
25574 return FAIL;
25575 }
25576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025577 }
25578 }
25579 return OK;
25580}
25581#endif
25582
Bram Moolenaar661b1822005-07-28 22:36:45 +000025583/*
25584 * Display script name where an item was last set.
25585 * Should only be invoked when 'verbose' is non-zero.
25586 */
25587 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025588last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025589{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025590 char_u *p;
25591
Bram Moolenaar661b1822005-07-28 22:36:45 +000025592 if (scriptID != 0)
25593 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025594 p = home_replace_save(NULL, get_scriptname(scriptID));
25595 if (p != NULL)
25596 {
25597 verbose_enter();
25598 MSG_PUTS(_("\n\tLast set from "));
25599 MSG_PUTS(p);
25600 vim_free(p);
25601 verbose_leave();
25602 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025603 }
25604}
25605
Bram Moolenaard812df62008-11-09 12:46:09 +000025606/*
25607 * List v:oldfiles in a nice way.
25608 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025609 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025610ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025611{
25612 list_T *l = vimvars[VV_OLDFILES].vv_list;
25613 listitem_T *li;
25614 int nr = 0;
25615
25616 if (l == NULL)
25617 msg((char_u *)_("No old files"));
25618 else
25619 {
25620 msg_start();
25621 msg_scroll = TRUE;
25622 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25623 {
25624 msg_outnum((long)++nr);
25625 MSG_PUTS(": ");
25626 msg_outtrans(get_tv_string(&li->li_tv));
25627 msg_putchar('\n');
25628 out_flush(); /* output one line at a time */
25629 ui_breakcheck();
25630 }
25631 /* Assume "got_int" was set to truncate the listing. */
25632 got_int = FALSE;
25633
25634#ifdef FEAT_BROWSE_CMD
25635 if (cmdmod.browse)
25636 {
25637 quit_more = FALSE;
25638 nr = prompt_for_number(FALSE);
25639 msg_starthere();
25640 if (nr > 0)
25641 {
25642 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25643 (long)nr);
25644
25645 if (p != NULL)
25646 {
25647 p = expand_env_save(p);
25648 eap->arg = p;
25649 eap->cmdidx = CMD_edit;
25650 cmdmod.browse = FALSE;
25651 do_exedit(eap, NULL);
25652 vim_free(p);
25653 }
25654 }
25655 }
25656#endif
25657 }
25658}
25659
Bram Moolenaar53744302015-07-17 17:38:22 +020025660/* reset v:option_new, v:option_old and v:option_type */
25661 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025662reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025663{
25664 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25665 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25666 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25667}
25668
25669
Bram Moolenaar071d4272004-06-13 20:20:40 +000025670#endif /* FEAT_EVAL */
25671
Bram Moolenaar071d4272004-06-13 20:20:40 +000025672
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025673#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025674
25675#ifdef WIN3264
25676/*
25677 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25678 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025679static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25680static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25681static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025682
25683/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025684 * Get the short path (8.3) for the filename in "fnamep".
25685 * Only works for a valid file name.
25686 * When the path gets longer "fnamep" is changed and the allocated buffer
25687 * is put in "bufp".
25688 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25689 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025690 */
25691 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025692get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025693{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025694 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025695 char_u *newbuf;
25696
25697 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025698 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025699 if (l > len - 1)
25700 {
25701 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025702 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025703 newbuf = vim_strnsave(*fnamep, l);
25704 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025705 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025706
25707 vim_free(*bufp);
25708 *fnamep = *bufp = newbuf;
25709
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025710 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025711 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025712 }
25713
25714 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025715 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025716}
25717
25718/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025719 * Get the short path (8.3) for the filename in "fname". The converted
25720 * path is returned in "bufp".
25721 *
25722 * Some of the directories specified in "fname" may not exist. This function
25723 * will shorten the existing directories at the beginning of the path and then
25724 * append the remaining non-existing path.
25725 *
25726 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025727 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025728 * bufp - Pointer to an allocated buffer for the filename.
25729 * fnamelen - Length of the filename pointed to by fname
25730 *
25731 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025732 */
25733 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025734shortpath_for_invalid_fname(
25735 char_u **fname,
25736 char_u **bufp,
25737 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025738{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025739 char_u *short_fname, *save_fname, *pbuf_unused;
25740 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025741 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025742 int old_len, len;
25743 int new_len, sfx_len;
25744 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025745
25746 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025747 old_len = *fnamelen;
25748 save_fname = vim_strnsave(*fname, old_len);
25749 pbuf_unused = NULL;
25750 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025751
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025752 endp = save_fname + old_len - 1; /* Find the end of the copy */
25753 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025754
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025755 /*
25756 * Try shortening the supplied path till it succeeds by removing one
25757 * directory at a time from the tail of the path.
25758 */
25759 len = 0;
25760 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025761 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025762 /* go back one path-separator */
25763 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25764 --endp;
25765 if (endp <= save_fname)
25766 break; /* processed the complete path */
25767
25768 /*
25769 * Replace the path separator with a NUL and try to shorten the
25770 * resulting path.
25771 */
25772 ch = *endp;
25773 *endp = 0;
25774 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025775 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025776 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25777 {
25778 retval = FAIL;
25779 goto theend;
25780 }
25781 *endp = ch; /* preserve the string */
25782
25783 if (len > 0)
25784 break; /* successfully shortened the path */
25785
25786 /* failed to shorten the path. Skip the path separator */
25787 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025788 }
25789
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025790 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025791 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025792 /*
25793 * Succeeded in shortening the path. Now concatenate the shortened
25794 * path with the remaining path at the tail.
25795 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025796
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025797 /* Compute the length of the new path. */
25798 sfx_len = (int)(save_endp - endp) + 1;
25799 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025800
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025801 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025802 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025803 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025804 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025805 /* There is not enough space in the currently allocated string,
25806 * copy it to a buffer big enough. */
25807 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025808 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025809 {
25810 retval = FAIL;
25811 goto theend;
25812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025813 }
25814 else
25815 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025816 /* Transfer short_fname to the main buffer (it's big enough),
25817 * unless get_short_pathname() did its work in-place. */
25818 *fname = *bufp = save_fname;
25819 if (short_fname != save_fname)
25820 vim_strncpy(save_fname, short_fname, len);
25821 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025822 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025823
25824 /* concat the not-shortened part of the path */
25825 vim_strncpy(*fname + len, endp, sfx_len);
25826 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025827 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025828
25829theend:
25830 vim_free(pbuf_unused);
25831 vim_free(save_fname);
25832
25833 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025834}
25835
25836/*
25837 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025838 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025839 */
25840 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025841shortpath_for_partial(
25842 char_u **fnamep,
25843 char_u **bufp,
25844 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025845{
25846 int sepcount, len, tflen;
25847 char_u *p;
25848 char_u *pbuf, *tfname;
25849 int hasTilde;
25850
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025851 /* Count up the path separators from the RHS.. so we know which part
25852 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025853 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025854 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025855 if (vim_ispathsep(*p))
25856 ++sepcount;
25857
25858 /* Need full path first (use expand_env() to remove a "~/") */
25859 hasTilde = (**fnamep == '~');
25860 if (hasTilde)
25861 pbuf = tfname = expand_env_save(*fnamep);
25862 else
25863 pbuf = tfname = FullName_save(*fnamep, FALSE);
25864
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025865 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025866
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025867 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25868 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025869
25870 if (len == 0)
25871 {
25872 /* Don't have a valid filename, so shorten the rest of the
25873 * path if we can. This CAN give us invalid 8.3 filenames, but
25874 * there's not a lot of point in guessing what it might be.
25875 */
25876 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025877 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25878 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025879 }
25880
25881 /* Count the paths backward to find the beginning of the desired string. */
25882 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025883 {
25884#ifdef FEAT_MBYTE
25885 if (has_mbyte)
25886 p -= mb_head_off(tfname, p);
25887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025888 if (vim_ispathsep(*p))
25889 {
25890 if (sepcount == 0 || (hasTilde && sepcount == 1))
25891 break;
25892 else
25893 sepcount --;
25894 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025896 if (hasTilde)
25897 {
25898 --p;
25899 if (p >= tfname)
25900 *p = '~';
25901 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025902 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025903 }
25904 else
25905 ++p;
25906
25907 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25908 vim_free(*bufp);
25909 *fnamelen = (int)STRLEN(p);
25910 *bufp = pbuf;
25911 *fnamep = p;
25912
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025913 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025914}
25915#endif /* WIN3264 */
25916
25917/*
25918 * Adjust a filename, according to a string of modifiers.
25919 * *fnamep must be NUL terminated when called. When returning, the length is
25920 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025921 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025922 * When there is an error, *fnamep is set to NULL.
25923 */
25924 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025925modify_fname(
25926 char_u *src, /* string with modifiers */
25927 int *usedlen, /* characters after src that are used */
25928 char_u **fnamep, /* file name so far */
25929 char_u **bufp, /* buffer for allocated file name or NULL */
25930 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025931{
25932 int valid = 0;
25933 char_u *tail;
25934 char_u *s, *p, *pbuf;
25935 char_u dirname[MAXPATHL];
25936 int c;
25937 int has_fullname = 0;
25938#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025939 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025940 int has_shortname = 0;
25941#endif
25942
25943repeat:
25944 /* ":p" - full path/file_name */
25945 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25946 {
25947 has_fullname = 1;
25948
25949 valid |= VALID_PATH;
25950 *usedlen += 2;
25951
25952 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25953 if ((*fnamep)[0] == '~'
25954#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25955 && ((*fnamep)[1] == '/'
25956# ifdef BACKSLASH_IN_FILENAME
25957 || (*fnamep)[1] == '\\'
25958# endif
25959 || (*fnamep)[1] == NUL)
25960
25961#endif
25962 )
25963 {
25964 *fnamep = expand_env_save(*fnamep);
25965 vim_free(*bufp); /* free any allocated file name */
25966 *bufp = *fnamep;
25967 if (*fnamep == NULL)
25968 return -1;
25969 }
25970
25971 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025972 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025973 {
25974 if (vim_ispathsep(*p)
25975 && p[1] == '.'
25976 && (p[2] == NUL
25977 || vim_ispathsep(p[2])
25978 || (p[2] == '.'
25979 && (p[3] == NUL || vim_ispathsep(p[3])))))
25980 break;
25981 }
25982
25983 /* FullName_save() is slow, don't use it when not needed. */
25984 if (*p != NUL || !vim_isAbsName(*fnamep))
25985 {
25986 *fnamep = FullName_save(*fnamep, *p != NUL);
25987 vim_free(*bufp); /* free any allocated file name */
25988 *bufp = *fnamep;
25989 if (*fnamep == NULL)
25990 return -1;
25991 }
25992
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025993#ifdef WIN3264
25994# if _WIN32_WINNT >= 0x0500
25995 if (vim_strchr(*fnamep, '~') != NULL)
25996 {
25997 /* Expand 8.3 filename to full path. Needed to make sure the same
25998 * file does not have two different names.
25999 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26000 p = alloc(_MAX_PATH + 1);
26001 if (p != NULL)
26002 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026003 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026004 {
26005 vim_free(*bufp);
26006 *bufp = *fnamep = p;
26007 }
26008 else
26009 vim_free(p);
26010 }
26011 }
26012# endif
26013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026014 /* Append a path separator to a directory. */
26015 if (mch_isdir(*fnamep))
26016 {
26017 /* Make room for one or two extra characters. */
26018 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26019 vim_free(*bufp); /* free any allocated file name */
26020 *bufp = *fnamep;
26021 if (*fnamep == NULL)
26022 return -1;
26023 add_pathsep(*fnamep);
26024 }
26025 }
26026
26027 /* ":." - path relative to the current directory */
26028 /* ":~" - path relative to the home directory */
26029 /* ":8" - shortname path - postponed till after */
26030 while (src[*usedlen] == ':'
26031 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26032 {
26033 *usedlen += 2;
26034 if (c == '8')
26035 {
26036#ifdef WIN3264
26037 has_shortname = 1; /* Postpone this. */
26038#endif
26039 continue;
26040 }
26041 pbuf = NULL;
26042 /* Need full path first (use expand_env() to remove a "~/") */
26043 if (!has_fullname)
26044 {
26045 if (c == '.' && **fnamep == '~')
26046 p = pbuf = expand_env_save(*fnamep);
26047 else
26048 p = pbuf = FullName_save(*fnamep, FALSE);
26049 }
26050 else
26051 p = *fnamep;
26052
26053 has_fullname = 0;
26054
26055 if (p != NULL)
26056 {
26057 if (c == '.')
26058 {
26059 mch_dirname(dirname, MAXPATHL);
26060 s = shorten_fname(p, dirname);
26061 if (s != NULL)
26062 {
26063 *fnamep = s;
26064 if (pbuf != NULL)
26065 {
26066 vim_free(*bufp); /* free any allocated file name */
26067 *bufp = pbuf;
26068 pbuf = NULL;
26069 }
26070 }
26071 }
26072 else
26073 {
26074 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26075 /* Only replace it when it starts with '~' */
26076 if (*dirname == '~')
26077 {
26078 s = vim_strsave(dirname);
26079 if (s != NULL)
26080 {
26081 *fnamep = s;
26082 vim_free(*bufp);
26083 *bufp = s;
26084 }
26085 }
26086 }
26087 vim_free(pbuf);
26088 }
26089 }
26090
26091 tail = gettail(*fnamep);
26092 *fnamelen = (int)STRLEN(*fnamep);
26093
26094 /* ":h" - head, remove "/file_name", can be repeated */
26095 /* Don't remove the first "/" or "c:\" */
26096 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26097 {
26098 valid |= VALID_HEAD;
26099 *usedlen += 2;
26100 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026101 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026102 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026103 *fnamelen = (int)(tail - *fnamep);
26104#ifdef VMS
26105 if (*fnamelen > 0)
26106 *fnamelen += 1; /* the path separator is part of the path */
26107#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026108 if (*fnamelen == 0)
26109 {
26110 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26111 p = vim_strsave((char_u *)".");
26112 if (p == NULL)
26113 return -1;
26114 vim_free(*bufp);
26115 *bufp = *fnamep = tail = p;
26116 *fnamelen = 1;
26117 }
26118 else
26119 {
26120 while (tail > s && !after_pathsep(s, tail))
26121 mb_ptr_back(*fnamep, tail);
26122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026123 }
26124
26125 /* ":8" - shortname */
26126 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26127 {
26128 *usedlen += 2;
26129#ifdef WIN3264
26130 has_shortname = 1;
26131#endif
26132 }
26133
26134#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026135 /*
26136 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026137 */
26138 if (has_shortname)
26139 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026140 /* Copy the string if it is shortened by :h and when it wasn't copied
26141 * yet, because we are going to change it in place. Avoids changing
26142 * the buffer name for "%:8". */
26143 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026144 {
26145 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026146 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026147 return -1;
26148 vim_free(*bufp);
26149 *bufp = *fnamep = p;
26150 }
26151
26152 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026153 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026154 if (!has_fullname && !vim_isAbsName(*fnamep))
26155 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026156 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026157 return -1;
26158 }
26159 else
26160 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026161 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026162
Bram Moolenaardc935552011-08-17 15:23:23 +020026163 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026164 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026165 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026166 return -1;
26167
26168 if (l == 0)
26169 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026170 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026171 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026172 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026173 return -1;
26174 }
26175 *fnamelen = l;
26176 }
26177 }
26178#endif /* WIN3264 */
26179
26180 /* ":t" - tail, just the basename */
26181 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26182 {
26183 *usedlen += 2;
26184 *fnamelen -= (int)(tail - *fnamep);
26185 *fnamep = tail;
26186 }
26187
26188 /* ":e" - extension, can be repeated */
26189 /* ":r" - root, without extension, can be repeated */
26190 while (src[*usedlen] == ':'
26191 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26192 {
26193 /* find a '.' in the tail:
26194 * - for second :e: before the current fname
26195 * - otherwise: The last '.'
26196 */
26197 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26198 s = *fnamep - 2;
26199 else
26200 s = *fnamep + *fnamelen - 1;
26201 for ( ; s > tail; --s)
26202 if (s[0] == '.')
26203 break;
26204 if (src[*usedlen + 1] == 'e') /* :e */
26205 {
26206 if (s > tail)
26207 {
26208 *fnamelen += (int)(*fnamep - (s + 1));
26209 *fnamep = s + 1;
26210#ifdef VMS
26211 /* cut version from the extension */
26212 s = *fnamep + *fnamelen - 1;
26213 for ( ; s > *fnamep; --s)
26214 if (s[0] == ';')
26215 break;
26216 if (s > *fnamep)
26217 *fnamelen = s - *fnamep;
26218#endif
26219 }
26220 else if (*fnamep <= tail)
26221 *fnamelen = 0;
26222 }
26223 else /* :r */
26224 {
26225 if (s > tail) /* remove one extension */
26226 *fnamelen = (int)(s - *fnamep);
26227 }
26228 *usedlen += 2;
26229 }
26230
26231 /* ":s?pat?foo?" - substitute */
26232 /* ":gs?pat?foo?" - global substitute */
26233 if (src[*usedlen] == ':'
26234 && (src[*usedlen + 1] == 's'
26235 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26236 {
26237 char_u *str;
26238 char_u *pat;
26239 char_u *sub;
26240 int sep;
26241 char_u *flags;
26242 int didit = FALSE;
26243
26244 flags = (char_u *)"";
26245 s = src + *usedlen + 2;
26246 if (src[*usedlen + 1] == 'g')
26247 {
26248 flags = (char_u *)"g";
26249 ++s;
26250 }
26251
26252 sep = *s++;
26253 if (sep)
26254 {
26255 /* find end of pattern */
26256 p = vim_strchr(s, sep);
26257 if (p != NULL)
26258 {
26259 pat = vim_strnsave(s, (int)(p - s));
26260 if (pat != NULL)
26261 {
26262 s = p + 1;
26263 /* find end of substitution */
26264 p = vim_strchr(s, sep);
26265 if (p != NULL)
26266 {
26267 sub = vim_strnsave(s, (int)(p - s));
26268 str = vim_strnsave(*fnamep, *fnamelen);
26269 if (sub != NULL && str != NULL)
26270 {
26271 *usedlen = (int)(p + 1 - src);
26272 s = do_string_sub(str, pat, sub, flags);
26273 if (s != NULL)
26274 {
26275 *fnamep = s;
26276 *fnamelen = (int)STRLEN(s);
26277 vim_free(*bufp);
26278 *bufp = s;
26279 didit = TRUE;
26280 }
26281 }
26282 vim_free(sub);
26283 vim_free(str);
26284 }
26285 vim_free(pat);
26286 }
26287 }
26288 /* after using ":s", repeat all the modifiers */
26289 if (didit)
26290 goto repeat;
26291 }
26292 }
26293
Bram Moolenaar26df0922014-02-23 23:39:13 +010026294 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26295 {
26296 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26297 if (p == NULL)
26298 return -1;
26299 vim_free(*bufp);
26300 *bufp = *fnamep = p;
26301 *fnamelen = (int)STRLEN(p);
26302 *usedlen += 2;
26303 }
26304
Bram Moolenaar071d4272004-06-13 20:20:40 +000026305 return valid;
26306}
26307
26308/*
26309 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26310 * "flags" can be "g" to do a global substitute.
26311 * Returns an allocated string, NULL for error.
26312 */
26313 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026314do_string_sub(
26315 char_u *str,
26316 char_u *pat,
26317 char_u *sub,
26318 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026319{
26320 int sublen;
26321 regmatch_T regmatch;
26322 int i;
26323 int do_all;
26324 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026325 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026326 garray_T ga;
26327 char_u *ret;
26328 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026329 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026330
26331 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26332 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026333 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026334
26335 ga_init2(&ga, 1, 200);
26336
26337 do_all = (flags[0] == 'g');
26338
26339 regmatch.rm_ic = p_ic;
26340 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26341 if (regmatch.regprog != NULL)
26342 {
26343 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026344 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026345 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26346 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026347 /* Skip empty match except for first match. */
26348 if (regmatch.startp[0] == regmatch.endp[0])
26349 {
26350 if (zero_width == regmatch.startp[0])
26351 {
26352 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026353 i = MB_PTR2LEN(tail);
26354 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26355 (size_t)i);
26356 ga.ga_len += i;
26357 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026358 continue;
26359 }
26360 zero_width = regmatch.startp[0];
26361 }
26362
Bram Moolenaar071d4272004-06-13 20:20:40 +000026363 /*
26364 * Get some space for a temporary buffer to do the substitution
26365 * into. It will contain:
26366 * - The text up to where the match is.
26367 * - The substituted text.
26368 * - The text after the match.
26369 */
26370 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026371 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026372 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26373 {
26374 ga_clear(&ga);
26375 break;
26376 }
26377
26378 /* copy the text up to where the match is */
26379 i = (int)(regmatch.startp[0] - tail);
26380 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26381 /* add the substituted text */
26382 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26383 + ga.ga_len + i, TRUE, TRUE, FALSE);
26384 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026385 tail = regmatch.endp[0];
26386 if (*tail == NUL)
26387 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026388 if (!do_all)
26389 break;
26390 }
26391
26392 if (ga.ga_data != NULL)
26393 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26394
Bram Moolenaar473de612013-06-08 18:19:48 +020026395 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026396 }
26397
26398 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26399 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026400 if (p_cpo == empty_option)
26401 p_cpo = save_cpo;
26402 else
26403 /* Darn, evaluating {sub} expression changed the value. */
26404 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026405
26406 return ret;
26407}
26408
26409#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */