blob: 891cbad738892eac6feddc4eddb3f241b5302bae [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);
Bram Moolenaar65639032016-03-16 21:40:30 +0100870static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd, partial_T **partial);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100871static 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 Moolenaar65639032016-03-16 21:40:30 +01003479 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
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 Moolenaar65639032016-03-16 21:40:30 +01003494 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3495 * contents. For VAR_PARTIAL get its partial, unless we already have one
3496 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003497 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003498 name = deref_func_name(tofree, &len,
3499 partial != NULL ? NULL : &partial, FALSE);
3500
3501 /* When calling fdict.func(), where "func" is a partial, use "fdict"
3502 * instead of the dict in the partial, for backwards compatibility.
3503 * TODO: Do use the arguments in the partial? */
3504 if (fudi.fd_dict != NULL)
3505 partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
Bram Moolenaar532c7802005-01-27 14:44:31 +00003507 /* Skip white space to allow ":call func ()". Not good, but required for
3508 * backward compatibility. */
3509 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003510 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511
3512 if (*startarg != '(')
3513 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003514 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 goto end;
3516 }
3517
3518 /*
3519 * When skipping, evaluate the function once, to find the end of the
3520 * arguments.
3521 * When the function takes a range, this is discovered after the first
3522 * call, and the loop is broken.
3523 */
3524 if (eap->skip)
3525 {
3526 ++emsg_skip;
3527 lnum = eap->line2; /* do it once, also with an invalid range */
3528 }
3529 else
3530 lnum = eap->line1;
3531 for ( ; lnum <= eap->line2; ++lnum)
3532 {
3533 if (!eap->skip && eap->addr_count > 0)
3534 {
3535 curwin->w_cursor.lnum = lnum;
3536 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003537#ifdef FEAT_VIRTUALEDIT
3538 curwin->w_cursor.coladd = 0;
3539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 }
3541 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003542 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003543 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003544 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 {
3546 failed = TRUE;
3547 break;
3548 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003549
3550 /* Handle a function returning a Funcref, Dictionary or List. */
3551 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3552 {
3553 failed = TRUE;
3554 break;
3555 }
3556
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003557 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 if (doesrange || eap->skip)
3559 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003560
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003562 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003563 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003564 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 if (aborting())
3566 break;
3567 }
3568 if (eap->skip)
3569 --emsg_skip;
3570
3571 if (!failed)
3572 {
3573 /* Check for trailing illegal characters and a following command. */
3574 if (!ends_excmd(*arg))
3575 {
3576 emsg_severe = TRUE;
3577 EMSG(_(e_trailing));
3578 }
3579 else
3580 eap->nextcmd = check_nextcmd(arg);
3581 }
3582
3583end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003584 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003585 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586}
3587
3588/*
3589 * ":unlet[!] var1 ... " command.
3590 */
3591 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003592ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003594 ex_unletlock(eap, eap->arg, 0);
3595}
3596
3597/*
3598 * ":lockvar" and ":unlockvar" commands
3599 */
3600 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003601ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003602{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003604 int deep = 2;
3605
3606 if (eap->forceit)
3607 deep = -1;
3608 else if (vim_isdigit(*arg))
3609 {
3610 deep = getdigits(&arg);
3611 arg = skipwhite(arg);
3612 }
3613
3614 ex_unletlock(eap, arg, deep);
3615}
3616
3617/*
3618 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3619 */
3620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003621ex_unletlock(
3622 exarg_T *eap,
3623 char_u *argstart,
3624 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003625{
3626 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003629 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630
3631 do
3632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003633 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003634 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003635 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003636 if (lv.ll_name == NULL)
3637 error = TRUE; /* error but continue parsing */
3638 if (name_end == NULL || (!vim_iswhite(*name_end)
3639 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003641 if (name_end != NULL)
3642 {
3643 emsg_severe = TRUE;
3644 EMSG(_(e_trailing));
3645 }
3646 if (!(eap->skip || error))
3647 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 break;
3649 }
3650
3651 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003652 {
3653 if (eap->cmdidx == CMD_unlet)
3654 {
3655 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3656 error = TRUE;
3657 }
3658 else
3659 {
3660 if (do_lock_var(&lv, name_end, deep,
3661 eap->cmdidx == CMD_lockvar) == FAIL)
3662 error = TRUE;
3663 }
3664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003666 if (!eap->skip)
3667 clear_lval(&lv);
3668
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 arg = skipwhite(name_end);
3670 } while (!ends_excmd(*arg));
3671
3672 eap->nextcmd = check_nextcmd(arg);
3673}
3674
Bram Moolenaar8c711452005-01-14 21:53:12 +00003675 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003676do_unlet_var(
3677 lval_T *lp,
3678 char_u *name_end,
3679 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003680{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003681 int ret = OK;
3682 int cc;
3683
3684 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003685 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003686 cc = *name_end;
3687 *name_end = NUL;
3688
3689 /* Normal name or expanded name. */
3690 if (check_changedtick(lp->ll_name))
3691 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003692 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003693 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003694 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003695 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003696 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003697 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003698 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003699 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003700 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003701 else if (lp->ll_range)
3702 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003703 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003704 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003705 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003706
3707 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3708 {
3709 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003710 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003711 return FAIL;
3712 ll_li = li;
3713 ++ll_n1;
3714 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715
3716 /* Delete a range of List items. */
3717 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3718 {
3719 li = lp->ll_li->li_next;
3720 listitem_remove(lp->ll_list, lp->ll_li);
3721 lp->ll_li = li;
3722 ++lp->ll_n1;
3723 }
3724 }
3725 else
3726 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003727 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003728 /* unlet a List item. */
3729 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003730 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003731 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003732 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003733 }
3734
3735 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003736}
3737
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738/*
3739 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003740 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 */
3742 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003743do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744{
Bram Moolenaar33570922005-01-25 22:26:29 +00003745 hashtab_T *ht;
3746 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003747 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003748 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003749 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750
Bram Moolenaar33570922005-01-25 22:26:29 +00003751 ht = find_var_ht(name, &varname);
3752 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003754 if (ht == &globvarht)
3755 d = &globvardict;
3756 else if (current_funccal != NULL
3757 && ht == &current_funccal->l_vars.dv_hashtab)
3758 d = &current_funccal->l_vars;
3759 else if (ht == &compat_hashtab)
3760 d = &vimvardict;
3761 else
3762 {
3763 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3764 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3765 }
3766 if (d == NULL)
3767 {
3768 EMSG2(_(e_intern2), "do_unlet()");
3769 return FAIL;
3770 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003771 hi = hash_find(ht, varname);
3772 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003773 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003774 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003775 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003776 || var_check_ro(di->di_flags, name, FALSE)
3777 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003778 return FAIL;
3779
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003780 delete_var(ht, hi);
3781 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003784 if (forceit)
3785 return OK;
3786 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 return FAIL;
3788}
3789
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003790/*
3791 * Lock or unlock variable indicated by "lp".
3792 * "deep" is the levels to go (-1 for unlimited);
3793 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3794 */
3795 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003796do_lock_var(
3797 lval_T *lp,
3798 char_u *name_end,
3799 int deep,
3800 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003801{
3802 int ret = OK;
3803 int cc;
3804 dictitem_T *di;
3805
3806 if (deep == 0) /* nothing to do */
3807 return OK;
3808
3809 if (lp->ll_tv == NULL)
3810 {
3811 cc = *name_end;
3812 *name_end = NUL;
3813
3814 /* Normal name or expanded name. */
3815 if (check_changedtick(lp->ll_name))
3816 ret = FAIL;
3817 else
3818 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003819 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003820 if (di == NULL)
3821 ret = FAIL;
3822 else
3823 {
3824 if (lock)
3825 di->di_flags |= DI_FLAGS_LOCK;
3826 else
3827 di->di_flags &= ~DI_FLAGS_LOCK;
3828 item_lock(&di->di_tv, deep, lock);
3829 }
3830 }
3831 *name_end = cc;
3832 }
3833 else if (lp->ll_range)
3834 {
3835 listitem_T *li = lp->ll_li;
3836
3837 /* (un)lock a range of List items. */
3838 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3839 {
3840 item_lock(&li->li_tv, deep, lock);
3841 li = li->li_next;
3842 ++lp->ll_n1;
3843 }
3844 }
3845 else if (lp->ll_list != NULL)
3846 /* (un)lock a List item. */
3847 item_lock(&lp->ll_li->li_tv, deep, lock);
3848 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003849 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003850 item_lock(&lp->ll_di->di_tv, deep, lock);
3851
3852 return ret;
3853}
3854
3855/*
3856 * Lock or unlock an item. "deep" is nr of levels to go.
3857 */
3858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003859item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003860{
3861 static int recurse = 0;
3862 list_T *l;
3863 listitem_T *li;
3864 dict_T *d;
3865 hashitem_T *hi;
3866 int todo;
3867
3868 if (recurse >= DICT_MAXNEST)
3869 {
3870 EMSG(_("E743: variable nested too deep for (un)lock"));
3871 return;
3872 }
3873 if (deep == 0)
3874 return;
3875 ++recurse;
3876
3877 /* lock/unlock the item itself */
3878 if (lock)
3879 tv->v_lock |= VAR_LOCKED;
3880 else
3881 tv->v_lock &= ~VAR_LOCKED;
3882
3883 switch (tv->v_type)
3884 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003885 case VAR_UNKNOWN:
3886 case VAR_NUMBER:
3887 case VAR_STRING:
3888 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003889 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003890 case VAR_FLOAT:
3891 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003892 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003893 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003894 break;
3895
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003896 case VAR_LIST:
3897 if ((l = tv->vval.v_list) != NULL)
3898 {
3899 if (lock)
3900 l->lv_lock |= VAR_LOCKED;
3901 else
3902 l->lv_lock &= ~VAR_LOCKED;
3903 if (deep < 0 || deep > 1)
3904 /* recursive: lock/unlock the items the List contains */
3905 for (li = l->lv_first; li != NULL; li = li->li_next)
3906 item_lock(&li->li_tv, deep - 1, lock);
3907 }
3908 break;
3909 case VAR_DICT:
3910 if ((d = tv->vval.v_dict) != NULL)
3911 {
3912 if (lock)
3913 d->dv_lock |= VAR_LOCKED;
3914 else
3915 d->dv_lock &= ~VAR_LOCKED;
3916 if (deep < 0 || deep > 1)
3917 {
3918 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003919 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003920 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3921 {
3922 if (!HASHITEM_EMPTY(hi))
3923 {
3924 --todo;
3925 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3926 }
3927 }
3928 }
3929 }
3930 }
3931 --recurse;
3932}
3933
Bram Moolenaara40058a2005-07-11 22:42:07 +00003934/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003935 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3936 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003937 */
3938 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003939tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003940{
3941 return (tv->v_lock & VAR_LOCKED)
3942 || (tv->v_type == VAR_LIST
3943 && tv->vval.v_list != NULL
3944 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3945 || (tv->v_type == VAR_DICT
3946 && tv->vval.v_dict != NULL
3947 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3948}
3949
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3951/*
3952 * Delete all "menutrans_" variables.
3953 */
3954 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003955del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956{
Bram Moolenaar33570922005-01-25 22:26:29 +00003957 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003958 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959
Bram Moolenaar33570922005-01-25 22:26:29 +00003960 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003961 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003963 {
3964 if (!HASHITEM_EMPTY(hi))
3965 {
3966 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003967 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3968 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003969 }
3970 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003971 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972}
3973#endif
3974
3975#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3976
3977/*
3978 * Local string buffer for the next two functions to store a variable name
3979 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3980 * get_user_var_name().
3981 */
3982
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003983static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984
3985static char_u *varnamebuf = NULL;
3986static int varnamebuflen = 0;
3987
3988/*
3989 * Function to concatenate a prefix and a variable name.
3990 */
3991 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003992cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993{
3994 int len;
3995
3996 len = (int)STRLEN(name) + 3;
3997 if (len > varnamebuflen)
3998 {
3999 vim_free(varnamebuf);
4000 len += 10; /* some additional space */
4001 varnamebuf = alloc(len);
4002 if (varnamebuf == NULL)
4003 {
4004 varnamebuflen = 0;
4005 return NULL;
4006 }
4007 varnamebuflen = len;
4008 }
4009 *varnamebuf = prefix;
4010 varnamebuf[1] = ':';
4011 STRCPY(varnamebuf + 2, name);
4012 return varnamebuf;
4013}
4014
4015/*
4016 * Function given to ExpandGeneric() to obtain the list of user defined
4017 * (global/buffer/window/built-in) variable names.
4018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004020get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004022 static long_u gdone;
4023 static long_u bdone;
4024 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004025#ifdef FEAT_WINDOWS
4026 static long_u tdone;
4027#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004028 static int vidx;
4029 static hashitem_T *hi;
4030 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031
4032 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004033 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004034 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004035#ifdef FEAT_WINDOWS
4036 tdone = 0;
4037#endif
4038 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004039
4040 /* Global variables */
4041 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004043 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004044 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004045 else
4046 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004047 while (HASHITEM_EMPTY(hi))
4048 ++hi;
4049 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4050 return cat_prefix_varname('g', hi->hi_key);
4051 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004053
4054 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004055 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004056 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004058 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004059 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004060 else
4061 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004062 while (HASHITEM_EMPTY(hi))
4063 ++hi;
4064 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004066 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004068 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 return (char_u *)"b:changedtick";
4070 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004071
4072 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004073 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004074 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004076 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004077 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004078 else
4079 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004080 while (HASHITEM_EMPTY(hi))
4081 ++hi;
4082 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004084
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004085#ifdef FEAT_WINDOWS
4086 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004087 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004088 if (tdone < ht->ht_used)
4089 {
4090 if (tdone++ == 0)
4091 hi = ht->ht_array;
4092 else
4093 ++hi;
4094 while (HASHITEM_EMPTY(hi))
4095 ++hi;
4096 return cat_prefix_varname('t', hi->hi_key);
4097 }
4098#endif
4099
Bram Moolenaar33570922005-01-25 22:26:29 +00004100 /* v: variables */
4101 if (vidx < VV_LEN)
4102 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103
4104 vim_free(varnamebuf);
4105 varnamebuf = NULL;
4106 varnamebuflen = 0;
4107 return NULL;
4108}
4109
4110#endif /* FEAT_CMDL_COMPL */
4111
4112/*
4113 * types for expressions.
4114 */
4115typedef enum
4116{
4117 TYPE_UNKNOWN = 0
4118 , TYPE_EQUAL /* == */
4119 , TYPE_NEQUAL /* != */
4120 , TYPE_GREATER /* > */
4121 , TYPE_GEQUAL /* >= */
4122 , TYPE_SMALLER /* < */
4123 , TYPE_SEQUAL /* <= */
4124 , TYPE_MATCH /* =~ */
4125 , TYPE_NOMATCH /* !~ */
4126} exptype_T;
4127
4128/*
4129 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004130 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4132 */
4133
4134/*
4135 * Handle zero level expression.
4136 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004138 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 * Return OK or FAIL.
4140 */
4141 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004142eval0(
4143 char_u *arg,
4144 typval_T *rettv,
4145 char_u **nextcmd,
4146 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147{
4148 int ret;
4149 char_u *p;
4150
4151 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004152 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 if (ret == FAIL || !ends_excmd(*p))
4154 {
4155 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004156 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 /*
4158 * Report the invalid expression unless the expression evaluation has
4159 * been cancelled due to an aborting error, an interrupt, or an
4160 * exception.
4161 */
4162 if (!aborting())
4163 EMSG2(_(e_invexpr2), arg);
4164 ret = FAIL;
4165 }
4166 if (nextcmd != NULL)
4167 *nextcmd = check_nextcmd(p);
4168
4169 return ret;
4170}
4171
4172/*
4173 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004174 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 *
4176 * "arg" must point to the first non-white of the expression.
4177 * "arg" is advanced to the next non-white after the recognized expression.
4178 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004179 * Note: "rettv.v_lock" is not set.
4180 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 * Return OK or FAIL.
4182 */
4183 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004184eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185{
4186 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004187 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188
4189 /*
4190 * Get the first variable.
4191 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004192 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 return FAIL;
4194
4195 if ((*arg)[0] == '?')
4196 {
4197 result = FALSE;
4198 if (evaluate)
4199 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004200 int error = FALSE;
4201
4202 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004204 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004205 if (error)
4206 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 }
4208
4209 /*
4210 * Get the second variable.
4211 */
4212 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 return FAIL;
4215
4216 /*
4217 * Check for the ":".
4218 */
4219 if ((*arg)[0] != ':')
4220 {
4221 EMSG(_("E109: Missing ':' after '?'"));
4222 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004223 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 return FAIL;
4225 }
4226
4227 /*
4228 * Get the third variable.
4229 */
4230 *arg = skipwhite(*arg + 1);
4231 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4232 {
4233 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004234 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 return FAIL;
4236 }
4237 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004238 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 }
4240
4241 return OK;
4242}
4243
4244/*
4245 * Handle first level expression:
4246 * expr2 || expr2 || expr2 logical OR
4247 *
4248 * "arg" must point to the first non-white of the expression.
4249 * "arg" is advanced to the next non-white after the recognized expression.
4250 *
4251 * Return OK or FAIL.
4252 */
4253 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004254eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255{
Bram Moolenaar33570922005-01-25 22:26:29 +00004256 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 long result;
4258 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260
4261 /*
4262 * Get the first variable.
4263 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004264 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 return FAIL;
4266
4267 /*
4268 * Repeat until there is no following "||".
4269 */
4270 first = TRUE;
4271 result = FALSE;
4272 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4273 {
4274 if (evaluate && first)
4275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004276 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004279 if (error)
4280 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 first = FALSE;
4282 }
4283
4284 /*
4285 * Get the second variable.
4286 */
4287 *arg = skipwhite(*arg + 2);
4288 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4289 return FAIL;
4290
4291 /*
4292 * Compute the result.
4293 */
4294 if (evaluate && !result)
4295 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004296 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004298 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004299 if (error)
4300 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302 if (evaluate)
4303 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004304 rettv->v_type = VAR_NUMBER;
4305 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 }
4307 }
4308
4309 return OK;
4310}
4311
4312/*
4313 * Handle second level expression:
4314 * expr3 && expr3 && expr3 logical AND
4315 *
4316 * "arg" must point to the first non-white of the expression.
4317 * "arg" is advanced to the next non-white after the recognized expression.
4318 *
4319 * Return OK or FAIL.
4320 */
4321 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004322eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323{
Bram Moolenaar33570922005-01-25 22:26:29 +00004324 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 long result;
4326 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004327 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328
4329 /*
4330 * Get the first variable.
4331 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004332 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 return FAIL;
4334
4335 /*
4336 * Repeat until there is no following "&&".
4337 */
4338 first = TRUE;
4339 result = TRUE;
4340 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4341 {
4342 if (evaluate && first)
4343 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004344 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004346 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004347 if (error)
4348 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 first = FALSE;
4350 }
4351
4352 /*
4353 * Get the second variable.
4354 */
4355 *arg = skipwhite(*arg + 2);
4356 if (eval4(arg, &var2, evaluate && result) == FAIL)
4357 return FAIL;
4358
4359 /*
4360 * Compute the result.
4361 */
4362 if (evaluate && result)
4363 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004364 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004366 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004367 if (error)
4368 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
4370 if (evaluate)
4371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004372 rettv->v_type = VAR_NUMBER;
4373 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 }
4375 }
4376
4377 return OK;
4378}
4379
4380/*
4381 * Handle third level expression:
4382 * var1 == var2
4383 * var1 =~ var2
4384 * var1 != var2
4385 * var1 !~ var2
4386 * var1 > var2
4387 * var1 >= var2
4388 * var1 < var2
4389 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004390 * var1 is var2
4391 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 *
4393 * "arg" must point to the first non-white of the expression.
4394 * "arg" is advanced to the next non-white after the recognized expression.
4395 *
4396 * Return OK or FAIL.
4397 */
4398 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004399eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400{
Bram Moolenaar33570922005-01-25 22:26:29 +00004401 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 char_u *p;
4403 int i;
4404 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004405 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 int len = 2;
4407 long n1, n2;
4408 char_u *s1, *s2;
4409 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4410 regmatch_T regmatch;
4411 int ic;
4412 char_u *save_cpo;
4413
4414 /*
4415 * Get the first variable.
4416 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004417 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 return FAIL;
4419
4420 p = *arg;
4421 switch (p[0])
4422 {
4423 case '=': if (p[1] == '=')
4424 type = TYPE_EQUAL;
4425 else if (p[1] == '~')
4426 type = TYPE_MATCH;
4427 break;
4428 case '!': if (p[1] == '=')
4429 type = TYPE_NEQUAL;
4430 else if (p[1] == '~')
4431 type = TYPE_NOMATCH;
4432 break;
4433 case '>': if (p[1] != '=')
4434 {
4435 type = TYPE_GREATER;
4436 len = 1;
4437 }
4438 else
4439 type = TYPE_GEQUAL;
4440 break;
4441 case '<': if (p[1] != '=')
4442 {
4443 type = TYPE_SMALLER;
4444 len = 1;
4445 }
4446 else
4447 type = TYPE_SEQUAL;
4448 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004449 case 'i': if (p[1] == 's')
4450 {
4451 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4452 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004453 i = p[len];
4454 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004455 {
4456 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4457 type_is = TRUE;
4458 }
4459 }
4460 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 }
4462
4463 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004464 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 */
4466 if (type != TYPE_UNKNOWN)
4467 {
4468 /* extra question mark appended: ignore case */
4469 if (p[len] == '?')
4470 {
4471 ic = TRUE;
4472 ++len;
4473 }
4474 /* extra '#' appended: match case */
4475 else if (p[len] == '#')
4476 {
4477 ic = FALSE;
4478 ++len;
4479 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004480 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 else
4482 ic = p_ic;
4483
4484 /*
4485 * Get the second variable.
4486 */
4487 *arg = skipwhite(p + len);
4488 if (eval5(arg, &var2, evaluate) == FAIL)
4489 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004490 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 return FAIL;
4492 }
4493
4494 if (evaluate)
4495 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004496 if (type_is && rettv->v_type != var2.v_type)
4497 {
4498 /* For "is" a different type always means FALSE, for "notis"
4499 * it means TRUE. */
4500 n1 = (type == TYPE_NEQUAL);
4501 }
4502 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4503 {
4504 if (type_is)
4505 {
4506 n1 = (rettv->v_type == var2.v_type
4507 && rettv->vval.v_list == var2.vval.v_list);
4508 if (type == TYPE_NEQUAL)
4509 n1 = !n1;
4510 }
4511 else if (rettv->v_type != var2.v_type
4512 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4513 {
4514 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004515 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004516 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004517 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004518 clear_tv(rettv);
4519 clear_tv(&var2);
4520 return FAIL;
4521 }
4522 else
4523 {
4524 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004525 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4526 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004527 if (type == TYPE_NEQUAL)
4528 n1 = !n1;
4529 }
4530 }
4531
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004532 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4533 {
4534 if (type_is)
4535 {
4536 n1 = (rettv->v_type == var2.v_type
4537 && rettv->vval.v_dict == var2.vval.v_dict);
4538 if (type == TYPE_NEQUAL)
4539 n1 = !n1;
4540 }
4541 else if (rettv->v_type != var2.v_type
4542 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4543 {
4544 if (rettv->v_type != var2.v_type)
4545 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4546 else
4547 EMSG(_("E736: Invalid operation for Dictionary"));
4548 clear_tv(rettv);
4549 clear_tv(&var2);
4550 return FAIL;
4551 }
4552 else
4553 {
4554 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004555 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4556 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004557 if (type == TYPE_NEQUAL)
4558 n1 = !n1;
4559 }
4560 }
4561
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004562 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4563 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004564 {
4565 if (rettv->v_type != var2.v_type
4566 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4567 {
4568 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004569 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004570 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004571 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004572 clear_tv(rettv);
4573 clear_tv(&var2);
4574 return FAIL;
4575 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004576 else if (rettv->v_type == VAR_PARTIAL)
4577 {
4578 /* Partials are only equal when identical. */
4579 n1 = rettv->vval.v_partial != NULL
4580 && rettv->vval.v_partial == var2.vval.v_partial;
4581 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004582 else
4583 {
4584 /* Compare two Funcrefs for being equal or unequal. */
4585 if (rettv->vval.v_string == NULL
4586 || var2.vval.v_string == NULL)
4587 n1 = FALSE;
4588 else
4589 n1 = STRCMP(rettv->vval.v_string,
4590 var2.vval.v_string) == 0;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004591 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004592 if (type == TYPE_NEQUAL)
4593 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004594 }
4595
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004596#ifdef FEAT_FLOAT
4597 /*
4598 * If one of the two variables is a float, compare as a float.
4599 * When using "=~" or "!~", always compare as string.
4600 */
4601 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4602 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4603 {
4604 float_T f1, f2;
4605
4606 if (rettv->v_type == VAR_FLOAT)
4607 f1 = rettv->vval.v_float;
4608 else
4609 f1 = get_tv_number(rettv);
4610 if (var2.v_type == VAR_FLOAT)
4611 f2 = var2.vval.v_float;
4612 else
4613 f2 = get_tv_number(&var2);
4614 n1 = FALSE;
4615 switch (type)
4616 {
4617 case TYPE_EQUAL: n1 = (f1 == f2); break;
4618 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4619 case TYPE_GREATER: n1 = (f1 > f2); break;
4620 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4621 case TYPE_SMALLER: n1 = (f1 < f2); break;
4622 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4623 case TYPE_UNKNOWN:
4624 case TYPE_MATCH:
4625 case TYPE_NOMATCH: break; /* avoid gcc warning */
4626 }
4627 }
4628#endif
4629
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 /*
4631 * If one of the two variables is a number, compare as a number.
4632 * When using "=~" or "!~", always compare as string.
4633 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004634 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4636 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004637 n1 = get_tv_number(rettv);
4638 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 switch (type)
4640 {
4641 case TYPE_EQUAL: n1 = (n1 == n2); break;
4642 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4643 case TYPE_GREATER: n1 = (n1 > n2); break;
4644 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4645 case TYPE_SMALLER: n1 = (n1 < n2); break;
4646 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4647 case TYPE_UNKNOWN:
4648 case TYPE_MATCH:
4649 case TYPE_NOMATCH: break; /* avoid gcc warning */
4650 }
4651 }
4652 else
4653 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004654 s1 = get_tv_string_buf(rettv, buf1);
4655 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4657 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4658 else
4659 i = 0;
4660 n1 = FALSE;
4661 switch (type)
4662 {
4663 case TYPE_EQUAL: n1 = (i == 0); break;
4664 case TYPE_NEQUAL: n1 = (i != 0); break;
4665 case TYPE_GREATER: n1 = (i > 0); break;
4666 case TYPE_GEQUAL: n1 = (i >= 0); break;
4667 case TYPE_SMALLER: n1 = (i < 0); break;
4668 case TYPE_SEQUAL: n1 = (i <= 0); break;
4669
4670 case TYPE_MATCH:
4671 case TYPE_NOMATCH:
4672 /* avoid 'l' flag in 'cpoptions' */
4673 save_cpo = p_cpo;
4674 p_cpo = (char_u *)"";
4675 regmatch.regprog = vim_regcomp(s2,
4676 RE_MAGIC + RE_STRING);
4677 regmatch.rm_ic = ic;
4678 if (regmatch.regprog != NULL)
4679 {
4680 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004681 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 if (type == TYPE_NOMATCH)
4683 n1 = !n1;
4684 }
4685 p_cpo = save_cpo;
4686 break;
4687
4688 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4689 }
4690 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004691 clear_tv(rettv);
4692 clear_tv(&var2);
4693 rettv->v_type = VAR_NUMBER;
4694 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 }
4696 }
4697
4698 return OK;
4699}
4700
4701/*
4702 * Handle fourth level expression:
4703 * + number addition
4704 * - number subtraction
4705 * . string concatenation
4706 *
4707 * "arg" must point to the first non-white of the expression.
4708 * "arg" is advanced to the next non-white after the recognized expression.
4709 *
4710 * Return OK or FAIL.
4711 */
4712 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004713eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714{
Bram Moolenaar33570922005-01-25 22:26:29 +00004715 typval_T var2;
4716 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 int op;
4718 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004719#ifdef FEAT_FLOAT
4720 float_T f1 = 0, f2 = 0;
4721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 char_u *s1, *s2;
4723 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4724 char_u *p;
4725
4726 /*
4727 * Get the first variable.
4728 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004729 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 return FAIL;
4731
4732 /*
4733 * Repeat computing, until no '+', '-' or '.' is following.
4734 */
4735 for (;;)
4736 {
4737 op = **arg;
4738 if (op != '+' && op != '-' && op != '.')
4739 break;
4740
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004741 if ((op != '+' || rettv->v_type != VAR_LIST)
4742#ifdef FEAT_FLOAT
4743 && (op == '.' || rettv->v_type != VAR_FLOAT)
4744#endif
4745 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004746 {
4747 /* For "list + ...", an illegal use of the first operand as
4748 * a number cannot be determined before evaluating the 2nd
4749 * operand: if this is also a list, all is ok.
4750 * For "something . ...", "something - ..." or "non-list + ...",
4751 * we know that the first operand needs to be a string or number
4752 * without evaluating the 2nd operand. So check before to avoid
4753 * side effects after an error. */
4754 if (evaluate && get_tv_string_chk(rettv) == NULL)
4755 {
4756 clear_tv(rettv);
4757 return FAIL;
4758 }
4759 }
4760
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 /*
4762 * Get the second variable.
4763 */
4764 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004765 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004767 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 return FAIL;
4769 }
4770
4771 if (evaluate)
4772 {
4773 /*
4774 * Compute the result.
4775 */
4776 if (op == '.')
4777 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004778 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4779 s2 = get_tv_string_buf_chk(&var2, buf2);
4780 if (s2 == NULL) /* type error ? */
4781 {
4782 clear_tv(rettv);
4783 clear_tv(&var2);
4784 return FAIL;
4785 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004786 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004787 clear_tv(rettv);
4788 rettv->v_type = VAR_STRING;
4789 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004791 else if (op == '+' && rettv->v_type == VAR_LIST
4792 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004793 {
4794 /* concatenate Lists */
4795 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4796 &var3) == FAIL)
4797 {
4798 clear_tv(rettv);
4799 clear_tv(&var2);
4800 return FAIL;
4801 }
4802 clear_tv(rettv);
4803 *rettv = var3;
4804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 else
4806 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004807 int error = FALSE;
4808
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004809#ifdef FEAT_FLOAT
4810 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004811 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004812 f1 = rettv->vval.v_float;
4813 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004814 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004815 else
4816#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004817 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004818 n1 = get_tv_number_chk(rettv, &error);
4819 if (error)
4820 {
4821 /* This can only happen for "list + non-list". For
4822 * "non-list + ..." or "something - ...", we returned
4823 * before evaluating the 2nd operand. */
4824 clear_tv(rettv);
4825 return FAIL;
4826 }
4827#ifdef FEAT_FLOAT
4828 if (var2.v_type == VAR_FLOAT)
4829 f1 = n1;
4830#endif
4831 }
4832#ifdef FEAT_FLOAT
4833 if (var2.v_type == VAR_FLOAT)
4834 {
4835 f2 = var2.vval.v_float;
4836 n2 = 0;
4837 }
4838 else
4839#endif
4840 {
4841 n2 = get_tv_number_chk(&var2, &error);
4842 if (error)
4843 {
4844 clear_tv(rettv);
4845 clear_tv(&var2);
4846 return FAIL;
4847 }
4848#ifdef FEAT_FLOAT
4849 if (rettv->v_type == VAR_FLOAT)
4850 f2 = n2;
4851#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004852 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004853 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004854
4855#ifdef FEAT_FLOAT
4856 /* If there is a float on either side the result is a float. */
4857 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4858 {
4859 if (op == '+')
4860 f1 = f1 + f2;
4861 else
4862 f1 = f1 - f2;
4863 rettv->v_type = VAR_FLOAT;
4864 rettv->vval.v_float = f1;
4865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004867#endif
4868 {
4869 if (op == '+')
4870 n1 = n1 + n2;
4871 else
4872 n1 = n1 - n2;
4873 rettv->v_type = VAR_NUMBER;
4874 rettv->vval.v_number = n1;
4875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004877 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
4879 }
4880 return OK;
4881}
4882
4883/*
4884 * Handle fifth level expression:
4885 * * number multiplication
4886 * / number division
4887 * % number modulo
4888 *
4889 * "arg" must point to the first non-white of the expression.
4890 * "arg" is advanced to the next non-white after the recognized expression.
4891 *
4892 * Return OK or FAIL.
4893 */
4894 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004895eval6(
4896 char_u **arg,
4897 typval_T *rettv,
4898 int evaluate,
4899 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900{
Bram Moolenaar33570922005-01-25 22:26:29 +00004901 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 int op;
4903 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004904#ifdef FEAT_FLOAT
4905 int use_float = FALSE;
4906 float_T f1 = 0, f2;
4907#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004908 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909
4910 /*
4911 * Get the first variable.
4912 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004913 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 return FAIL;
4915
4916 /*
4917 * Repeat computing, until no '*', '/' or '%' is following.
4918 */
4919 for (;;)
4920 {
4921 op = **arg;
4922 if (op != '*' && op != '/' && op != '%')
4923 break;
4924
4925 if (evaluate)
4926 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004927#ifdef FEAT_FLOAT
4928 if (rettv->v_type == VAR_FLOAT)
4929 {
4930 f1 = rettv->vval.v_float;
4931 use_float = TRUE;
4932 n1 = 0;
4933 }
4934 else
4935#endif
4936 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004937 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004938 if (error)
4939 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 }
4941 else
4942 n1 = 0;
4943
4944 /*
4945 * Get the second variable.
4946 */
4947 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004948 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 return FAIL;
4950
4951 if (evaluate)
4952 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004953#ifdef FEAT_FLOAT
4954 if (var2.v_type == VAR_FLOAT)
4955 {
4956 if (!use_float)
4957 {
4958 f1 = n1;
4959 use_float = TRUE;
4960 }
4961 f2 = var2.vval.v_float;
4962 n2 = 0;
4963 }
4964 else
4965#endif
4966 {
4967 n2 = get_tv_number_chk(&var2, &error);
4968 clear_tv(&var2);
4969 if (error)
4970 return FAIL;
4971#ifdef FEAT_FLOAT
4972 if (use_float)
4973 f2 = n2;
4974#endif
4975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976
4977 /*
4978 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004979 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004981#ifdef FEAT_FLOAT
4982 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004984 if (op == '*')
4985 f1 = f1 * f2;
4986 else if (op == '/')
4987 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004988# ifdef VMS
4989 /* VMS crashes on divide by zero, work around it */
4990 if (f2 == 0.0)
4991 {
4992 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004993 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004994 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004995 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004996 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004997 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004998 }
4999 else
5000 f1 = f1 / f2;
5001# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005002 /* We rely on the floating point library to handle divide
5003 * by zero to result in "inf" and not a crash. */
5004 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005005# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005008 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005009 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005010 return FAIL;
5011 }
5012 rettv->v_type = VAR_FLOAT;
5013 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 }
5015 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005018 if (op == '*')
5019 n1 = n1 * n2;
5020 else if (op == '/')
5021 {
5022 if (n2 == 0) /* give an error message? */
5023 {
5024 if (n1 == 0)
5025 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5026 else if (n1 < 0)
5027 n1 = -0x7fffffffL;
5028 else
5029 n1 = 0x7fffffffL;
5030 }
5031 else
5032 n1 = n1 / n2;
5033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005035 {
5036 if (n2 == 0) /* give an error message? */
5037 n1 = 0;
5038 else
5039 n1 = n1 % n2;
5040 }
5041 rettv->v_type = VAR_NUMBER;
5042 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 }
5045 }
5046
5047 return OK;
5048}
5049
5050/*
5051 * Handle sixth level expression:
5052 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005053 * "string" string constant
5054 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 * &option-name option value
5056 * @r register contents
5057 * identifier variable value
5058 * function() function call
5059 * $VAR environment variable
5060 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005061 * [expr, expr] List
5062 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 *
5064 * Also handle:
5065 * ! in front logical NOT
5066 * - in front unary minus
5067 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005068 * trailing [] subscript in String or List
5069 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 *
5071 * "arg" must point to the first non-white of the expression.
5072 * "arg" is advanced to the next non-white after the recognized expression.
5073 *
5074 * Return OK or FAIL.
5075 */
5076 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005077eval7(
5078 char_u **arg,
5079 typval_T *rettv,
5080 int evaluate,
5081 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 long n;
5084 int len;
5085 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 char_u *start_leader, *end_leader;
5087 int ret = OK;
5088 char_u *alias;
5089
5090 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005091 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005092 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005094 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095
5096 /*
5097 * Skip '!' and '-' characters. They are handled later.
5098 */
5099 start_leader = *arg;
5100 while (**arg == '!' || **arg == '-' || **arg == '+')
5101 *arg = skipwhite(*arg + 1);
5102 end_leader = *arg;
5103
5104 switch (**arg)
5105 {
5106 /*
5107 * Number constant.
5108 */
5109 case '0':
5110 case '1':
5111 case '2':
5112 case '3':
5113 case '4':
5114 case '5':
5115 case '6':
5116 case '7':
5117 case '8':
5118 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005119 {
5120#ifdef FEAT_FLOAT
5121 char_u *p = skipdigits(*arg + 1);
5122 int get_float = FALSE;
5123
5124 /* We accept a float when the format matches
5125 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005126 * strict to avoid backwards compatibility problems.
5127 * Don't look for a float after the "." operator, so that
5128 * ":let vers = 1.2.3" doesn't fail. */
5129 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005131 get_float = TRUE;
5132 p = skipdigits(p + 2);
5133 if (*p == 'e' || *p == 'E')
5134 {
5135 ++p;
5136 if (*p == '-' || *p == '+')
5137 ++p;
5138 if (!vim_isdigit(*p))
5139 get_float = FALSE;
5140 else
5141 p = skipdigits(p + 1);
5142 }
5143 if (ASCII_ISALPHA(*p) || *p == '.')
5144 get_float = FALSE;
5145 }
5146 if (get_float)
5147 {
5148 float_T f;
5149
5150 *arg += string2float(*arg, &f);
5151 if (evaluate)
5152 {
5153 rettv->v_type = VAR_FLOAT;
5154 rettv->vval.v_float = f;
5155 }
5156 }
5157 else
5158#endif
5159 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005160 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005161 *arg += len;
5162 if (evaluate)
5163 {
5164 rettv->v_type = VAR_NUMBER;
5165 rettv->vval.v_number = n;
5166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 }
5168 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170
5171 /*
5172 * String constant: "string".
5173 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005174 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 break;
5176
5177 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005180 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005181 break;
5182
5183 /*
5184 * List: [expr, expr]
5185 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005186 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 break;
5188
5189 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005190 * Dictionary: {key: val, key: val}
5191 */
5192 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5193 break;
5194
5195 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005196 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005198 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 break;
5200
5201 /*
5202 * Environment variable: $VAR.
5203 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005204 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 break;
5206
5207 /*
5208 * Register contents: @r.
5209 */
5210 case '@': ++*arg;
5211 if (evaluate)
5212 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005213 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005214 rettv->vval.v_string = get_reg_contents(**arg,
5215 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 }
5217 if (**arg != NUL)
5218 ++*arg;
5219 break;
5220
5221 /*
5222 * nested expression: (expression).
5223 */
5224 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005225 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 if (**arg == ')')
5227 ++*arg;
5228 else if (ret == OK)
5229 {
5230 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005231 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 ret = FAIL;
5233 }
5234 break;
5235
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 break;
5238 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239
5240 if (ret == NOTDONE)
5241 {
5242 /*
5243 * Must be a variable or function name.
5244 * Can also be a curly-braces kind of name: {expr}.
5245 */
5246 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005247 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248 if (alias != NULL)
5249 s = alias;
5250
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005251 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 ret = FAIL;
5253 else
5254 {
5255 if (**arg == '(') /* recursive! */
5256 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005257 partial_T *partial;
5258
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 /* If "s" is the name of a variable of type VAR_FUNC
5260 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005261 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005262
5263 /* Invoke the function. */
5264 ret = get_func_tv(s, len, rettv, arg,
5265 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005266 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005267
5268 /* If evaluate is FALSE rettv->v_type was not set in
5269 * get_func_tv, but it's needed in handle_subscript() to parse
5270 * what follows. So set it here. */
5271 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5272 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005273 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005274 rettv->v_type = VAR_FUNC;
5275 }
5276
Bram Moolenaar8c711452005-01-14 21:53:12 +00005277 /* Stop the expression evaluation when immediately
5278 * aborting on error, or when an interrupt occurred or
5279 * an exception was thrown but not caught. */
5280 if (aborting())
5281 {
5282 if (ret == OK)
5283 clear_tv(rettv);
5284 ret = FAIL;
5285 }
5286 }
5287 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005288 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005289 else
5290 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005292 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005293 }
5294
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 *arg = skipwhite(*arg);
5296
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005297 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5298 * expr(expr). */
5299 if (ret == OK)
5300 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301
5302 /*
5303 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5304 */
5305 if (ret == OK && evaluate && end_leader > start_leader)
5306 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005307 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005308 int val = 0;
5309#ifdef FEAT_FLOAT
5310 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005311
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005312 if (rettv->v_type == VAR_FLOAT)
5313 f = rettv->vval.v_float;
5314 else
5315#endif
5316 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005317 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005319 clear_tv(rettv);
5320 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005322 else
5323 {
5324 while (end_leader > start_leader)
5325 {
5326 --end_leader;
5327 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 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005337 {
5338#ifdef FEAT_FLOAT
5339 if (rettv->v_type == VAR_FLOAT)
5340 f = -f;
5341 else
5342#endif
5343 val = -val;
5344 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005345 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005346#ifdef FEAT_FLOAT
5347 if (rettv->v_type == VAR_FLOAT)
5348 {
5349 clear_tv(rettv);
5350 rettv->vval.v_float = f;
5351 }
5352 else
5353#endif
5354 {
5355 clear_tv(rettv);
5356 rettv->v_type = VAR_NUMBER;
5357 rettv->vval.v_number = val;
5358 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 }
5361
5362 return ret;
5363}
5364
5365/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005366 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5367 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5369 */
5370 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005371eval_index(
5372 char_u **arg,
5373 typval_T *rettv,
5374 int evaluate,
5375 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005376{
5377 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005378 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005380 long len = -1;
5381 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005383 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384
Bram Moolenaara03f2332016-02-06 18:09:59 +01005385 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005387 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005388 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005389 if (verbose)
5390 EMSG(_("E695: Cannot index a Funcref"));
5391 return FAIL;
5392 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005393#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005394 if (verbose)
5395 EMSG(_(e_float_as_string));
5396 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005397#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005398 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005399 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005400 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005401 if (verbose)
5402 EMSG(_("E909: Cannot index a special variable"));
5403 return FAIL;
5404 case VAR_UNKNOWN:
5405 if (evaluate)
5406 return FAIL;
5407 /* FALLTHROUGH */
5408
5409 case VAR_STRING:
5410 case VAR_NUMBER:
5411 case VAR_LIST:
5412 case VAR_DICT:
5413 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005414 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005416 init_tv(&var1);
5417 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005418 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005419 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005420 /*
5421 * dict.name
5422 */
5423 key = *arg + 1;
5424 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5425 ;
5426 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005428 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 }
5430 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005432 /*
5433 * something[idx]
5434 *
5435 * Get the (first) variable from inside the [].
5436 */
5437 *arg = skipwhite(*arg + 1);
5438 if (**arg == ':')
5439 empty1 = TRUE;
5440 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5441 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005442 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5443 {
5444 /* not a number or string */
5445 clear_tv(&var1);
5446 return FAIL;
5447 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005448
5449 /*
5450 * Get the second variable from inside the [:].
5451 */
5452 if (**arg == ':')
5453 {
5454 range = TRUE;
5455 *arg = skipwhite(*arg + 1);
5456 if (**arg == ']')
5457 empty2 = TRUE;
5458 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5459 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005460 if (!empty1)
5461 clear_tv(&var1);
5462 return FAIL;
5463 }
5464 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5465 {
5466 /* not a number or string */
5467 if (!empty1)
5468 clear_tv(&var1);
5469 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005470 return FAIL;
5471 }
5472 }
5473
5474 /* Check for the ']'. */
5475 if (**arg != ']')
5476 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005477 if (verbose)
5478 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005479 clear_tv(&var1);
5480 if (range)
5481 clear_tv(&var2);
5482 return FAIL;
5483 }
5484 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005485 }
5486
5487 if (evaluate)
5488 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005489 n1 = 0;
5490 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005491 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005492 n1 = get_tv_number(&var1);
5493 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494 }
5495 if (range)
5496 {
5497 if (empty2)
5498 n2 = -1;
5499 else
5500 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005501 n2 = get_tv_number(&var2);
5502 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005503 }
5504 }
5505
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005506 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005507 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005508 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005509 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005510 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005511 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005512 case VAR_SPECIAL:
5513 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005514 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005515 break; /* not evaluating, skipping over subscript */
5516
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005517 case VAR_NUMBER:
5518 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005519 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005520 len = (long)STRLEN(s);
5521 if (range)
5522 {
5523 /* The resulting variable is a substring. If the indexes
5524 * are out of range the result is empty. */
5525 if (n1 < 0)
5526 {
5527 n1 = len + n1;
5528 if (n1 < 0)
5529 n1 = 0;
5530 }
5531 if (n2 < 0)
5532 n2 = len + n2;
5533 else if (n2 >= len)
5534 n2 = len;
5535 if (n1 >= len || n2 < 0 || n1 > n2)
5536 s = NULL;
5537 else
5538 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5539 }
5540 else
5541 {
5542 /* The resulting variable is a string of a single
5543 * character. If the index is too big or negative the
5544 * result is empty. */
5545 if (n1 >= len || n1 < 0)
5546 s = NULL;
5547 else
5548 s = vim_strnsave(s + n1, 1);
5549 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005550 clear_tv(rettv);
5551 rettv->v_type = VAR_STRING;
5552 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005553 break;
5554
5555 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005556 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005557 if (n1 < 0)
5558 n1 = len + n1;
5559 if (!empty1 && (n1 < 0 || n1 >= len))
5560 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005561 /* For a range we allow invalid values and return an empty
5562 * list. A list index out of range is an error. */
5563 if (!range)
5564 {
5565 if (verbose)
5566 EMSGN(_(e_listidx), n1);
5567 return FAIL;
5568 }
5569 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005570 }
5571 if (range)
5572 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005573 list_T *l;
5574 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005575
5576 if (n2 < 0)
5577 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005578 else if (n2 >= len)
5579 n2 = len - 1;
5580 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005581 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005582 l = list_alloc();
5583 if (l == NULL)
5584 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005585 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005586 n1 <= n2; ++n1)
5587 {
5588 if (list_append_tv(l, &item->li_tv) == FAIL)
5589 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005590 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005591 return FAIL;
5592 }
5593 item = item->li_next;
5594 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005595 clear_tv(rettv);
5596 rettv->v_type = VAR_LIST;
5597 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005598 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005599 }
5600 else
5601 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005602 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005603 clear_tv(rettv);
5604 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005605 }
5606 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005607
5608 case VAR_DICT:
5609 if (range)
5610 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005611 if (verbose)
5612 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005613 if (len == -1)
5614 clear_tv(&var1);
5615 return FAIL;
5616 }
5617 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005618 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005619
5620 if (len == -1)
5621 {
5622 key = get_tv_string(&var1);
5623 if (*key == NUL)
5624 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005625 if (verbose)
5626 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005627 clear_tv(&var1);
5628 return FAIL;
5629 }
5630 }
5631
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005632 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005633
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005634 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005635 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005636 if (len == -1)
5637 clear_tv(&var1);
5638 if (item == NULL)
5639 return FAIL;
5640
5641 copy_tv(&item->di_tv, &var1);
5642 clear_tv(rettv);
5643 *rettv = var1;
5644 }
5645 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005646 }
5647 }
5648
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005649 return OK;
5650}
5651
5652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 * Get an option value.
5654 * "arg" points to the '&' or '+' before the option name.
5655 * "arg" is advanced to character after the option name.
5656 * Return OK or FAIL.
5657 */
5658 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005659get_option_tv(
5660 char_u **arg,
5661 typval_T *rettv, /* when NULL, only check if option exists */
5662 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663{
5664 char_u *option_end;
5665 long numval;
5666 char_u *stringval;
5667 int opt_type;
5668 int c;
5669 int working = (**arg == '+'); /* has("+option") */
5670 int ret = OK;
5671 int opt_flags;
5672
5673 /*
5674 * Isolate the option name and find its value.
5675 */
5676 option_end = find_option_end(arg, &opt_flags);
5677 if (option_end == NULL)
5678 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005679 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 EMSG2(_("E112: Option name missing: %s"), *arg);
5681 return FAIL;
5682 }
5683
5684 if (!evaluate)
5685 {
5686 *arg = option_end;
5687 return OK;
5688 }
5689
5690 c = *option_end;
5691 *option_end = NUL;
5692 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005693 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694
5695 if (opt_type == -3) /* invalid name */
5696 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005697 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 EMSG2(_("E113: Unknown option: %s"), *arg);
5699 ret = FAIL;
5700 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005701 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 {
5703 if (opt_type == -2) /* hidden string option */
5704 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005705 rettv->v_type = VAR_STRING;
5706 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 }
5708 else if (opt_type == -1) /* hidden number option */
5709 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005710 rettv->v_type = VAR_NUMBER;
5711 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 }
5713 else if (opt_type == 1) /* number option */
5714 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005715 rettv->v_type = VAR_NUMBER;
5716 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 }
5718 else /* string option */
5719 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005720 rettv->v_type = VAR_STRING;
5721 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 }
5723 }
5724 else if (working && (opt_type == -2 || opt_type == -1))
5725 ret = FAIL;
5726
5727 *option_end = c; /* put back for error messages */
5728 *arg = option_end;
5729
5730 return ret;
5731}
5732
5733/*
5734 * Allocate a variable for a string constant.
5735 * Return OK or FAIL.
5736 */
5737 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005738get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739{
5740 char_u *p;
5741 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 int extra = 0;
5743
5744 /*
5745 * Find the end of the string, skipping backslashed characters.
5746 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005747 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 {
5749 if (*p == '\\' && p[1] != NUL)
5750 {
5751 ++p;
5752 /* A "\<x>" form occupies at least 4 characters, and produces up
5753 * to 6 characters: reserve space for 2 extra */
5754 if (*p == '<')
5755 extra += 2;
5756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 }
5758
5759 if (*p != '"')
5760 {
5761 EMSG2(_("E114: Missing quote: %s"), *arg);
5762 return FAIL;
5763 }
5764
5765 /* If only parsing, set *arg and return here */
5766 if (!evaluate)
5767 {
5768 *arg = p + 1;
5769 return OK;
5770 }
5771
5772 /*
5773 * Copy the string into allocated memory, handling backslashed
5774 * characters.
5775 */
5776 name = alloc((unsigned)(p - *arg + extra));
5777 if (name == NULL)
5778 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 rettv->v_type = VAR_STRING;
5780 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 {
5784 if (*p == '\\')
5785 {
5786 switch (*++p)
5787 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005788 case 'b': *name++ = BS; ++p; break;
5789 case 'e': *name++ = ESC; ++p; break;
5790 case 'f': *name++ = FF; ++p; break;
5791 case 'n': *name++ = NL; ++p; break;
5792 case 'r': *name++ = CAR; ++p; break;
5793 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794
5795 case 'X': /* hex: "\x1", "\x12" */
5796 case 'x':
5797 case 'u': /* Unicode: "\u0023" */
5798 case 'U':
5799 if (vim_isxdigit(p[1]))
5800 {
5801 int n, nr;
5802 int c = toupper(*p);
5803
5804 if (c == 'X')
5805 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005806 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005808 else
5809 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 nr = 0;
5811 while (--n >= 0 && vim_isxdigit(p[1]))
5812 {
5813 ++p;
5814 nr = (nr << 4) + hex2nr(*p);
5815 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817#ifdef FEAT_MBYTE
5818 /* For "\u" store the number according to
5819 * 'encoding'. */
5820 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005821 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 else
5823#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005824 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 break;
5827
5828 /* octal: "\1", "\12", "\123" */
5829 case '0':
5830 case '1':
5831 case '2':
5832 case '3':
5833 case '4':
5834 case '5':
5835 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 case '7': *name = *p++ - '0';
5837 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005839 *name = (*name << 3) + *p++ - '0';
5840 if (*p >= '0' && *p <= '7')
5841 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 break;
5845
5846 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 if (extra != 0)
5849 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005850 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 break;
5852 }
5853 /* FALLTHROUGH */
5854
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 break;
5857 }
5858 }
5859 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005863 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 *arg = p + 1;
5865
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 return OK;
5867}
5868
5869/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005870 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 * Return OK or FAIL.
5872 */
5873 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005874get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875{
5876 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005877 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005878 int reduce = 0;
5879
5880 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 */
5883 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
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 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888 break;
5889 ++reduce;
5890 ++p;
5891 }
5892 }
5893
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005897 return FAIL;
5898 }
5899
Bram Moolenaar8c711452005-01-14 21:53:12 +00005900 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005901 if (!evaluate)
5902 {
5903 *arg = p + 1;
5904 return OK;
5905 }
5906
5907 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005908 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005909 */
5910 str = alloc((unsigned)((p - *arg) - reduce));
5911 if (str == NULL)
5912 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005913 rettv->v_type = VAR_STRING;
5914 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005915
Bram Moolenaar8c711452005-01-14 21:53:12 +00005916 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005917 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005918 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005919 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005920 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005921 break;
5922 ++p;
5923 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005924 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005925 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005926 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005927 *arg = p + 1;
5928
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005929 return OK;
5930}
5931
5932/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933 * Allocate a variable for a List and fill it from "*arg".
5934 * Return OK or FAIL.
5935 */
5936 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005937get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938{
Bram Moolenaar33570922005-01-25 22:26:29 +00005939 list_T *l = NULL;
5940 typval_T tv;
5941 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942
5943 if (evaluate)
5944 {
5945 l = list_alloc();
5946 if (l == NULL)
5947 return FAIL;
5948 }
5949
5950 *arg = skipwhite(*arg + 1);
5951 while (**arg != ']' && **arg != NUL)
5952 {
5953 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5954 goto failret;
5955 if (evaluate)
5956 {
5957 item = listitem_alloc();
5958 if (item != NULL)
5959 {
5960 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005961 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962 list_append(l, item);
5963 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005964 else
5965 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966 }
5967
5968 if (**arg == ']')
5969 break;
5970 if (**arg != ',')
5971 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005972 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973 goto failret;
5974 }
5975 *arg = skipwhite(*arg + 1);
5976 }
5977
5978 if (**arg != ']')
5979 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005980 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981failret:
5982 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005983 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984 return FAIL;
5985 }
5986
5987 *arg = skipwhite(*arg + 1);
5988 if (evaluate)
5989 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005990 rettv->v_type = VAR_LIST;
5991 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992 ++l->lv_refcount;
5993 }
5994
5995 return OK;
5996}
5997
5998/*
5999 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006000 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006001 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006002 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006003list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006004{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006005 list_T *l;
6006
6007 l = (list_T *)alloc_clear(sizeof(list_T));
6008 if (l != NULL)
6009 {
6010 /* Prepend the list to the list of lists for garbage collection. */
6011 if (first_list != NULL)
6012 first_list->lv_used_prev = l;
6013 l->lv_used_prev = NULL;
6014 l->lv_used_next = first_list;
6015 first_list = l;
6016 }
6017 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018}
6019
6020/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006021 * Allocate an empty list for a return value.
6022 * Returns OK or FAIL.
6023 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006024 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006025rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006026{
6027 list_T *l = list_alloc();
6028
6029 if (l == NULL)
6030 return FAIL;
6031
6032 rettv->vval.v_list = l;
6033 rettv->v_type = VAR_LIST;
6034 ++l->lv_refcount;
6035 return OK;
6036}
6037
6038/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006039 * Unreference a list: decrement the reference count and free it when it
6040 * becomes zero.
6041 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006042 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006043list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006044{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006045 if (l != NULL && --l->lv_refcount <= 0)
6046 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047}
6048
6049/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006050 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051 * Ignores the reference count.
6052 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006053 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006054list_free(
6055 list_T *l,
6056 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006057{
Bram Moolenaar33570922005-01-25 22:26:29 +00006058 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006060 /* Remove the list from the list of lists for garbage collection. */
6061 if (l->lv_used_prev == NULL)
6062 first_list = l->lv_used_next;
6063 else
6064 l->lv_used_prev->lv_used_next = l->lv_used_next;
6065 if (l->lv_used_next != NULL)
6066 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6067
Bram Moolenaard9fba312005-06-26 22:34:35 +00006068 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006070 /* Remove the item before deleting it. */
6071 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006072 if (recurse || (item->li_tv.v_type != VAR_LIST
6073 && item->li_tv.v_type != VAR_DICT))
6074 clear_tv(&item->li_tv);
6075 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006076 }
6077 vim_free(l);
6078}
6079
6080/*
6081 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006082 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006083 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006084 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006085listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006086{
Bram Moolenaar33570922005-01-25 22:26:29 +00006087 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088}
6089
6090/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006091 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006092 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006093 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006094listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006095{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006096 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006097 vim_free(item);
6098}
6099
6100/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006101 * Remove a list item from a List and free it. Also clears the value.
6102 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006103 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006104listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006105{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006106 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006107 listitem_free(item);
6108}
6109
6110/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006111 * Get the number of items in a list.
6112 */
6113 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006114list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006115{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006116 if (l == NULL)
6117 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006118 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006119}
6120
6121/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006122 * Return TRUE when two lists have exactly the same values.
6123 */
6124 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006125list_equal(
6126 list_T *l1,
6127 list_T *l2,
6128 int ic, /* ignore case for strings */
6129 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006130{
Bram Moolenaar33570922005-01-25 22:26:29 +00006131 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006132
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006133 if (l1 == NULL || l2 == NULL)
6134 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006135 if (l1 == l2)
6136 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006137 if (list_len(l1) != list_len(l2))
6138 return FALSE;
6139
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006140 for (item1 = l1->lv_first, item2 = l2->lv_first;
6141 item1 != NULL && item2 != NULL;
6142 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006143 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006144 return FALSE;
6145 return item1 == NULL && item2 == NULL;
6146}
6147
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006148/*
6149 * Return the dictitem that an entry in a hashtable points to.
6150 */
6151 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006152dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006153{
6154 return HI2DI(hi);
6155}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006156
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006157/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006158 * Return TRUE when two dictionaries have exactly the same key/values.
6159 */
6160 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006161dict_equal(
6162 dict_T *d1,
6163 dict_T *d2,
6164 int ic, /* ignore case for strings */
6165 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006166{
Bram Moolenaar33570922005-01-25 22:26:29 +00006167 hashitem_T *hi;
6168 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006169 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006170
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006171 if (d1 == NULL || d2 == NULL)
6172 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006173 if (d1 == d2)
6174 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006175 if (dict_len(d1) != dict_len(d2))
6176 return FALSE;
6177
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006178 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006179 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006180 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006181 if (!HASHITEM_EMPTY(hi))
6182 {
6183 item2 = dict_find(d2, hi->hi_key, -1);
6184 if (item2 == NULL)
6185 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006186 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006187 return FALSE;
6188 --todo;
6189 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006190 }
6191 return TRUE;
6192}
6193
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006194static int tv_equal_recurse_limit;
6195
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006196/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006197 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006198 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006199 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006200 */
6201 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006202tv_equal(
6203 typval_T *tv1,
6204 typval_T *tv2,
6205 int ic, /* ignore case */
6206 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006207{
6208 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006209 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006210 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006211 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006212
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006213 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006214 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006215
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006216 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006217 * recursiveness to a limit. We guess they are equal then.
6218 * A fixed limit has the problem of still taking an awful long time.
6219 * Reduce the limit every time running into it. That should work fine for
6220 * deeply linked structures that are not recursively linked and catch
6221 * recursiveness quickly. */
6222 if (!recursive)
6223 tv_equal_recurse_limit = 1000;
6224 if (recursive_cnt >= tv_equal_recurse_limit)
6225 {
6226 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006227 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006228 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006229
6230 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006231 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006232 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006233 ++recursive_cnt;
6234 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6235 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006236 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006237
6238 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006239 ++recursive_cnt;
6240 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6241 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006242 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006243
6244 case VAR_FUNC:
6245 return (tv1->vval.v_string != NULL
6246 && tv2->vval.v_string != NULL
6247 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6248
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006249 case VAR_PARTIAL:
6250 return tv1->vval.v_partial != NULL
6251 && tv1->vval.v_partial == tv2->vval.v_partial;
6252
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006253 case VAR_NUMBER:
6254 return tv1->vval.v_number == tv2->vval.v_number;
6255
6256 case VAR_STRING:
6257 s1 = get_tv_string_buf(tv1, buf1);
6258 s2 = get_tv_string_buf(tv2, buf2);
6259 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006260
6261 case VAR_SPECIAL:
6262 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006263
6264 case VAR_FLOAT:
6265#ifdef FEAT_FLOAT
6266 return tv1->vval.v_float == tv2->vval.v_float;
6267#endif
6268 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006269#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006270 return tv1->vval.v_job == tv2->vval.v_job;
6271#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006272 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006273#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006274 return tv1->vval.v_channel == tv2->vval.v_channel;
6275#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006276 case VAR_UNKNOWN:
6277 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006278 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006279
Bram Moolenaara03f2332016-02-06 18:09:59 +01006280 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6281 * does not equal anything, not even itself. */
6282 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006283}
6284
6285/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006286 * Locate item with index "n" in list "l" and return it.
6287 * A negative index is counted from the end; -1 is the last item.
6288 * Returns NULL when "n" is out of range.
6289 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006290 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006291list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006292{
Bram Moolenaar33570922005-01-25 22:26:29 +00006293 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006294 long idx;
6295
6296 if (l == NULL)
6297 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006298
6299 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006300 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006301 n = l->lv_len + n;
6302
6303 /* Check for index out of range. */
6304 if (n < 0 || n >= l->lv_len)
6305 return NULL;
6306
6307 /* When there is a cached index may start search from there. */
6308 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006309 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006310 if (n < l->lv_idx / 2)
6311 {
6312 /* closest to the start of the list */
6313 item = l->lv_first;
6314 idx = 0;
6315 }
6316 else if (n > (l->lv_idx + l->lv_len) / 2)
6317 {
6318 /* closest to the end of the list */
6319 item = l->lv_last;
6320 idx = l->lv_len - 1;
6321 }
6322 else
6323 {
6324 /* closest to the cached index */
6325 item = l->lv_idx_item;
6326 idx = l->lv_idx;
6327 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006328 }
6329 else
6330 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006331 if (n < l->lv_len / 2)
6332 {
6333 /* closest to the start of the list */
6334 item = l->lv_first;
6335 idx = 0;
6336 }
6337 else
6338 {
6339 /* closest to the end of the list */
6340 item = l->lv_last;
6341 idx = l->lv_len - 1;
6342 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006343 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006344
6345 while (n > idx)
6346 {
6347 /* search forward */
6348 item = item->li_next;
6349 ++idx;
6350 }
6351 while (n < idx)
6352 {
6353 /* search backward */
6354 item = item->li_prev;
6355 --idx;
6356 }
6357
6358 /* cache the used index */
6359 l->lv_idx = idx;
6360 l->lv_idx_item = item;
6361
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006362 return item;
6363}
6364
6365/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006366 * Get list item "l[idx]" as a number.
6367 */
6368 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006369list_find_nr(
6370 list_T *l,
6371 long idx,
6372 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006373{
6374 listitem_T *li;
6375
6376 li = list_find(l, idx);
6377 if (li == NULL)
6378 {
6379 if (errorp != NULL)
6380 *errorp = TRUE;
6381 return -1L;
6382 }
6383 return get_tv_number_chk(&li->li_tv, errorp);
6384}
6385
6386/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006387 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6388 */
6389 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006390list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006391{
6392 listitem_T *li;
6393
6394 li = list_find(l, idx - 1);
6395 if (li == NULL)
6396 {
6397 EMSGN(_(e_listidx), idx);
6398 return NULL;
6399 }
6400 return get_tv_string(&li->li_tv);
6401}
6402
6403/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006404 * Locate "item" list "l" and return its index.
6405 * Returns -1 when "item" is not in the list.
6406 */
6407 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006408list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006409{
6410 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006412
6413 if (l == NULL)
6414 return -1;
6415 idx = 0;
6416 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6417 ++idx;
6418 if (li == NULL)
6419 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006420 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006421}
6422
6423/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006424 * Append item "item" to the end of list "l".
6425 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006426 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006427list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006428{
6429 if (l->lv_last == NULL)
6430 {
6431 /* empty list */
6432 l->lv_first = item;
6433 l->lv_last = item;
6434 item->li_prev = NULL;
6435 }
6436 else
6437 {
6438 l->lv_last->li_next = item;
6439 item->li_prev = l->lv_last;
6440 l->lv_last = item;
6441 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006442 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006443 item->li_next = NULL;
6444}
6445
6446/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006447 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006449 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006450 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006451list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006452{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006453 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006454
Bram Moolenaar05159a02005-02-26 23:04:13 +00006455 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006456 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006457 copy_tv(tv, &li->li_tv);
6458 list_append(l, li);
6459 return OK;
6460}
6461
6462/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006463 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006464 * Return FAIL when out of memory.
6465 */
6466 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006467list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006468{
6469 listitem_T *li = listitem_alloc();
6470
6471 if (li == NULL)
6472 return FAIL;
6473 li->li_tv.v_type = VAR_DICT;
6474 li->li_tv.v_lock = 0;
6475 li->li_tv.vval.v_dict = dict;
6476 list_append(list, li);
6477 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006478 return OK;
6479}
6480
6481/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006482 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006483 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006484 * Returns FAIL when out of memory.
6485 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006486 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006487list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006488{
6489 listitem_T *li = listitem_alloc();
6490
6491 if (li == NULL)
6492 return FAIL;
6493 list_append(l, li);
6494 li->li_tv.v_type = VAR_STRING;
6495 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006496 if (str == NULL)
6497 li->li_tv.vval.v_string = NULL;
6498 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006499 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006500 return FAIL;
6501 return OK;
6502}
6503
6504/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006505 * Append "n" to list "l".
6506 * Returns FAIL when out of memory.
6507 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006508 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006509list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006510{
6511 listitem_T *li;
6512
6513 li = listitem_alloc();
6514 if (li == NULL)
6515 return FAIL;
6516 li->li_tv.v_type = VAR_NUMBER;
6517 li->li_tv.v_lock = 0;
6518 li->li_tv.vval.v_number = n;
6519 list_append(l, li);
6520 return OK;
6521}
6522
6523/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006524 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006525 * If "item" is NULL append at the end.
6526 * Return FAIL when out of memory.
6527 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006528 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006529list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530{
Bram Moolenaar33570922005-01-25 22:26:29 +00006531 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006532
6533 if (ni == NULL)
6534 return FAIL;
6535 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006536 list_insert(l, ni, item);
6537 return OK;
6538}
6539
6540 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006541list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006542{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006543 if (item == NULL)
6544 /* Append new item at end of list. */
6545 list_append(l, ni);
6546 else
6547 {
6548 /* Insert new item before existing item. */
6549 ni->li_prev = item->li_prev;
6550 ni->li_next = item;
6551 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006552 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006553 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006554 ++l->lv_idx;
6555 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006556 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006557 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006558 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006559 l->lv_idx_item = NULL;
6560 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006561 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006562 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006564}
6565
6566/*
6567 * Extend "l1" with "l2".
6568 * If "bef" is NULL append at the end, otherwise insert before this item.
6569 * Returns FAIL when out of memory.
6570 */
6571 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006572list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006573{
Bram Moolenaar33570922005-01-25 22:26:29 +00006574 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006575 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006576
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006577 /* We also quit the loop when we have inserted the original item count of
6578 * the list, avoid a hang when we extend a list with itself. */
6579 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006580 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6581 return FAIL;
6582 return OK;
6583}
6584
6585/*
6586 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6587 * Return FAIL when out of memory.
6588 */
6589 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006590list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006591{
Bram Moolenaar33570922005-01-25 22:26:29 +00006592 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006593
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006594 if (l1 == NULL || l2 == NULL)
6595 return FAIL;
6596
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006597 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006598 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006599 if (l == NULL)
6600 return FAIL;
6601 tv->v_type = VAR_LIST;
6602 tv->vval.v_list = l;
6603
6604 /* append all items from the second list */
6605 return list_extend(l, l2, NULL);
6606}
6607
6608/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006609 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006610 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006611 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006612 * Returns NULL when out of memory.
6613 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006614 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006615list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006616{
Bram Moolenaar33570922005-01-25 22:26:29 +00006617 list_T *copy;
6618 listitem_T *item;
6619 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006620
6621 if (orig == NULL)
6622 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623
6624 copy = list_alloc();
6625 if (copy != NULL)
6626 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006627 if (copyID != 0)
6628 {
6629 /* Do this before adding the items, because one of the items may
6630 * refer back to this list. */
6631 orig->lv_copyID = copyID;
6632 orig->lv_copylist = copy;
6633 }
6634 for (item = orig->lv_first; item != NULL && !got_int;
6635 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006636 {
6637 ni = listitem_alloc();
6638 if (ni == NULL)
6639 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006640 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006641 {
6642 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6643 {
6644 vim_free(ni);
6645 break;
6646 }
6647 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006648 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006649 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006650 list_append(copy, ni);
6651 }
6652 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006653 if (item != NULL)
6654 {
6655 list_unref(copy);
6656 copy = NULL;
6657 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006658 }
6659
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006660 return copy;
6661}
6662
6663/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006664 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006665 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006666 * This used to be called list_remove, but that conflicts with a Sun header
6667 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006668 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006669 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006670vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006671{
Bram Moolenaar33570922005-01-25 22:26:29 +00006672 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006673
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006674 /* notify watchers */
6675 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006676 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006677 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006678 list_fix_watch(l, ip);
6679 if (ip == item2)
6680 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006681 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006682
6683 if (item2->li_next == NULL)
6684 l->lv_last = item->li_prev;
6685 else
6686 item2->li_next->li_prev = item->li_prev;
6687 if (item->li_prev == NULL)
6688 l->lv_first = item2->li_next;
6689 else
6690 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006691 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006692}
6693
6694/*
6695 * Return an allocated string with the string representation of a list.
6696 * May return NULL.
6697 */
6698 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006699list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006700{
6701 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006702
6703 if (tv->vval.v_list == NULL)
6704 return NULL;
6705 ga_init2(&ga, (int)sizeof(char), 80);
6706 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006707 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006708 {
6709 vim_free(ga.ga_data);
6710 return NULL;
6711 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006712 ga_append(&ga, ']');
6713 ga_append(&ga, NUL);
6714 return (char_u *)ga.ga_data;
6715}
6716
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006717typedef struct join_S {
6718 char_u *s;
6719 char_u *tofree;
6720} join_T;
6721
6722 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006723list_join_inner(
6724 garray_T *gap, /* to store the result in */
6725 list_T *l,
6726 char_u *sep,
6727 int echo_style,
6728 int copyID,
6729 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006730{
6731 int i;
6732 join_T *p;
6733 int len;
6734 int sumlen = 0;
6735 int first = TRUE;
6736 char_u *tofree;
6737 char_u numbuf[NUMBUFLEN];
6738 listitem_T *item;
6739 char_u *s;
6740
6741 /* Stringify each item in the list. */
6742 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6743 {
6744 if (echo_style)
6745 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6746 else
6747 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6748 if (s == NULL)
6749 return FAIL;
6750
6751 len = (int)STRLEN(s);
6752 sumlen += len;
6753
Bram Moolenaarcde88542015-08-11 19:14:00 +02006754 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006755 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6756 if (tofree != NULL || s != numbuf)
6757 {
6758 p->s = s;
6759 p->tofree = tofree;
6760 }
6761 else
6762 {
6763 p->s = vim_strnsave(s, len);
6764 p->tofree = p->s;
6765 }
6766
6767 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006768 if (did_echo_string_emsg) /* recursion error, bail out */
6769 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006770 }
6771
6772 /* Allocate result buffer with its total size, avoid re-allocation and
6773 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6774 if (join_gap->ga_len >= 2)
6775 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6776 if (ga_grow(gap, sumlen + 2) == FAIL)
6777 return FAIL;
6778
6779 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6780 {
6781 if (first)
6782 first = FALSE;
6783 else
6784 ga_concat(gap, sep);
6785 p = ((join_T *)join_gap->ga_data) + i;
6786
6787 if (p->s != NULL)
6788 ga_concat(gap, p->s);
6789 line_breakcheck();
6790 }
6791
6792 return OK;
6793}
6794
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006795/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006796 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006797 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006798 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006799 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006800 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006801list_join(
6802 garray_T *gap,
6803 list_T *l,
6804 char_u *sep,
6805 int echo_style,
6806 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006807{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006808 garray_T join_ga;
6809 int retval;
6810 join_T *p;
6811 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006812
Bram Moolenaard39a7512015-04-16 22:51:22 +02006813 if (l->lv_len < 1)
6814 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006815 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6816 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6817
6818 /* Dispose each item in join_ga. */
6819 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006820 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006821 p = (join_T *)join_ga.ga_data;
6822 for (i = 0; i < join_ga.ga_len; ++i)
6823 {
6824 vim_free(p->tofree);
6825 ++p;
6826 }
6827 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006828 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006829
6830 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006831}
6832
6833/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006834 * Return the next (unique) copy ID.
6835 * Used for serializing nested structures.
6836 */
6837 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006838get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006839{
6840 current_copyID += COPYID_INC;
6841 return current_copyID;
6842}
6843
6844/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006845 * Garbage collection for lists and dictionaries.
6846 *
6847 * We use reference counts to be able to free most items right away when they
6848 * are no longer used. But for composite items it's possible that it becomes
6849 * unused while the reference count is > 0: When there is a recursive
6850 * reference. Example:
6851 * :let l = [1, 2, 3]
6852 * :let d = {9: l}
6853 * :let l[1] = d
6854 *
6855 * Since this is quite unusual we handle this with garbage collection: every
6856 * once in a while find out which lists and dicts are not referenced from any
6857 * variable.
6858 *
6859 * Here is a good reference text about garbage collection (refers to Python
6860 * but it applies to all reference-counting mechanisms):
6861 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006862 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006863
6864/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006865 * Do garbage collection for lists and dicts.
6866 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006867 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006868 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006869garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006870{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006871 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006872 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006873 buf_T *buf;
6874 win_T *wp;
6875 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006876 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006877 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006878 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006879#ifdef FEAT_WINDOWS
6880 tabpage_T *tp;
6881#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006882
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006883 /* Only do this once. */
6884 want_garbage_collect = FALSE;
6885 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006886 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006887
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006888 /* We advance by two because we add one for items referenced through
6889 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006890 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006891
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006892 /*
6893 * 1. Go through all accessible variables and mark all lists and dicts
6894 * with copyID.
6895 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006896
6897 /* Don't free variables in the previous_funccal list unless they are only
6898 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006899 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006900 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6901 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006902 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6903 NULL);
6904 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6905 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006906 }
6907
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006908 /* script-local variables */
6909 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006910 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911
6912 /* buffer-local variables */
6913 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006914 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6915 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006916
6917 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006918 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006919 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6920 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006921#ifdef FEAT_AUTOCMD
6922 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006923 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6924 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006925#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006926
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006927#ifdef FEAT_WINDOWS
6928 /* tabpage-local variables */
6929 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006930 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6931 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006932#endif
6933
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006934 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006935 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006936
6937 /* function-local variables */
6938 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6939 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006940 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6941 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006942 }
6943
Bram Moolenaard812df62008-11-09 12:46:09 +00006944 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006945 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006946
Bram Moolenaar1dced572012-04-05 16:54:08 +02006947#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006948 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006949#endif
6950
Bram Moolenaardb913952012-06-29 12:54:53 +02006951#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006952 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006953#endif
6954
6955#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006956 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006957#endif
6958
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006959#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006960 abort = abort || set_ref_in_channel(copyID);
6961#endif
6962
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006963 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006964 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006965 /*
6966 * 2. Free lists and dictionaries that are not referenced.
6967 */
6968 did_free = free_unref_items(copyID);
6969
6970 /*
6971 * 3. Check if any funccal can be freed now.
6972 */
6973 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006974 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006975 if (can_free_funccal(*pfc, copyID))
6976 {
6977 fc = *pfc;
6978 *pfc = fc->caller;
6979 free_funccal(fc, TRUE);
6980 did_free = TRUE;
6981 did_free_funccal = TRUE;
6982 }
6983 else
6984 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006985 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006986 if (did_free_funccal)
6987 /* When a funccal was freed some more items might be garbage
6988 * collected, so run again. */
6989 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006990 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006991 else if (p_verbose > 0)
6992 {
6993 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6994 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006995
6996 return did_free;
6997}
6998
6999/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01007000 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007001 */
7002 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007003free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007004{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007005 dict_T *dd, *dd_next;
7006 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007007 int did_free = FALSE;
7008
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007009 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007010 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 */
7012 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007013 {
7014 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007015 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007017 /* Free the Dictionary and ordinary items it contains, but don't
7018 * recurse into Lists and Dictionaries, they will be in the list
7019 * of dicts or list of lists. */
7020 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007022 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007023 dd = dd_next;
7024 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007025
7026 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007027 * Go through the list of lists and free items without the copyID.
7028 * But don't free a list that has a watcher (used in a for loop), these
7029 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007030 */
7031 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007032 {
7033 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007034 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7035 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007036 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007037 /* Free the List and ordinary items it contains, but don't recurse
7038 * into Lists and Dictionaries, they will be in the list of dicts
7039 * or list of lists. */
7040 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007041 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007042 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007043 ll = ll_next;
7044 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007045
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007046 return did_free;
7047}
7048
7049/*
7050 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007051 * "list_stack" is used to add lists to be marked. Can be NULL.
7052 *
7053 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007054 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007055 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007056set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007057{
7058 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007059 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007060 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007061 hashtab_T *cur_ht;
7062 ht_stack_T *ht_stack = NULL;
7063 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007064
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007065 cur_ht = ht;
7066 for (;;)
7067 {
7068 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007069 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007070 /* Mark each item in the hashtab. If the item contains a hashtab
7071 * it is added to ht_stack, if it contains a list it is added to
7072 * list_stack. */
7073 todo = (int)cur_ht->ht_used;
7074 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7075 if (!HASHITEM_EMPTY(hi))
7076 {
7077 --todo;
7078 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7079 &ht_stack, list_stack);
7080 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007081 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007082
7083 if (ht_stack == NULL)
7084 break;
7085
7086 /* take an item from the stack */
7087 cur_ht = ht_stack->ht;
7088 tempitem = ht_stack;
7089 ht_stack = ht_stack->prev;
7090 free(tempitem);
7091 }
7092
7093 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094}
7095
7096/*
7097 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007098 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7099 *
7100 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007101 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007102 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007103set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007104{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007105 listitem_T *li;
7106 int abort = FALSE;
7107 list_T *cur_l;
7108 list_stack_T *list_stack = NULL;
7109 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007110
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007111 cur_l = l;
7112 for (;;)
7113 {
7114 if (!abort)
7115 /* Mark each item in the list. If the item contains a hashtab
7116 * it is added to ht_stack, if it contains a list it is added to
7117 * list_stack. */
7118 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7119 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7120 ht_stack, &list_stack);
7121 if (list_stack == NULL)
7122 break;
7123
7124 /* take an item from the stack */
7125 cur_l = list_stack->list;
7126 tempitem = list_stack;
7127 list_stack = list_stack->prev;
7128 free(tempitem);
7129 }
7130
7131 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007132}
7133
7134/*
7135 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007136 * "list_stack" is used to add lists to be marked. Can be NULL.
7137 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7138 *
7139 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007140 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007141 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007142set_ref_in_item(
7143 typval_T *tv,
7144 int copyID,
7145 ht_stack_T **ht_stack,
7146 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007147{
7148 dict_T *dd;
7149 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007150 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007151
Bram Moolenaara03f2332016-02-06 18:09:59 +01007152 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007153 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007154 dd = tv->vval.v_dict;
7155 if (dd != NULL && dd->dv_copyID != copyID)
7156 {
7157 /* Didn't see this dict yet. */
7158 dd->dv_copyID = copyID;
7159 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007160 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007161 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7162 }
7163 else
7164 {
7165 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7166 if (newitem == NULL)
7167 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007168 else
7169 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007170 newitem->ht = &dd->dv_hashtab;
7171 newitem->prev = *ht_stack;
7172 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007173 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007174 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007175 }
7176 }
7177 else if (tv->v_type == VAR_LIST)
7178 {
7179 ll = tv->vval.v_list;
7180 if (ll != NULL && ll->lv_copyID != copyID)
7181 {
7182 /* Didn't see this list yet. */
7183 ll->lv_copyID = copyID;
7184 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007185 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007186 abort = set_ref_in_list(ll, copyID, ht_stack);
7187 }
7188 else
7189 {
7190 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007191 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007192 if (newitem == NULL)
7193 abort = TRUE;
7194 else
7195 {
7196 newitem->list = ll;
7197 newitem->prev = *list_stack;
7198 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007199 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007200 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007201 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007202 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007203 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007204}
7205
7206/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007207 * Allocate an empty header for a dictionary.
7208 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007209 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007210dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007211{
Bram Moolenaar33570922005-01-25 22:26:29 +00007212 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007213
Bram Moolenaar33570922005-01-25 22:26:29 +00007214 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007215 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007216 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007217 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007218 if (first_dict != NULL)
7219 first_dict->dv_used_prev = d;
7220 d->dv_used_next = first_dict;
7221 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007222 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007223
Bram Moolenaar33570922005-01-25 22:26:29 +00007224 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007225 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007226 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007227 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007228 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007229 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007230 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007231}
7232
7233/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007234 * Allocate an empty dict for a return value.
7235 * Returns OK or FAIL.
7236 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007237 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007238rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007239{
7240 dict_T *d = dict_alloc();
7241
7242 if (d == NULL)
7243 return FAIL;
7244
7245 rettv->vval.v_dict = d;
7246 rettv->v_type = VAR_DICT;
7247 ++d->dv_refcount;
7248 return OK;
7249}
7250
7251
7252/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253 * Unreference a Dictionary: decrement the reference count and free it when it
7254 * becomes zero.
7255 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007256 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007257dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007258{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007259 if (d != NULL && --d->dv_refcount <= 0)
7260 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007261}
7262
7263/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007264 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265 * Ignores the reference count.
7266 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007267 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007268dict_free(
7269 dict_T *d,
7270 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007271{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007272 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007273 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007274 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007275
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007276 /* Remove the dict from the list of dicts for garbage collection. */
7277 if (d->dv_used_prev == NULL)
7278 first_dict = d->dv_used_next;
7279 else
7280 d->dv_used_prev->dv_used_next = d->dv_used_next;
7281 if (d->dv_used_next != NULL)
7282 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7283
7284 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007285 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007286 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007287 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007289 if (!HASHITEM_EMPTY(hi))
7290 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007291 /* Remove the item before deleting it, just in case there is
7292 * something recursive causing trouble. */
7293 di = HI2DI(hi);
7294 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007295 if (recurse || (di->di_tv.v_type != VAR_LIST
7296 && di->di_tv.v_type != VAR_DICT))
7297 clear_tv(&di->di_tv);
7298 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007299 --todo;
7300 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007301 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007302 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007303 vim_free(d);
7304}
7305
7306/*
7307 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007308 * The "key" is copied to the new item.
7309 * Note that the value of the item "di_tv" still needs to be initialized!
7310 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007311 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007312 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007313dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007314{
Bram Moolenaar33570922005-01-25 22:26:29 +00007315 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007316
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007317 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007318 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007319 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007321 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007322 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007323 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007324}
7325
7326/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007327 * Make a copy of a Dictionary item.
7328 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007329 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007330dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007331{
Bram Moolenaar33570922005-01-25 22:26:29 +00007332 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007333
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007334 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7335 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007336 if (di != NULL)
7337 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007338 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007339 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007340 copy_tv(&org->di_tv, &di->di_tv);
7341 }
7342 return di;
7343}
7344
7345/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007346 * Remove item "item" from Dictionary "dict" and free it.
7347 */
7348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007349dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007350{
Bram Moolenaar33570922005-01-25 22:26:29 +00007351 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007352
Bram Moolenaar33570922005-01-25 22:26:29 +00007353 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007354 if (HASHITEM_EMPTY(hi))
7355 EMSG2(_(e_intern2), "dictitem_remove()");
7356 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007357 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007358 dictitem_free(item);
7359}
7360
7361/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007362 * Free a dict item. Also clears the value.
7363 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007364 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007365dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007366{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007367 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007368 if (item->di_flags & DI_FLAGS_ALLOC)
7369 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007370}
7371
7372/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007373 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7374 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007375 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007376 * Returns NULL when out of memory.
7377 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007378 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007379dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007380{
Bram Moolenaar33570922005-01-25 22:26:29 +00007381 dict_T *copy;
7382 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007383 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007384 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007385
7386 if (orig == NULL)
7387 return NULL;
7388
7389 copy = dict_alloc();
7390 if (copy != NULL)
7391 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007392 if (copyID != 0)
7393 {
7394 orig->dv_copyID = copyID;
7395 orig->dv_copydict = copy;
7396 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007397 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007398 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007399 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007400 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007401 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007402 --todo;
7403
7404 di = dictitem_alloc(hi->hi_key);
7405 if (di == NULL)
7406 break;
7407 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007408 {
7409 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7410 copyID) == FAIL)
7411 {
7412 vim_free(di);
7413 break;
7414 }
7415 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007416 else
7417 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7418 if (dict_add(copy, di) == FAIL)
7419 {
7420 dictitem_free(di);
7421 break;
7422 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007423 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007424 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007425
Bram Moolenaare9a41262005-01-15 22:18:47 +00007426 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007427 if (todo > 0)
7428 {
7429 dict_unref(copy);
7430 copy = NULL;
7431 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007432 }
7433
7434 return copy;
7435}
7436
7437/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007438 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007439 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007440 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007441 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007442dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007443{
Bram Moolenaar33570922005-01-25 22:26:29 +00007444 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007445}
7446
Bram Moolenaar8c711452005-01-14 21:53:12 +00007447/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007448 * Add a number or string entry to dictionary "d".
7449 * When "str" is NULL use number "nr", otherwise use "str".
7450 * Returns FAIL when out of memory and when key already exists.
7451 */
7452 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007453dict_add_nr_str(
7454 dict_T *d,
7455 char *key,
7456 long nr,
7457 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007458{
7459 dictitem_T *item;
7460
7461 item = dictitem_alloc((char_u *)key);
7462 if (item == NULL)
7463 return FAIL;
7464 item->di_tv.v_lock = 0;
7465 if (str == NULL)
7466 {
7467 item->di_tv.v_type = VAR_NUMBER;
7468 item->di_tv.vval.v_number = nr;
7469 }
7470 else
7471 {
7472 item->di_tv.v_type = VAR_STRING;
7473 item->di_tv.vval.v_string = vim_strsave(str);
7474 }
7475 if (dict_add(d, item) == FAIL)
7476 {
7477 dictitem_free(item);
7478 return FAIL;
7479 }
7480 return OK;
7481}
7482
7483/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007484 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007485 * Returns FAIL when out of memory and when key already exists.
7486 */
7487 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007488dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007489{
7490 dictitem_T *item;
7491
7492 item = dictitem_alloc((char_u *)key);
7493 if (item == NULL)
7494 return FAIL;
7495 item->di_tv.v_lock = 0;
7496 item->di_tv.v_type = VAR_LIST;
7497 item->di_tv.vval.v_list = list;
7498 if (dict_add(d, item) == FAIL)
7499 {
7500 dictitem_free(item);
7501 return FAIL;
7502 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007503 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007504 return OK;
7505}
7506
7507/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007508 * Get the number of items in a Dictionary.
7509 */
7510 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007511dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007512{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007513 if (d == NULL)
7514 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007515 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007516}
7517
7518/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007519 * Find item "key[len]" in Dictionary "d".
7520 * If "len" is negative use strlen(key).
7521 * Returns NULL when not found.
7522 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007523 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007524dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007525{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007526#define AKEYLEN 200
7527 char_u buf[AKEYLEN];
7528 char_u *akey;
7529 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007530 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007531
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007532 if (len < 0)
7533 akey = key;
7534 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007535 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007536 tofree = akey = vim_strnsave(key, len);
7537 if (akey == NULL)
7538 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007539 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007540 else
7541 {
7542 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007543 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007544 akey = buf;
7545 }
7546
Bram Moolenaar33570922005-01-25 22:26:29 +00007547 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007548 vim_free(tofree);
7549 if (HASHITEM_EMPTY(hi))
7550 return NULL;
7551 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007552}
7553
7554/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007555 * Get a string item from a dictionary.
7556 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007557 * Returns NULL if the entry doesn't exist or out of memory.
7558 */
7559 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007560get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007561{
7562 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007563 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007564
7565 di = dict_find(d, key, -1);
7566 if (di == NULL)
7567 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007568 s = get_tv_string(&di->di_tv);
7569 if (save && s != NULL)
7570 s = vim_strsave(s);
7571 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007572}
7573
7574/*
7575 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007576 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007577 */
7578 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007579get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007580{
7581 dictitem_T *di;
7582
7583 di = dict_find(d, key, -1);
7584 if (di == NULL)
7585 return 0;
7586 return get_tv_number(&di->di_tv);
7587}
7588
7589/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007590 * Return an allocated string with the string representation of a Dictionary.
7591 * May return NULL.
7592 */
7593 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007594dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007595{
7596 garray_T ga;
7597 int first = TRUE;
7598 char_u *tofree;
7599 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007600 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007602 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007603 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007604
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007605 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007606 return NULL;
7607 ga_init2(&ga, (int)sizeof(char), 80);
7608 ga_append(&ga, '{');
7609
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007610 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007611 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007612 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007613 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007614 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007615 --todo;
7616
7617 if (first)
7618 first = FALSE;
7619 else
7620 ga_concat(&ga, (char_u *)", ");
7621
7622 tofree = string_quote(hi->hi_key, FALSE);
7623 if (tofree != NULL)
7624 {
7625 ga_concat(&ga, tofree);
7626 vim_free(tofree);
7627 }
7628 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007629 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007630 if (s != NULL)
7631 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007632 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007633 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007634 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007635 line_breakcheck();
7636
Bram Moolenaar8c711452005-01-14 21:53:12 +00007637 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007638 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007639 if (todo > 0)
7640 {
7641 vim_free(ga.ga_data);
7642 return NULL;
7643 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007644
7645 ga_append(&ga, '}');
7646 ga_append(&ga, NUL);
7647 return (char_u *)ga.ga_data;
7648}
7649
7650/*
7651 * Allocate a variable for a Dictionary and fill it from "*arg".
7652 * Return OK or FAIL. Returns NOTDONE for {expr}.
7653 */
7654 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007655get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656{
Bram Moolenaar33570922005-01-25 22:26:29 +00007657 dict_T *d = NULL;
7658 typval_T tvkey;
7659 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007660 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007661 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007663 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007664
7665 /*
7666 * First check if it's not a curly-braces thing: {expr}.
7667 * Must do this without evaluating, otherwise a function may be called
7668 * twice. Unfortunately this means we need to call eval1() twice for the
7669 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007670 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007671 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007672 if (*start != '}')
7673 {
7674 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7675 return FAIL;
7676 if (*start == '}')
7677 return NOTDONE;
7678 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007679
7680 if (evaluate)
7681 {
7682 d = dict_alloc();
7683 if (d == NULL)
7684 return FAIL;
7685 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007686 tvkey.v_type = VAR_UNKNOWN;
7687 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007688
7689 *arg = skipwhite(*arg + 1);
7690 while (**arg != '}' && **arg != NUL)
7691 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007692 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007693 goto failret;
7694 if (**arg != ':')
7695 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007696 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007697 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007698 goto failret;
7699 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007700 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007701 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007702 key = get_tv_string_buf_chk(&tvkey, buf);
7703 if (key == NULL || *key == NUL)
7704 {
7705 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7706 if (key != NULL)
7707 EMSG(_(e_emptykey));
7708 clear_tv(&tvkey);
7709 goto failret;
7710 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007711 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007712
7713 *arg = skipwhite(*arg + 1);
7714 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7715 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007716 if (evaluate)
7717 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007718 goto failret;
7719 }
7720 if (evaluate)
7721 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007722 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723 if (item != NULL)
7724 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007725 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007726 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007727 clear_tv(&tv);
7728 goto failret;
7729 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007730 item = dictitem_alloc(key);
7731 clear_tv(&tvkey);
7732 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007733 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007734 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007735 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007736 if (dict_add(d, item) == FAIL)
7737 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007738 }
7739 }
7740
7741 if (**arg == '}')
7742 break;
7743 if (**arg != ',')
7744 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007745 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007746 goto failret;
7747 }
7748 *arg = skipwhite(*arg + 1);
7749 }
7750
7751 if (**arg != '}')
7752 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007753 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007754failret:
7755 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007756 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007757 return FAIL;
7758 }
7759
7760 *arg = skipwhite(*arg + 1);
7761 if (evaluate)
7762 {
7763 rettv->v_type = VAR_DICT;
7764 rettv->vval.v_dict = d;
7765 ++d->dv_refcount;
7766 }
7767
7768 return OK;
7769}
7770
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007771#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007772#endif
7773
Bram Moolenaar17a13432016-01-24 14:22:10 +01007774 static char *
7775get_var_special_name(int nr)
7776{
7777 switch (nr)
7778 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007779 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007780 case VVAL_TRUE: return "v:true";
7781 case VVAL_NONE: return "v:none";
7782 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007783 }
7784 EMSG2(_(e_intern2), "get_var_special_name()");
7785 return "42";
7786}
7787
Bram Moolenaar8c711452005-01-14 21:53:12 +00007788/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007789 * Return a string with the string representation of a variable.
7790 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007791 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007792 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007793 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007794 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007795 */
7796 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007797echo_string(
7798 typval_T *tv,
7799 char_u **tofree,
7800 char_u *numbuf,
7801 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007802{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007803 static int recurse = 0;
7804 char_u *r = NULL;
7805
Bram Moolenaar33570922005-01-25 22:26:29 +00007806 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007807 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007808 if (!did_echo_string_emsg)
7809 {
7810 /* Only give this message once for a recursive call to avoid
7811 * flooding the user with errors. And stop iterating over lists
7812 * and dicts. */
7813 did_echo_string_emsg = TRUE;
7814 EMSG(_("E724: variable nested too deep for displaying"));
7815 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007816 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007817 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007818 }
7819 ++recurse;
7820
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007821 switch (tv->v_type)
7822 {
7823 case VAR_FUNC:
7824 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007825 r = tv->vval.v_string;
7826 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007827
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007828 case VAR_PARTIAL:
7829 *tofree = NULL;
7830 /* TODO: arguments */
7831 r = tv->vval.v_partial == NULL ? NULL : tv->vval.v_partial->pt_name;
7832 break;
7833
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007834 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007835 if (tv->vval.v_list == NULL)
7836 {
7837 *tofree = NULL;
7838 r = NULL;
7839 }
7840 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7841 {
7842 *tofree = NULL;
7843 r = (char_u *)"[...]";
7844 }
7845 else
7846 {
7847 tv->vval.v_list->lv_copyID = copyID;
7848 *tofree = list2string(tv, copyID);
7849 r = *tofree;
7850 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007851 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007852
Bram Moolenaar8c711452005-01-14 21:53:12 +00007853 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007854 if (tv->vval.v_dict == NULL)
7855 {
7856 *tofree = NULL;
7857 r = NULL;
7858 }
7859 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7860 {
7861 *tofree = NULL;
7862 r = (char_u *)"{...}";
7863 }
7864 else
7865 {
7866 tv->vval.v_dict->dv_copyID = copyID;
7867 *tofree = dict2string(tv, copyID);
7868 r = *tofree;
7869 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007870 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007871
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007872 case VAR_STRING:
7873 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007874 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007875 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007876 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007877 *tofree = NULL;
7878 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007879 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007880
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007881 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007882#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007883 *tofree = NULL;
7884 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7885 r = numbuf;
7886 break;
7887#endif
7888
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007889 case VAR_SPECIAL:
7890 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007891 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007892 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007893 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007894
Bram Moolenaar8502c702014-06-17 12:51:16 +02007895 if (--recurse == 0)
7896 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007897 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007898}
7899
7900/*
7901 * Return a string with the string representation of a variable.
7902 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7903 * "numbuf" is used for a number.
7904 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007905 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007906 */
7907 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007908tv2string(
7909 typval_T *tv,
7910 char_u **tofree,
7911 char_u *numbuf,
7912 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007913{
7914 switch (tv->v_type)
7915 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007916 case VAR_FUNC:
7917 *tofree = string_quote(tv->vval.v_string, TRUE);
7918 return *tofree;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007919 case VAR_PARTIAL:
7920 *tofree = string_quote(tv->vval.v_partial == NULL ? NULL
7921 : tv->vval.v_partial->pt_name, TRUE);
7922 return *tofree;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007923 case VAR_STRING:
7924 *tofree = string_quote(tv->vval.v_string, FALSE);
7925 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007926 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01007927#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007928 *tofree = NULL;
7929 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7930 return numbuf;
7931#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007932 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007933 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007934 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007935 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007936 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007937 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007938 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007939 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007940 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007941 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007942}
7943
7944/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 * Return string "str" in ' quotes, doubling ' characters.
7946 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007947 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007948 */
7949 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007950string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007951{
Bram Moolenaar33570922005-01-25 22:26:29 +00007952 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007953 char_u *p, *r, *s;
7954
Bram Moolenaar33570922005-01-25 22:26:29 +00007955 len = (function ? 13 : 3);
7956 if (str != NULL)
7957 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007958 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007959 for (p = str; *p != NUL; mb_ptr_adv(p))
7960 if (*p == '\'')
7961 ++len;
7962 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007963 s = r = alloc(len);
7964 if (r != NULL)
7965 {
7966 if (function)
7967 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007968 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007969 r += 10;
7970 }
7971 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007972 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007973 if (str != NULL)
7974 for (p = str; *p != NUL; )
7975 {
7976 if (*p == '\'')
7977 *r++ = '\'';
7978 MB_COPY_CHAR(p, r);
7979 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007980 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007981 if (function)
7982 *r++ = ')';
7983 *r++ = NUL;
7984 }
7985 return s;
7986}
7987
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007988#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007989/*
7990 * Convert the string "text" to a floating point number.
7991 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7992 * this always uses a decimal point.
7993 * Returns the length of the text that was consumed.
7994 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007995 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007996string2float(
7997 char_u *text,
7998 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007999{
8000 char *s = (char *)text;
8001 float_T f;
8002
8003 f = strtod(s, &s);
8004 *value = f;
8005 return (int)((char_u *)s - text);
8006}
8007#endif
8008
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 * Get the value of an environment variable.
8011 * "arg" is pointing to the '$'. It is advanced to after the name.
8012 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008013 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 */
8015 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008016get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017{
8018 char_u *string = NULL;
8019 int len;
8020 int cc;
8021 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008022 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023
8024 ++*arg;
8025 name = *arg;
8026 len = get_env_len(arg);
8027 if (evaluate)
8028 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008029 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008030 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008031
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008032 cc = name[len];
8033 name[len] = NUL;
8034 /* first try vim_getenv(), fast for normal environment vars */
8035 string = vim_getenv(name, &mustfree);
8036 if (string != NULL && *string != NUL)
8037 {
8038 if (!mustfree)
8039 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008041 else
8042 {
8043 if (mustfree)
8044 vim_free(string);
8045
8046 /* next try expanding things like $VIM and ${HOME} */
8047 string = expand_env_save(name - 1);
8048 if (string != NULL && *string == '$')
8049 {
8050 vim_free(string);
8051 string = NULL;
8052 }
8053 }
8054 name[len] = cc;
8055
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008056 rettv->v_type = VAR_STRING;
8057 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 }
8059
8060 return OK;
8061}
8062
8063/*
8064 * Array with names and number of arguments of all internal functions
8065 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8066 */
8067static struct fst
8068{
8069 char *f_name; /* function name */
8070 char f_min_argc; /* minimal number of arguments */
8071 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008072 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008073 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074} functions[] =
8075{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008076#ifdef FEAT_FLOAT
8077 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008078 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008079#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008080 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008081 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008082 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 {"append", 2, 2, f_append},
8084 {"argc", 0, 0, f_argc},
8085 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008086 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008087 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008088#ifdef FEAT_FLOAT
8089 {"asin", 1, 1, f_asin}, /* WJMc */
8090#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008091 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008092 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008093 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008094 {"assert_false", 1, 2, f_assert_false},
8095 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008096#ifdef FEAT_FLOAT
8097 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008098 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008101 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 {"bufexists", 1, 1, f_bufexists},
8103 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8104 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8105 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8106 {"buflisted", 1, 1, f_buflisted},
8107 {"bufloaded", 1, 1, f_bufloaded},
8108 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008109 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 {"bufwinnr", 1, 1, f_bufwinnr},
8111 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008112 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008113 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008114 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008115#ifdef FEAT_FLOAT
8116 {"ceil", 1, 1, f_ceil},
8117#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008118#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008119 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008120 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8121 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008122 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008123 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008124 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008125 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008126 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008127 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008128 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008129 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8130 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008131 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008132 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008133#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008134 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008135 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008137 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008139#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008140 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008141 {"complete_add", 1, 1, f_complete_add},
8142 {"complete_check", 0, 0, f_complete_check},
8143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008145 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008146#ifdef FEAT_FLOAT
8147 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008148 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008149#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008150 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008152 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008153 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008154 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008156 {"diff_filler", 1, 1, f_diff_filler},
8157 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008158 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008159 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008161 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {"eventhandler", 0, 0, f_eventhandler},
8163 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008164 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008166#ifdef FEAT_FLOAT
8167 {"exp", 1, 1, f_exp},
8168#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008169 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008170 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008171 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8173 {"filereadable", 1, 1, f_filereadable},
8174 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008175 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008176 {"finddir", 1, 3, f_finddir},
8177 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008178#ifdef FEAT_FLOAT
8179 {"float2nr", 1, 1, f_float2nr},
8180 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008181 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008182#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008183 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 {"fnamemodify", 2, 2, f_fnamemodify},
8185 {"foldclosed", 1, 1, f_foldclosed},
8186 {"foldclosedend", 1, 1, f_foldclosedend},
8187 {"foldlevel", 1, 1, f_foldlevel},
8188 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008189 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008191 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008192 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008193 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008194 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008195 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 {"getchar", 0, 1, f_getchar},
8197 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008198 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {"getcmdline", 0, 0, f_getcmdline},
8200 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008201 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008202 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008203 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008204 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008205 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008206 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 {"getfsize", 1, 1, f_getfsize},
8208 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008209 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008210 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008211 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008212 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008213 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008214 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008215 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008216 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008218 {"gettabvar", 2, 3, f_gettabvar},
8219 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 {"getwinposx", 0, 0, f_getwinposx},
8221 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008222 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008223 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008224 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008225 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008227 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008228 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008229 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8231 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8232 {"histadd", 2, 2, f_histadd},
8233 {"histdel", 1, 2, f_histdel},
8234 {"histget", 1, 2, f_histget},
8235 {"histnr", 1, 1, f_histnr},
8236 {"hlID", 1, 1, f_hlID},
8237 {"hlexists", 1, 1, f_hlexists},
8238 {"hostname", 0, 0, f_hostname},
8239 {"iconv", 3, 3, f_iconv},
8240 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008241 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008242 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008244 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 {"inputrestore", 0, 0, f_inputrestore},
8246 {"inputsave", 0, 0, f_inputsave},
8247 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008248 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008249 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008251 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008252#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8253 {"isnan", 1, 1, f_isnan},
8254#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008255 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008256#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008257 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008258 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008259 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008260 {"job_start", 1, 2, f_job_start},
8261 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008262 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008263#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008264 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008265 {"js_decode", 1, 1, f_js_decode},
8266 {"js_encode", 1, 1, f_js_encode},
8267 {"json_decode", 1, 1, f_json_decode},
8268 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008269 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008271 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 {"libcall", 3, 3, f_libcall},
8273 {"libcallnr", 3, 3, f_libcallnr},
8274 {"line", 1, 1, f_line},
8275 {"line2byte", 1, 1, f_line2byte},
8276 {"lispindent", 1, 1, f_lispindent},
8277 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008278#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008279 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008280 {"log10", 1, 1, f_log10},
8281#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008282#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008283 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008284#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008285 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008286 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008287 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008288 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008289 {"matchadd", 2, 5, f_matchadd},
8290 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008291 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008292 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008293 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008294 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008295 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008296 {"max", 1, 1, f_max},
8297 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008298#ifdef vim_mkdir
8299 {"mkdir", 1, 3, f_mkdir},
8300#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008301 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008302#ifdef FEAT_MZSCHEME
8303 {"mzeval", 1, 1, f_mzeval},
8304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008306 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008307 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008308 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008309#ifdef FEAT_PERL
8310 {"perleval", 1, 1, f_perleval},
8311#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008312#ifdef FEAT_FLOAT
8313 {"pow", 2, 2, f_pow},
8314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008316 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008317 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008318#ifdef FEAT_PYTHON3
8319 {"py3eval", 1, 1, f_py3eval},
8320#endif
8321#ifdef FEAT_PYTHON
8322 {"pyeval", 1, 1, f_pyeval},
8323#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008324 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008325 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008326 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008327#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008328 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008329#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008330 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 {"remote_expr", 2, 3, f_remote_expr},
8332 {"remote_foreground", 1, 1, f_remote_foreground},
8333 {"remote_peek", 1, 2, f_remote_peek},
8334 {"remote_read", 1, 1, f_remote_read},
8335 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008336 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008338 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008340 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008341#ifdef FEAT_FLOAT
8342 {"round", 1, 1, f_round},
8343#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008344 {"screenattr", 2, 2, f_screenattr},
8345 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008346 {"screencol", 0, 0, f_screencol},
8347 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008348 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008349 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008350 {"searchpair", 3, 7, f_searchpair},
8351 {"searchpairpos", 3, 7, f_searchpairpos},
8352 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 {"server2client", 2, 2, f_server2client},
8354 {"serverlist", 0, 0, f_serverlist},
8355 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008356 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008358 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008360 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008361 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008362 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008363 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008365 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008366 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008368#ifdef FEAT_CRYPT
8369 {"sha256", 1, 1, f_sha256},
8370#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008371 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008372 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008374#ifdef FEAT_FLOAT
8375 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008376 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008377#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008378 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008379 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008380 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008381 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008382 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008383#ifdef FEAT_FLOAT
8384 {"sqrt", 1, 1, f_sqrt},
8385 {"str2float", 1, 1, f_str2float},
8386#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008387 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008388 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008389 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390#ifdef HAVE_STRFTIME
8391 {"strftime", 1, 2, f_strftime},
8392#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008393 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008394 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 {"strlen", 1, 1, f_strlen},
8396 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008397 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008399 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008400 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 {"substitute", 4, 4, f_substitute},
8402 {"synID", 3, 3, f_synID},
8403 {"synIDattr", 2, 3, f_synIDattr},
8404 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008405 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008406 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008407 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008408 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008409 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008410 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008411 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008412 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008413 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008414#ifdef FEAT_FLOAT
8415 {"tan", 1, 1, f_tan},
8416 {"tanh", 1, 1, f_tanh},
8417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008419 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008420#ifdef FEAT_TIMERS
8421 {"timer_start", 2, 3, f_timer_start},
8422 {"timer_stop", 1, 1, f_timer_stop},
8423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 {"tolower", 1, 1, f_tolower},
8425 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008426 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008427#ifdef FEAT_FLOAT
8428 {"trunc", 1, 1, f_trunc},
8429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008431 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008432 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008433 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008434 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 {"virtcol", 1, 1, f_virtcol},
8436 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008437 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008438 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008439 {"win_getid", 0, 2, f_win_getid},
8440 {"win_gotoid", 1, 1, f_win_gotoid},
8441 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8442 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 {"winbufnr", 1, 1, f_winbufnr},
8444 {"wincol", 0, 0, f_wincol},
8445 {"winheight", 1, 1, f_winheight},
8446 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008447 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008449 {"winrestview", 1, 1, f_winrestview},
8450 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008452 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008453 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008454 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455};
8456
8457#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8458
8459/*
8460 * Function given to ExpandGeneric() to obtain the list of internal
8461 * or user defined function names.
8462 */
8463 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008464get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465{
8466 static int intidx = -1;
8467 char_u *name;
8468
8469 if (idx == 0)
8470 intidx = -1;
8471 if (intidx < 0)
8472 {
8473 name = get_user_func_name(xp, idx);
8474 if (name != NULL)
8475 return name;
8476 }
8477 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8478 {
8479 STRCPY(IObuff, functions[intidx].f_name);
8480 STRCAT(IObuff, "(");
8481 if (functions[intidx].f_max_argc == 0)
8482 STRCAT(IObuff, ")");
8483 return IObuff;
8484 }
8485
8486 return NULL;
8487}
8488
8489/*
8490 * Function given to ExpandGeneric() to obtain the list of internal or
8491 * user defined variable or function names.
8492 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008494get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495{
8496 static int intidx = -1;
8497 char_u *name;
8498
8499 if (idx == 0)
8500 intidx = -1;
8501 if (intidx < 0)
8502 {
8503 name = get_function_name(xp, idx);
8504 if (name != NULL)
8505 return name;
8506 }
8507 return get_user_var_name(xp, ++intidx);
8508}
8509
8510#endif /* FEAT_CMDL_COMPL */
8511
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008512#if defined(EBCDIC) || defined(PROTO)
8513/*
8514 * Compare struct fst by function name.
8515 */
8516 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008517compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008518{
8519 struct fst *p1 = (struct fst *)s1;
8520 struct fst *p2 = (struct fst *)s2;
8521
8522 return STRCMP(p1->f_name, p2->f_name);
8523}
8524
8525/*
8526 * Sort the function table by function name.
8527 * The sorting of the table above is ASCII dependant.
8528 * On machines using EBCDIC we have to sort it.
8529 */
8530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008531sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008532{
8533 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8534
8535 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8536}
8537#endif
8538
8539
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540/*
8541 * Find internal function in table above.
8542 * Return index, or -1 if not found
8543 */
8544 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008545find_internal_func(
8546 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547{
8548 int first = 0;
8549 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8550 int cmp;
8551 int x;
8552
8553 /*
8554 * Find the function name in the table. Binary search.
8555 */
8556 while (first <= last)
8557 {
8558 x = first + ((unsigned)(last - first) >> 1);
8559 cmp = STRCMP(name, functions[x].f_name);
8560 if (cmp < 0)
8561 last = x - 1;
8562 else if (cmp > 0)
8563 first = x + 1;
8564 else
8565 return x;
8566 }
8567 return -1;
8568}
8569
8570/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008571 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8572 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008573 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8574 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008575 */
8576 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008577deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008578{
Bram Moolenaar33570922005-01-25 22:26:29 +00008579 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008580 int cc;
8581
Bram Moolenaar65639032016-03-16 21:40:30 +01008582 if (partialp != NULL)
8583 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008584
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008585 cc = name[*lenp];
8586 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008587 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008588 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008589 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008590 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008591 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008592 {
8593 *lenp = 0;
8594 return (char_u *)""; /* just in case */
8595 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008596 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008597 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008598 }
8599
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008600 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8601 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008602 partial_T *pt = v->di_tv.vval.v_partial;
8603
8604 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008605 {
8606 *lenp = 0;
8607 return (char_u *)""; /* just in case */
8608 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008609 if (partialp != NULL)
8610 *partialp = pt;
8611 *lenp = (int)STRLEN(pt->pt_name);
8612 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008613 }
8614
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008615 return name;
8616}
8617
8618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 * Allocate a variable for the result of a function.
8620 * Return OK or FAIL.
8621 */
8622 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008623get_func_tv(
8624 char_u *name, /* name of the function */
8625 int len, /* length of "name" */
8626 typval_T *rettv,
8627 char_u **arg, /* argument, pointing to the '(' */
8628 linenr_T firstline, /* first line of range */
8629 linenr_T lastline, /* last line of range */
8630 int *doesrange, /* return: function handled range */
8631 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008632 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008633 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634{
8635 char_u *argp;
8636 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008637 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 int argcount = 0; /* number of arguments found */
8639
8640 /*
8641 * Get the arguments.
8642 */
8643 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008644 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 {
8646 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8647 if (*argp == ')' || *argp == ',' || *argp == NUL)
8648 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8650 {
8651 ret = FAIL;
8652 break;
8653 }
8654 ++argcount;
8655 if (*argp != ',')
8656 break;
8657 }
8658 if (*argp == ')')
8659 ++argp;
8660 else
8661 ret = FAIL;
8662
8663 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008664 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008665 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008667 {
8668 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008669 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008670 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008671 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673
8674 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008675 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676
8677 *arg = skipwhite(argp);
8678 return ret;
8679}
8680
8681
8682/*
8683 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008684 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008685 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008687 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008688call_func(
8689 char_u *funcname, /* name of the function */
8690 int len, /* length of "name" */
8691 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008692 int argcount_in, /* number of "argvars" */
8693 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008694 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008695 linenr_T firstline, /* first line of range */
8696 linenr_T lastline, /* last line of range */
8697 int *doesrange, /* return: function handled range */
8698 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008699 partial_T *partial, /* optional, can be NULL */
8700 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701{
8702 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703#define ERROR_UNKNOWN 0
8704#define ERROR_TOOMANY 1
8705#define ERROR_TOOFEW 2
8706#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008707#define ERROR_DICT 4
8708#define ERROR_NONE 5
8709#define ERROR_OTHER 6
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008710#define ERROR_BOTH 7
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 int error = ERROR_NONE;
8712 int i;
8713 int llen;
8714 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715#define FLEN_FIXED 40
8716 char_u fname_buf[FLEN_FIXED + 1];
8717 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008718 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008719 int argcount = argcount_in;
8720 typval_T *argvars = argvars_in;
8721 dict_T *selfdict = selfdict_in;
8722 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
8723 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008724
8725 /* Make a copy of the name, if it comes from a funcref variable it could
8726 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008727 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008728 if (name == NULL)
8729 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730
8731 /*
8732 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8733 * Change <SNR>123_name() to K_SNR 123_name().
8734 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 llen = eval_fname_script(name);
8737 if (llen > 0)
8738 {
8739 fname_buf[0] = K_SPECIAL;
8740 fname_buf[1] = KS_EXTRA;
8741 fname_buf[2] = (int)KE_SNR;
8742 i = 3;
8743 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8744 {
8745 if (current_SID <= 0)
8746 error = ERROR_SCRIPT;
8747 else
8748 {
8749 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8750 i = (int)STRLEN(fname_buf);
8751 }
8752 }
8753 if (i + STRLEN(name + llen) < FLEN_FIXED)
8754 {
8755 STRCPY(fname_buf + i, name + llen);
8756 fname = fname_buf;
8757 }
8758 else
8759 {
8760 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8761 if (fname == NULL)
8762 error = ERROR_OTHER;
8763 else
8764 {
8765 mch_memmove(fname, fname_buf, (size_t)i);
8766 STRCPY(fname + i, name + llen);
8767 }
8768 }
8769 }
8770 else
8771 fname = name;
8772
8773 *doesrange = FALSE;
8774
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008775 if (partial != NULL)
8776 {
8777 if (partial->pt_dict != NULL)
8778 {
8779 if (selfdict_in != NULL)
8780 error = ERROR_BOTH;
8781 selfdict = partial->pt_dict;
8782 }
8783 if (error == ERROR_NONE && partial->pt_argc > 0)
8784 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008785 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
8786 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
8787 for (i = 0; i < argcount_in; ++i)
8788 argv[i + argv_clear] = argvars_in[i];
8789 argvars = argv;
8790 argcount = partial->pt_argc + argcount_in;
8791 }
8792 }
8793
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794
8795 /* execute the function if no errors detected and executing */
8796 if (evaluate && error == ERROR_NONE)
8797 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008798 char_u *rfname = fname;
8799
8800 /* Ignore "g:" before a function name. */
8801 if (fname[0] == 'g' && fname[1] == ':')
8802 rfname = fname + 2;
8803
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008804 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8805 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 error = ERROR_UNKNOWN;
8807
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008808 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 {
8810 /*
8811 * User defined function.
8812 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008813 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008814
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008816 /* Trigger FuncUndefined event, may load the function. */
8817 if (fp == NULL
8818 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008819 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008820 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008822 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008823 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 }
8825#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008826 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008827 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008828 {
8829 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008830 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008831 }
8832
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 if (fp != NULL)
8834 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008835 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008837 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008839 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008841 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008842 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 else
8844 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008845 int did_save_redo = FALSE;
8846
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 /*
8848 * Call the user function.
8849 * Save and restore search patterns, script variables and
8850 * redo buffer.
8851 */
8852 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008853#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008854 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008855#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008856 {
8857 saveRedobuff();
8858 did_save_redo = TRUE;
8859 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008860 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008861 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008862 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008863 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8864 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8865 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008866 /* Function was unreferenced while being used, free it
8867 * now. */
8868 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008869 if (did_save_redo)
8870 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 restore_search_patterns();
8872 error = ERROR_NONE;
8873 }
8874 }
8875 }
8876 else
8877 {
8878 /*
8879 * Find the function name in the table, call its implementation.
8880 */
8881 i = find_internal_func(fname);
8882 if (i >= 0)
8883 {
8884 if (argcount < functions[i].f_min_argc)
8885 error = ERROR_TOOFEW;
8886 else if (argcount > functions[i].f_max_argc)
8887 error = ERROR_TOOMANY;
8888 else
8889 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008890 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008891 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 error = ERROR_NONE;
8893 }
8894 }
8895 }
8896 /*
8897 * The function call (or "FuncUndefined" autocommand sequence) might
8898 * have been aborted by an error, an interrupt, or an explicitly thrown
8899 * exception that has not been caught so far. This situation can be
8900 * tested for by calling aborting(). For an error in an internal
8901 * function or for the "E132" error in call_user_func(), however, the
8902 * throw point at which the "force_abort" flag (temporarily reset by
8903 * emsg()) is normally updated has not been reached yet. We need to
8904 * update that flag first to make aborting() reliable.
8905 */
8906 update_force_abort();
8907 }
8908 if (error == ERROR_NONE)
8909 ret = OK;
8910
8911 /*
8912 * Report an error unless the argument evaluation or function call has been
8913 * cancelled due to an aborting error, an interrupt, or an exception.
8914 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008915 if (!aborting())
8916 {
8917 switch (error)
8918 {
8919 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008920 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008921 break;
8922 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008923 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008924 break;
8925 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008926 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008927 name);
8928 break;
8929 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008930 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008931 name);
8932 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008933 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008934 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008935 name);
8936 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008937 case ERROR_BOTH:
Bram Moolenaarab1fa392016-03-15 19:33:34 +01008938 emsg_funcname(e_dict_both, name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008939 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008940 }
8941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008943 while (argv_clear > 0)
8944 clear_tv(&argv[--argv_clear]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 if (fname != name && fname != fname_buf)
8946 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008947 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948
8949 return ret;
8950}
8951
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008952/*
8953 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008954 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008955 */
8956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008957emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008958{
8959 char_u *p;
8960
8961 if (*name == K_SPECIAL)
8962 p = concat_str((char_u *)"<SNR>", name + 3);
8963 else
8964 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008965 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008966 if (p != name)
8967 vim_free(p);
8968}
8969
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008970/*
8971 * Return TRUE for a non-zero Number and a non-empty String.
8972 */
8973 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008974non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008975{
8976 return ((argvars[0].v_type == VAR_NUMBER
8977 && argvars[0].vval.v_number != 0)
8978 || (argvars[0].v_type == VAR_STRING
8979 && argvars[0].vval.v_string != NULL
8980 && *argvars[0].vval.v_string != NUL));
8981}
8982
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983/*********************************************
8984 * Implementation of the built-in functions
8985 */
8986
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008987#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008988static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008989
8990/*
8991 * Get the float value of "argvars[0]" into "f".
8992 * Returns FAIL when the argument is not a Number or Float.
8993 */
8994 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008995get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008996{
8997 if (argvars[0].v_type == VAR_FLOAT)
8998 {
8999 *f = argvars[0].vval.v_float;
9000 return OK;
9001 }
9002 if (argvars[0].v_type == VAR_NUMBER)
9003 {
9004 *f = (float_T)argvars[0].vval.v_number;
9005 return OK;
9006 }
9007 EMSG(_("E808: Number or Float required"));
9008 return FAIL;
9009}
9010
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009011/*
9012 * "abs(expr)" function
9013 */
9014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009015f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009016{
9017 if (argvars[0].v_type == VAR_FLOAT)
9018 {
9019 rettv->v_type = VAR_FLOAT;
9020 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9021 }
9022 else
9023 {
9024 varnumber_T n;
9025 int error = FALSE;
9026
9027 n = get_tv_number_chk(&argvars[0], &error);
9028 if (error)
9029 rettv->vval.v_number = -1;
9030 else if (n > 0)
9031 rettv->vval.v_number = n;
9032 else
9033 rettv->vval.v_number = -n;
9034 }
9035}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009036
9037/*
9038 * "acos()" function
9039 */
9040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009041f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009042{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009043 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009044
9045 rettv->v_type = VAR_FLOAT;
9046 if (get_float_arg(argvars, &f) == OK)
9047 rettv->vval.v_float = acos(f);
9048 else
9049 rettv->vval.v_float = 0.0;
9050}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009051#endif
9052
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009054 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 */
9056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009057f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058{
Bram Moolenaar33570922005-01-25 22:26:29 +00009059 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009061 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009062 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009064 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009065 && !tv_check_lock(l->lv_lock,
9066 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009067 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009068 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009069 }
9070 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009071 EMSG(_(e_listreq));
9072}
9073
9074/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009075 * "alloc_fail(id, countdown, repeat)" function
9076 */
9077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009078f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009079{
9080 if (argvars[0].v_type != VAR_NUMBER
9081 || argvars[0].vval.v_number <= 0
9082 || argvars[1].v_type != VAR_NUMBER
9083 || argvars[1].vval.v_number < 0
9084 || argvars[2].v_type != VAR_NUMBER)
9085 EMSG(_(e_invarg));
9086 else
9087 {
9088 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009089 if (alloc_fail_id >= aid_last)
9090 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009091 alloc_fail_countdown = argvars[1].vval.v_number;
9092 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009093 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009094 }
9095}
9096
9097/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009098 * "and(expr, expr)" function
9099 */
9100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009101f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009102{
9103 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9104 & get_tv_number_chk(&argvars[1], NULL);
9105}
9106
9107/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009108 * "append(lnum, string/list)" function
9109 */
9110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009111f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009112{
9113 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009114 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009115 list_T *l = NULL;
9116 listitem_T *li = NULL;
9117 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009118 long added = 0;
9119
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009120 /* When coming here from Insert mode, sync undo, so that this can be
9121 * undone separately from what was previously inserted. */
9122 if (u_sync_once == 2)
9123 {
9124 u_sync_once = 1; /* notify that u_sync() was called */
9125 u_sync(TRUE);
9126 }
9127
Bram Moolenaar0d660222005-01-07 21:51:51 +00009128 lnum = get_tv_lnum(argvars);
9129 if (lnum >= 0
9130 && lnum <= curbuf->b_ml.ml_line_count
9131 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009132 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009133 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009134 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009135 l = argvars[1].vval.v_list;
9136 if (l == NULL)
9137 return;
9138 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009139 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009140 for (;;)
9141 {
9142 if (l == NULL)
9143 tv = &argvars[1]; /* append a string */
9144 else if (li == NULL)
9145 break; /* end of list */
9146 else
9147 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009148 line = get_tv_string_chk(tv);
9149 if (line == NULL) /* type error */
9150 {
9151 rettv->vval.v_number = 1; /* Failed */
9152 break;
9153 }
9154 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009155 ++added;
9156 if (l == NULL)
9157 break;
9158 li = li->li_next;
9159 }
9160
9161 appended_lines_mark(lnum, added);
9162 if (curwin->w_cursor.lnum > lnum)
9163 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009165 else
9166 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167}
9168
9169/*
9170 * "argc()" function
9171 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009173f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009175 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176}
9177
9178/*
9179 * "argidx()" function
9180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009182f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009184 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185}
9186
9187/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009188 * "arglistid()" function
9189 */
9190 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009191f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009192{
9193 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009194
9195 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009196 wp = find_tabwin(&argvars[0], &argvars[1]);
9197 if (wp != NULL)
9198 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009199}
9200
9201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 * "argv(nr)" function
9203 */
9204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009205f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206{
9207 int idx;
9208
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009209 if (argvars[0].v_type != VAR_UNKNOWN)
9210 {
9211 idx = get_tv_number_chk(&argvars[0], NULL);
9212 if (idx >= 0 && idx < ARGCOUNT)
9213 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9214 else
9215 rettv->vval.v_string = NULL;
9216 rettv->v_type = VAR_STRING;
9217 }
9218 else if (rettv_list_alloc(rettv) == OK)
9219 for (idx = 0; idx < ARGCOUNT; ++idx)
9220 list_append_string(rettv->vval.v_list,
9221 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222}
9223
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009224static void prepare_assert_error(garray_T*gap);
9225static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9226static void assert_error(garray_T *gap);
9227static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009228
9229/*
9230 * Prepare "gap" for an assert error and add the sourcing position.
9231 */
9232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009233prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009234{
9235 char buf[NUMBUFLEN];
9236
9237 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009238 if (sourcing_name != NULL)
9239 {
9240 ga_concat(gap, sourcing_name);
9241 if (sourcing_lnum > 0)
9242 ga_concat(gap, (char_u *)" ");
9243 }
9244 if (sourcing_lnum > 0)
9245 {
9246 sprintf(buf, "line %ld", (long)sourcing_lnum);
9247 ga_concat(gap, (char_u *)buf);
9248 }
9249 if (sourcing_name != NULL || sourcing_lnum > 0)
9250 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009251}
9252
9253/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009254 * Append "str" to "gap", escaping unprintable characters.
9255 * Changes NL to \n, CR to \r, etc.
9256 */
9257 static void
9258ga_concat_esc(garray_T *gap, char_u *str)
9259{
9260 char_u *p;
9261 char_u buf[NUMBUFLEN];
9262
Bram Moolenaarf1551962016-03-15 12:55:58 +01009263 if (str == NULL)
9264 {
9265 ga_concat(gap, (char_u *)"NULL");
9266 return;
9267 }
9268
Bram Moolenaar23689172016-02-15 22:37:37 +01009269 for (p = str; *p != NUL; ++p)
9270 switch (*p)
9271 {
9272 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9273 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9274 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9275 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9276 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9277 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9278 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9279 default:
9280 if (*p < ' ')
9281 {
9282 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9283 ga_concat(gap, buf);
9284 }
9285 else
9286 ga_append(gap, *p);
9287 break;
9288 }
9289}
9290
9291/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009292 * Fill "gap" with information about an assert error.
9293 */
9294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009295fill_assert_error(
9296 garray_T *gap,
9297 typval_T *opt_msg_tv,
9298 char_u *exp_str,
9299 typval_T *exp_tv,
9300 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009301{
9302 char_u numbuf[NUMBUFLEN];
9303 char_u *tofree;
9304
9305 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9306 {
9307 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9308 vim_free(tofree);
9309 }
9310 else
9311 {
9312 ga_concat(gap, (char_u *)"Expected ");
9313 if (exp_str == NULL)
9314 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009315 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009316 vim_free(tofree);
9317 }
9318 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009319 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009320 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009321 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009322 vim_free(tofree);
9323 }
9324}
Bram Moolenaar43345542015-11-29 17:35:35 +01009325
9326/*
9327 * Add an assert error to v:errors.
9328 */
9329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009330assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009331{
9332 struct vimvar *vp = &vimvars[VV_ERRORS];
9333
9334 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9335 /* Make sure v:errors is a list. */
9336 set_vim_var_list(VV_ERRORS, list_alloc());
9337 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9338}
9339
Bram Moolenaar43345542015-11-29 17:35:35 +01009340/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009341 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009342 */
9343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009344f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009345{
9346 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009347
9348 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9349 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009350 prepare_assert_error(&ga);
9351 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9352 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009353 ga_clear(&ga);
9354 }
9355}
9356
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009357/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009358 * "assert_exception(string[, msg])" function
9359 */
9360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009361f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009362{
9363 garray_T ga;
9364 char *error;
9365
9366 error = (char *)get_tv_string_chk(&argvars[0]);
9367 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9368 {
9369 prepare_assert_error(&ga);
9370 ga_concat(&ga, (char_u *)"v:exception is not set");
9371 assert_error(&ga);
9372 ga_clear(&ga);
9373 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009374 else if (error != NULL
9375 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009376 {
9377 prepare_assert_error(&ga);
9378 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9379 &vimvars[VV_EXCEPTION].vv_tv);
9380 assert_error(&ga);
9381 ga_clear(&ga);
9382 }
9383}
9384
9385/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009386 * "assert_fails(cmd [, error])" function
9387 */
9388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009389f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009390{
9391 char_u *cmd = get_tv_string_chk(&argvars[0]);
9392 garray_T ga;
9393
9394 called_emsg = FALSE;
9395 suppress_errthrow = TRUE;
9396 emsg_silent = TRUE;
9397 do_cmdline_cmd(cmd);
9398 if (!called_emsg)
9399 {
9400 prepare_assert_error(&ga);
9401 ga_concat(&ga, (char_u *)"command did not fail: ");
9402 ga_concat(&ga, cmd);
9403 assert_error(&ga);
9404 ga_clear(&ga);
9405 }
9406 else if (argvars[1].v_type != VAR_UNKNOWN)
9407 {
9408 char_u buf[NUMBUFLEN];
9409 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9410
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009411 if (error == NULL
9412 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009413 {
9414 prepare_assert_error(&ga);
9415 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9416 &vimvars[VV_ERRMSG].vv_tv);
9417 assert_error(&ga);
9418 ga_clear(&ga);
9419 }
9420 }
9421
9422 called_emsg = FALSE;
9423 suppress_errthrow = FALSE;
9424 emsg_silent = FALSE;
9425 emsg_on_display = FALSE;
9426 set_vim_var_string(VV_ERRMSG, NULL, 0);
9427}
9428
9429/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009430 * Common for assert_true() and assert_false().
9431 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009433assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009434{
9435 int error = FALSE;
9436 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009437
Bram Moolenaar37127922016-02-06 20:29:28 +01009438 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009439 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009440 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009441 if (argvars[0].v_type != VAR_NUMBER
9442 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9443 || error)
9444 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009445 prepare_assert_error(&ga);
9446 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009447 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009448 NULL, &argvars[0]);
9449 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009450 ga_clear(&ga);
9451 }
9452}
9453
9454/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009455 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009456 */
9457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009458f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009459{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009460 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009461}
9462
9463/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009464 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009465 */
9466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009467f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009468{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009469 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009470}
9471
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009472#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009473/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009474 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009475 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009477f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009478{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009479 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009480
9481 rettv->v_type = VAR_FLOAT;
9482 if (get_float_arg(argvars, &f) == OK)
9483 rettv->vval.v_float = asin(f);
9484 else
9485 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009486}
9487
9488/*
9489 * "atan()" function
9490 */
9491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009492f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009493{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009494 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009495
9496 rettv->v_type = VAR_FLOAT;
9497 if (get_float_arg(argvars, &f) == OK)
9498 rettv->vval.v_float = atan(f);
9499 else
9500 rettv->vval.v_float = 0.0;
9501}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009502
9503/*
9504 * "atan2()" function
9505 */
9506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009507f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009508{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009509 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009510
9511 rettv->v_type = VAR_FLOAT;
9512 if (get_float_arg(argvars, &fx) == OK
9513 && get_float_arg(&argvars[1], &fy) == OK)
9514 rettv->vval.v_float = atan2(fx, fy);
9515 else
9516 rettv->vval.v_float = 0.0;
9517}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009518#endif
9519
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520/*
9521 * "browse(save, title, initdir, default)" function
9522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009524f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525{
9526#ifdef FEAT_BROWSE
9527 int save;
9528 char_u *title;
9529 char_u *initdir;
9530 char_u *defname;
9531 char_u buf[NUMBUFLEN];
9532 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009533 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009535 save = get_tv_number_chk(&argvars[0], &error);
9536 title = get_tv_string_chk(&argvars[1]);
9537 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9538 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009540 if (error || title == NULL || initdir == NULL || defname == NULL)
9541 rettv->vval.v_string = NULL;
9542 else
9543 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009544 do_browse(save ? BROWSE_SAVE : 0,
9545 title, defname, NULL, initdir, NULL, curbuf);
9546#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009548#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009549 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009550}
9551
9552/*
9553 * "browsedir(title, initdir)" function
9554 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009556f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009557{
9558#ifdef FEAT_BROWSE
9559 char_u *title;
9560 char_u *initdir;
9561 char_u buf[NUMBUFLEN];
9562
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009563 title = get_tv_string_chk(&argvars[0]);
9564 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009565
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009566 if (title == NULL || initdir == NULL)
9567 rettv->vval.v_string = NULL;
9568 else
9569 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009570 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009572 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009574 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575}
9576
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009577static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009578
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579/*
9580 * Find a buffer by number or exact name.
9581 */
9582 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009583find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584{
9585 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009587 if (avar->v_type == VAR_NUMBER)
9588 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009589 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009591 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009592 if (buf == NULL)
9593 {
9594 /* No full path name match, try a match with a URL or a "nofile"
9595 * buffer, these don't use the full path. */
9596 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9597 if (buf->b_fname != NULL
9598 && (path_with_url(buf->b_fname)
9599#ifdef FEAT_QUICKFIX
9600 || bt_nofile(buf)
9601#endif
9602 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009603 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009604 break;
9605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 }
9607 return buf;
9608}
9609
9610/*
9611 * "bufexists(expr)" function
9612 */
9613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009614f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009616 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617}
9618
9619/*
9620 * "buflisted(expr)" function
9621 */
9622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009623f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624{
9625 buf_T *buf;
9626
9627 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009628 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629}
9630
9631/*
9632 * "bufloaded(expr)" function
9633 */
9634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009635f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636{
9637 buf_T *buf;
9638
9639 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009640 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641}
9642
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009643 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009644buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 int save_magic;
9647 char_u *save_cpo;
9648 buf_T *buf;
9649
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9651 save_magic = p_magic;
9652 p_magic = TRUE;
9653 save_cpo = p_cpo;
9654 p_cpo = (char_u *)"";
9655
9656 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009657 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658
9659 p_magic = save_magic;
9660 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009661 return buf;
9662}
9663
9664/*
9665 * Get buffer by number or pattern.
9666 */
9667 static buf_T *
9668get_buf_tv(typval_T *tv, int curtab_only)
9669{
9670 char_u *name = tv->vval.v_string;
9671 buf_T *buf;
9672
9673 if (tv->v_type == VAR_NUMBER)
9674 return buflist_findnr((int)tv->vval.v_number);
9675 if (tv->v_type != VAR_STRING)
9676 return NULL;
9677 if (name == NULL || *name == NUL)
9678 return curbuf;
9679 if (name[0] == '$' && name[1] == NUL)
9680 return lastbuf;
9681
9682 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683
9684 /* If not found, try expanding the name, like done for bufexists(). */
9685 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009686 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687
9688 return buf;
9689}
9690
9691/*
9692 * "bufname(expr)" function
9693 */
9694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009695f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696{
9697 buf_T *buf;
9698
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009699 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009701 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009702 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009704 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009706 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 --emsg_off;
9708}
9709
9710/*
9711 * "bufnr(expr)" function
9712 */
9713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009714f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715{
9716 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009717 int error = FALSE;
9718 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009720 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009722 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009723 --emsg_off;
9724
9725 /* If the buffer isn't found and the second argument is not zero create a
9726 * new buffer. */
9727 if (buf == NULL
9728 && argvars[1].v_type != VAR_UNKNOWN
9729 && get_tv_number_chk(&argvars[1], &error) != 0
9730 && !error
9731 && (name = get_tv_string_chk(&argvars[0])) != NULL
9732 && !error)
9733 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9734
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009736 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739}
9740
9741/*
9742 * "bufwinnr(nr)" function
9743 */
9744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009745f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746{
9747#ifdef FEAT_WINDOWS
9748 win_T *wp;
9749 int winnr = 0;
9750#endif
9751 buf_T *buf;
9752
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009753 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009755 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756#ifdef FEAT_WINDOWS
9757 for (wp = firstwin; wp; wp = wp->w_next)
9758 {
9759 ++winnr;
9760 if (wp->w_buffer == buf)
9761 break;
9762 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009763 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009765 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766#endif
9767 --emsg_off;
9768}
9769
9770/*
9771 * "byte2line(byte)" function
9772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009774f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775{
9776#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009777 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778#else
9779 long boff = 0;
9780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009781 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009783 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009785 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 (linenr_T)0, &boff);
9787#endif
9788}
9789
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009791byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009792{
9793#ifdef FEAT_MBYTE
9794 char_u *t;
9795#endif
9796 char_u *str;
9797 long idx;
9798
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009799 str = get_tv_string_chk(&argvars[0]);
9800 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009801 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009802 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009803 return;
9804
9805#ifdef FEAT_MBYTE
9806 t = str;
9807 for ( ; idx > 0; idx--)
9808 {
9809 if (*t == NUL) /* EOL reached */
9810 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009811 if (enc_utf8 && comp)
9812 t += utf_ptr2len(t);
9813 else
9814 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009815 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009816 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009817#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009818 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009819 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009820#endif
9821}
9822
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009823/*
9824 * "byteidx()" function
9825 */
9826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009827f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009828{
9829 byteidx(argvars, rettv, FALSE);
9830}
9831
9832/*
9833 * "byteidxcomp()" function
9834 */
9835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009836f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009837{
9838 byteidx(argvars, rettv, TRUE);
9839}
9840
Bram Moolenaardb913952012-06-29 12:54:53 +02009841 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009842func_call(
9843 char_u *name,
9844 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009845 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009846 dict_T *selfdict,
9847 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009848{
9849 listitem_T *item;
9850 typval_T argv[MAX_FUNC_ARGS + 1];
9851 int argc = 0;
9852 int dummy;
9853 int r = 0;
9854
9855 for (item = args->vval.v_list->lv_first; item != NULL;
9856 item = item->li_next)
9857 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009858 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +02009859 {
9860 EMSG(_("E699: Too many arguments"));
9861 break;
9862 }
9863 /* Make a copy of each argument. This is needed to be able to set
9864 * v_lock to VAR_FIXED in the copy without changing the original list.
9865 */
9866 copy_tv(&item->li_tv, &argv[argc++]);
9867 }
9868
9869 if (item == NULL)
9870 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9871 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009872 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +02009873
9874 /* Free the arguments. */
9875 while (argc > 0)
9876 clear_tv(&argv[--argc]);
9877
9878 return r;
9879}
9880
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009881/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009882 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009883 */
9884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009885f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009886{
9887 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009888 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00009889 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009890
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009891 if (argvars[1].v_type != VAR_LIST)
9892 {
9893 EMSG(_(e_listreq));
9894 return;
9895 }
9896 if (argvars[1].vval.v_list == NULL)
9897 return;
9898
9899 if (argvars[0].v_type == VAR_FUNC)
9900 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009901 else if (argvars[0].v_type == VAR_PARTIAL)
9902 {
9903 partial = argvars[0].vval.v_partial;
9904 func = partial->pt_name;
9905 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009906 else
9907 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009908 if (*func == NUL)
9909 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009910
Bram Moolenaare9a41262005-01-15 22:18:47 +00009911 if (argvars[2].v_type != VAR_UNKNOWN)
9912 {
9913 if (argvars[2].v_type != VAR_DICT)
9914 {
9915 EMSG(_(e_dictreq));
9916 return;
9917 }
9918 selfdict = argvars[2].vval.v_dict;
9919 }
9920
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009921 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009922}
9923
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009924#ifdef FEAT_FLOAT
9925/*
9926 * "ceil({float})" function
9927 */
9928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009929f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009930{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009931 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009932
9933 rettv->v_type = VAR_FLOAT;
9934 if (get_float_arg(argvars, &f) == OK)
9935 rettv->vval.v_float = ceil(f);
9936 else
9937 rettv->vval.v_float = 0.0;
9938}
9939#endif
9940
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01009941#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009942/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009943 * "ch_close()" function
9944 */
9945 static void
9946f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9947{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009948 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009949
9950 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +01009951 {
Bram Moolenaar8b374212016-02-24 20:43:06 +01009952 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +01009953 channel_clear(channel);
9954 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009955}
9956
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01009957/*
9958 * "ch_getbufnr()" function
9959 */
9960 static void
9961f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
9962{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009963 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01009964
9965 rettv->vval.v_number = -1;
9966 if (channel != NULL)
9967 {
9968 char_u *what = get_tv_string(&argvars[1]);
9969 int part;
9970
9971 if (STRCMP(what, "err") == 0)
9972 part = PART_ERR;
9973 else if (STRCMP(what, "out") == 0)
9974 part = PART_OUT;
9975 else if (STRCMP(what, "in") == 0)
9976 part = PART_IN;
9977 else
9978 part = PART_SOCK;
9979 if (channel->ch_part[part].ch_buffer != NULL)
9980 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
9981 }
9982}
9983
Bram Moolenaar02e83b42016-02-21 20:10:26 +01009984/*
9985 * "ch_getjob()" function
9986 */
9987 static void
9988f_ch_getjob(typval_T *argvars, typval_T *rettv)
9989{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009990 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +01009991
9992 if (channel != NULL)
9993 {
9994 rettv->v_type = VAR_JOB;
9995 rettv->vval.v_job = channel->ch_job;
9996 if (channel->ch_job != NULL)
9997 ++channel->ch_job->jv_refcount;
9998 }
9999}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010000
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010001/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010002 * "ch_log()" function
10003 */
10004 static void
10005f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10006{
10007 char_u *msg = get_tv_string(&argvars[0]);
10008 channel_T *channel = NULL;
10009
10010 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010011 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010012
10013 ch_log(channel, (char *)msg);
10014}
10015
10016/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010017 * "ch_logfile()" function
10018 */
10019 static void
10020f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10021{
10022 char_u *fname;
10023 char_u *opt = (char_u *)"";
10024 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010025
10026 fname = get_tv_string(&argvars[0]);
10027 if (argvars[1].v_type == VAR_STRING)
10028 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010029 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010030}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010031
10032/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010033 * "ch_open()" function
10034 */
10035 static void
10036f_ch_open(typval_T *argvars, typval_T *rettv)
10037{
Bram Moolenaar77073442016-02-13 23:23:53 +010010038 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010039 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010040}
10041
10042/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010043 * "ch_read()" function
10044 */
10045 static void
10046f_ch_read(typval_T *argvars, typval_T *rettv)
10047{
10048 common_channel_read(argvars, rettv, FALSE);
10049}
10050
10051/*
10052 * "ch_readraw()" function
10053 */
10054 static void
10055f_ch_readraw(typval_T *argvars, typval_T *rettv)
10056{
10057 common_channel_read(argvars, rettv, TRUE);
10058}
10059
10060/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010061 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010062 */
10063 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010064f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10065{
10066 ch_expr_common(argvars, rettv, TRUE);
10067}
10068
10069/*
10070 * "ch_sendexpr()" function
10071 */
10072 static void
10073f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10074{
10075 ch_expr_common(argvars, rettv, FALSE);
10076}
10077
10078/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010079 * "ch_evalraw()" function
10080 */
10081 static void
10082f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10083{
10084 ch_raw_common(argvars, rettv, TRUE);
10085}
10086
10087/*
10088 * "ch_sendraw()" function
10089 */
10090 static void
10091f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10092{
10093 ch_raw_common(argvars, rettv, FALSE);
10094}
10095
10096/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010097 * "ch_setoptions()" function
10098 */
10099 static void
10100f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10101{
10102 channel_T *channel;
10103 jobopt_T opt;
10104
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010105 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010106 if (channel == NULL)
10107 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010108 clear_job_options(&opt);
10109 if (get_job_options(&argvars[1], &opt,
10110 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010111 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010112 channel_set_options(channel, &opt);
10113}
10114
10115/*
10116 * "ch_status()" function
10117 */
10118 static void
10119f_ch_status(typval_T *argvars, typval_T *rettv)
10120{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010121 channel_T *channel;
10122
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010123 /* return an empty string by default */
10124 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010125 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010126
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010127 channel = get_channel_arg(&argvars[0], FALSE);
10128 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010129}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010130#endif
10131
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010132/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010133 * "changenr()" function
10134 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010136f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010137{
10138 rettv->vval.v_number = curbuf->b_u_seq_cur;
10139}
10140
10141/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 * "char2nr(string)" function
10143 */
10144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010145f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146{
10147#ifdef FEAT_MBYTE
10148 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010149 {
10150 int utf8 = 0;
10151
10152 if (argvars[1].v_type != VAR_UNKNOWN)
10153 utf8 = get_tv_number_chk(&argvars[1], NULL);
10154
10155 if (utf8)
10156 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10157 else
10158 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160 else
10161#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010162 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163}
10164
10165/*
10166 * "cindent(lnum)" function
10167 */
10168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010169f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170{
10171#ifdef FEAT_CINDENT
10172 pos_T pos;
10173 linenr_T lnum;
10174
10175 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010176 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10178 {
10179 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010180 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 curwin->w_cursor = pos;
10182 }
10183 else
10184#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010185 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186}
10187
10188/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010189 * "clearmatches()" function
10190 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010192f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010193{
10194#ifdef FEAT_SEARCH_EXTRA
10195 clear_matches(curwin);
10196#endif
10197}
10198
10199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 * "col(string)" function
10201 */
10202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010203f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204{
10205 colnr_T col = 0;
10206 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010207 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010209 fp = var2fpos(&argvars[0], FALSE, &fnum);
10210 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 {
10212 if (fp->col == MAXCOL)
10213 {
10214 /* '> can be MAXCOL, get the length of the line then */
10215 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010216 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 else
10218 col = MAXCOL;
10219 }
10220 else
10221 {
10222 col = fp->col + 1;
10223#ifdef FEAT_VIRTUALEDIT
10224 /* col(".") when the cursor is on the NUL at the end of the line
10225 * because of "coladd" can be seen as an extra column. */
10226 if (virtual_active() && fp == &curwin->w_cursor)
10227 {
10228 char_u *p = ml_get_cursor();
10229
10230 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10231 curwin->w_virtcol - curwin->w_cursor.coladd))
10232 {
10233# ifdef FEAT_MBYTE
10234 int l;
10235
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010236 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 col += l;
10238# else
10239 if (*p != NUL && p[1] == NUL)
10240 ++col;
10241# endif
10242 }
10243 }
10244#endif
10245 }
10246 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010247 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248}
10249
Bram Moolenaar572cb562005-08-05 21:35:02 +000010250#if defined(FEAT_INS_EXPAND)
10251/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010252 * "complete()" function
10253 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010255f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010256{
10257 int startcol;
10258
10259 if ((State & INSERT) == 0)
10260 {
10261 EMSG(_("E785: complete() can only be used in Insert mode"));
10262 return;
10263 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010264
10265 /* Check for undo allowed here, because if something was already inserted
10266 * the line was already saved for undo and this check isn't done. */
10267 if (!undo_allowed())
10268 return;
10269
Bram Moolenaarade00832006-03-10 21:46:58 +000010270 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10271 {
10272 EMSG(_(e_invarg));
10273 return;
10274 }
10275
10276 startcol = get_tv_number_chk(&argvars[0], NULL);
10277 if (startcol <= 0)
10278 return;
10279
10280 set_completion(startcol - 1, argvars[1].vval.v_list);
10281}
10282
10283/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010284 * "complete_add()" function
10285 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010287f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010288{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010289 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010290}
10291
10292/*
10293 * "complete_check()" function
10294 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010296f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010297{
10298 int saved = RedrawingDisabled;
10299
10300 RedrawingDisabled = 0;
10301 ins_compl_check_keys(0);
10302 rettv->vval.v_number = compl_interrupted;
10303 RedrawingDisabled = saved;
10304}
10305#endif
10306
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307/*
10308 * "confirm(message, buttons[, default [, type]])" function
10309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010311f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312{
10313#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10314 char_u *message;
10315 char_u *buttons = NULL;
10316 char_u buf[NUMBUFLEN];
10317 char_u buf2[NUMBUFLEN];
10318 int def = 1;
10319 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010320 char_u *typestr;
10321 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010323 message = get_tv_string_chk(&argvars[0]);
10324 if (message == NULL)
10325 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010326 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010328 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10329 if (buttons == NULL)
10330 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010331 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010333 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010334 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010336 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10337 if (typestr == NULL)
10338 error = TRUE;
10339 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010341 switch (TOUPPER_ASC(*typestr))
10342 {
10343 case 'E': type = VIM_ERROR; break;
10344 case 'Q': type = VIM_QUESTION; break;
10345 case 'I': type = VIM_INFO; break;
10346 case 'W': type = VIM_WARNING; break;
10347 case 'G': type = VIM_GENERIC; break;
10348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 }
10350 }
10351 }
10352 }
10353
10354 if (buttons == NULL || *buttons == NUL)
10355 buttons = (char_u *)_("&Ok");
10356
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010357 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010358 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010359 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360#endif
10361}
10362
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010363/*
10364 * "copy()" function
10365 */
10366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010367f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010368{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010369 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010370}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010372#ifdef FEAT_FLOAT
10373/*
10374 * "cos()" function
10375 */
10376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010377f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010378{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010379 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010380
10381 rettv->v_type = VAR_FLOAT;
10382 if (get_float_arg(argvars, &f) == OK)
10383 rettv->vval.v_float = cos(f);
10384 else
10385 rettv->vval.v_float = 0.0;
10386}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010387
10388/*
10389 * "cosh()" function
10390 */
10391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010392f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010393{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010394 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010395
10396 rettv->v_type = VAR_FLOAT;
10397 if (get_float_arg(argvars, &f) == OK)
10398 rettv->vval.v_float = cosh(f);
10399 else
10400 rettv->vval.v_float = 0.0;
10401}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010402#endif
10403
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010405 * "count()" function
10406 */
10407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010408f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010409{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010410 long n = 0;
10411 int ic = FALSE;
10412
Bram Moolenaare9a41262005-01-15 22:18:47 +000010413 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010414 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010415 listitem_T *li;
10416 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010417 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010418
Bram Moolenaare9a41262005-01-15 22:18:47 +000010419 if ((l = argvars[0].vval.v_list) != NULL)
10420 {
10421 li = l->lv_first;
10422 if (argvars[2].v_type != VAR_UNKNOWN)
10423 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010424 int error = FALSE;
10425
10426 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010427 if (argvars[3].v_type != VAR_UNKNOWN)
10428 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010429 idx = get_tv_number_chk(&argvars[3], &error);
10430 if (!error)
10431 {
10432 li = list_find(l, idx);
10433 if (li == NULL)
10434 EMSGN(_(e_listidx), idx);
10435 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010436 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010437 if (error)
10438 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010439 }
10440
10441 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010442 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010443 ++n;
10444 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010445 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010446 else if (argvars[0].v_type == VAR_DICT)
10447 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010448 int todo;
10449 dict_T *d;
10450 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010451
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010452 if ((d = argvars[0].vval.v_dict) != NULL)
10453 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010454 int error = FALSE;
10455
Bram Moolenaare9a41262005-01-15 22:18:47 +000010456 if (argvars[2].v_type != VAR_UNKNOWN)
10457 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010458 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010459 if (argvars[3].v_type != VAR_UNKNOWN)
10460 EMSG(_(e_invarg));
10461 }
10462
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010463 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010464 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010465 {
10466 if (!HASHITEM_EMPTY(hi))
10467 {
10468 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010469 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010470 ++n;
10471 }
10472 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010473 }
10474 }
10475 else
10476 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010477 rettv->vval.v_number = n;
10478}
10479
10480/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10482 *
10483 * Checks the existence of a cscope connection.
10484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010486f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487{
10488#ifdef FEAT_CSCOPE
10489 int num = 0;
10490 char_u *dbpath = NULL;
10491 char_u *prepend = NULL;
10492 char_u buf[NUMBUFLEN];
10493
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010494 if (argvars[0].v_type != VAR_UNKNOWN
10495 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010497 num = (int)get_tv_number(&argvars[0]);
10498 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010499 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010500 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501 }
10502
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010503 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504#endif
10505}
10506
10507/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010508 * "cursor(lnum, col)" function, or
10509 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010511 * Moves the cursor to the specified line and column.
10512 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010515f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516{
10517 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010518#ifdef FEAT_VIRTUALEDIT
10519 long coladd = 0;
10520#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010521 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010523 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010524 if (argvars[1].v_type == VAR_UNKNOWN)
10525 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010526 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010527 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010528
Bram Moolenaar493c1782014-05-28 14:34:46 +020010529 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010530 {
10531 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010532 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010533 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010534 line = pos.lnum;
10535 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010536#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010537 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010538#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010539 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010540 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010541 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010542 set_curswant = FALSE;
10543 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010544 }
10545 else
10546 {
10547 line = get_tv_lnum(argvars);
10548 col = get_tv_number_chk(&argvars[1], NULL);
10549#ifdef FEAT_VIRTUALEDIT
10550 if (argvars[2].v_type != VAR_UNKNOWN)
10551 coladd = get_tv_number_chk(&argvars[2], NULL);
10552#endif
10553 }
10554 if (line < 0 || col < 0
10555#ifdef FEAT_VIRTUALEDIT
10556 || coladd < 0
10557#endif
10558 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010559 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 if (line > 0)
10561 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 if (col > 0)
10563 curwin->w_cursor.col = col - 1;
10564#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010565 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566#endif
10567
10568 /* Make sure the cursor is in a valid position. */
10569 check_cursor();
10570#ifdef FEAT_MBYTE
10571 /* Correct cursor for multi-byte character. */
10572 if (has_mbyte)
10573 mb_adjust_cursor();
10574#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010575
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010576 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010577 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578}
10579
10580/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010581 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582 */
10583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010584f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010586 int noref = 0;
10587
10588 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010589 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010590 if (noref < 0 || noref > 1)
10591 EMSG(_(e_invarg));
10592 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010593 {
10594 current_copyID += COPYID_INC;
10595 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597}
10598
10599/*
10600 * "delete()" function
10601 */
10602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010603f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010605 char_u nbuf[NUMBUFLEN];
10606 char_u *name;
10607 char_u *flags;
10608
10609 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010611 return;
10612
10613 name = get_tv_string(&argvars[0]);
10614 if (name == NULL || *name == NUL)
10615 {
10616 EMSG(_(e_invarg));
10617 return;
10618 }
10619
10620 if (argvars[1].v_type != VAR_UNKNOWN)
10621 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010623 flags = (char_u *)"";
10624
10625 if (*flags == NUL)
10626 /* delete a file */
10627 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10628 else if (STRCMP(flags, "d") == 0)
10629 /* delete an empty directory */
10630 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10631 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010632 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010633 rettv->vval.v_number = delete_recursive(name);
10634 else
10635 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636}
10637
10638/*
10639 * "did_filetype()" function
10640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010642f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643{
10644#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010645 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646#endif
10647}
10648
10649/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010650 * "diff_filler()" function
10651 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010653f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010654{
10655#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010656 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010657#endif
10658}
10659
10660/*
10661 * "diff_hlID()" function
10662 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010664f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010665{
10666#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010667 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010668 static linenr_T prev_lnum = 0;
10669 static int changedtick = 0;
10670 static int fnum = 0;
10671 static int change_start = 0;
10672 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010673 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010674 int filler_lines;
10675 int col;
10676
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010677 if (lnum < 0) /* ignore type error in {lnum} arg */
10678 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010679 if (lnum != prev_lnum
10680 || changedtick != curbuf->b_changedtick
10681 || fnum != curbuf->b_fnum)
10682 {
10683 /* New line, buffer, change: need to get the values. */
10684 filler_lines = diff_check(curwin, lnum);
10685 if (filler_lines < 0)
10686 {
10687 if (filler_lines == -1)
10688 {
10689 change_start = MAXCOL;
10690 change_end = -1;
10691 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10692 hlID = HLF_ADD; /* added line */
10693 else
10694 hlID = HLF_CHD; /* changed line */
10695 }
10696 else
10697 hlID = HLF_ADD; /* added line */
10698 }
10699 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010700 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010701 prev_lnum = lnum;
10702 changedtick = curbuf->b_changedtick;
10703 fnum = curbuf->b_fnum;
10704 }
10705
10706 if (hlID == HLF_CHD || hlID == HLF_TXD)
10707 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010708 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010709 if (col >= change_start && col <= change_end)
10710 hlID = HLF_TXD; /* changed text */
10711 else
10712 hlID = HLF_CHD; /* changed line */
10713 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010714 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010715#endif
10716}
10717
10718/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010719 * "disable_char_avail_for_testing({expr})" function
10720 */
10721 static void
10722f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10723{
10724 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10725}
10726
10727/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010728 * "empty({expr})" function
10729 */
10730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010731f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010732{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010733 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010734
10735 switch (argvars[0].v_type)
10736 {
10737 case VAR_STRING:
10738 case VAR_FUNC:
10739 n = argvars[0].vval.v_string == NULL
10740 || *argvars[0].vval.v_string == NUL;
10741 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010742 case VAR_PARTIAL:
10743 n = FALSE;
10744 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010745 case VAR_NUMBER:
10746 n = argvars[0].vval.v_number == 0;
10747 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010748 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010749#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010750 n = argvars[0].vval.v_float == 0.0;
10751 break;
10752#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010753 case VAR_LIST:
10754 n = argvars[0].vval.v_list == NULL
10755 || argvars[0].vval.v_list->lv_first == NULL;
10756 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010757 case VAR_DICT:
10758 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010759 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010760 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010761 case VAR_SPECIAL:
10762 n = argvars[0].vval.v_number != VVAL_TRUE;
10763 break;
10764
Bram Moolenaar835dc632016-02-07 14:27:38 +010010765 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010766#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010767 n = argvars[0].vval.v_job == NULL
10768 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10769 break;
10770#endif
10771 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010772#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010773 n = argvars[0].vval.v_channel == NULL
10774 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010775 break;
10776#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010777 case VAR_UNKNOWN:
10778 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10779 n = TRUE;
10780 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010781 }
10782
10783 rettv->vval.v_number = n;
10784}
10785
10786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787 * "escape({string}, {chars})" function
10788 */
10789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010790f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791{
10792 char_u buf[NUMBUFLEN];
10793
Bram Moolenaar758711c2005-02-02 23:11:38 +000010794 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10795 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010796 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797}
10798
10799/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010800 * "eval()" function
10801 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010803f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010804{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010805 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010806
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010807 s = get_tv_string_chk(&argvars[0]);
10808 if (s != NULL)
10809 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010810
Bram Moolenaar615b9972015-01-14 17:15:05 +010010811 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010812 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10813 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010814 if (p != NULL && !aborting())
10815 EMSG2(_(e_invexpr2), p);
10816 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010817 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010818 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010819 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010820 else if (*s != NUL)
10821 EMSG(_(e_trailing));
10822}
10823
10824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825 * "eventhandler()" function
10826 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010828f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010830 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831}
10832
10833/*
10834 * "executable()" function
10835 */
10836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010837f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010839 char_u *name = get_tv_string(&argvars[0]);
10840
10841 /* Check in $PATH and also check directly if there is a directory name. */
10842 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10843 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010844}
10845
10846/*
10847 * "exepath()" function
10848 */
10849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010850f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010851{
10852 char_u *p = NULL;
10853
Bram Moolenaarb5971142015-03-21 17:32:19 +010010854 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010855 rettv->v_type = VAR_STRING;
10856 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857}
10858
10859/*
10860 * "exists()" function
10861 */
10862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010863f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864{
10865 char_u *p;
10866 char_u *name;
10867 int n = FALSE;
10868 int len = 0;
10869
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010870 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 if (*p == '$') /* environment variable */
10872 {
10873 /* first try "normal" environment variables (fast) */
10874 if (mch_getenv(p + 1) != NULL)
10875 n = TRUE;
10876 else
10877 {
10878 /* try expanding things like $VIM and ${HOME} */
10879 p = expand_env_save(p);
10880 if (p != NULL && *p != '$')
10881 n = TRUE;
10882 vim_free(p);
10883 }
10884 }
10885 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010886 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010887 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010888 if (*skipwhite(p) != NUL)
10889 n = FALSE; /* trailing garbage */
10890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891 else if (*p == '*') /* internal or user defined function */
10892 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010893 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 }
10895 else if (*p == ':')
10896 {
10897 n = cmd_exists(p + 1);
10898 }
10899 else if (*p == '#')
10900 {
10901#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010902 if (p[1] == '#')
10903 n = autocmd_supported(p + 2);
10904 else
10905 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906#endif
10907 }
10908 else /* internal variable */
10909 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010910 char_u *tofree;
10911 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010913 /* get_name_len() takes care of expanding curly braces */
10914 name = p;
10915 len = get_name_len(&p, &tofree, TRUE, FALSE);
10916 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010918 if (tofree != NULL)
10919 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010920 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010921 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010923 /* handle d.key, l[idx], f(expr) */
10924 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10925 if (n)
10926 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927 }
10928 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010929 if (*p != NUL)
10930 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010932 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 }
10934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010935 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936}
10937
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010938#ifdef FEAT_FLOAT
10939/*
10940 * "exp()" function
10941 */
10942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010943f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010944{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010945 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010946
10947 rettv->v_type = VAR_FLOAT;
10948 if (get_float_arg(argvars, &f) == OK)
10949 rettv->vval.v_float = exp(f);
10950 else
10951 rettv->vval.v_float = 0.0;
10952}
10953#endif
10954
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955/*
10956 * "expand()" function
10957 */
10958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010959f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960{
10961 char_u *s;
10962 int len;
10963 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010964 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010966 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010967 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010969 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010970 if (argvars[1].v_type != VAR_UNKNOWN
10971 && argvars[2].v_type != VAR_UNKNOWN
10972 && get_tv_number_chk(&argvars[2], &error)
10973 && !error)
10974 {
10975 rettv->v_type = VAR_LIST;
10976 rettv->vval.v_list = NULL;
10977 }
10978
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010979 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 if (*s == '%' || *s == '#' || *s == '<')
10981 {
10982 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010983 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010985 if (rettv->v_type == VAR_LIST)
10986 {
10987 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10988 list_append_string(rettv->vval.v_list, result, -1);
10989 else
10990 vim_free(result);
10991 }
10992 else
10993 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 }
10995 else
10996 {
10997 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010998 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010999 if (argvars[1].v_type != VAR_UNKNOWN
11000 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011001 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011002 if (!error)
11003 {
11004 ExpandInit(&xpc);
11005 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011006 if (p_wic)
11007 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011008 if (rettv->v_type == VAR_STRING)
11009 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11010 options, WILD_ALL);
11011 else if (rettv_list_alloc(rettv) != FAIL)
11012 {
11013 int i;
11014
11015 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11016 for (i = 0; i < xpc.xp_numfiles; i++)
11017 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11018 ExpandCleanup(&xpc);
11019 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011020 }
11021 else
11022 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 }
11024}
11025
11026/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011027 * Go over all entries in "d2" and add them to "d1".
11028 * When "action" is "error" then a duplicate key is an error.
11029 * When "action" is "force" then a duplicate key is overwritten.
11030 * Otherwise duplicate keys are ignored ("action" is "keep").
11031 */
11032 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011033dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011034{
11035 dictitem_T *di1;
11036 hashitem_T *hi2;
11037 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011038 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011039
11040 todo = (int)d2->dv_hashtab.ht_used;
11041 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11042 {
11043 if (!HASHITEM_EMPTY(hi2))
11044 {
11045 --todo;
11046 di1 = dict_find(d1, hi2->hi_key, -1);
11047 if (d1->dv_scope != 0)
11048 {
11049 /* Disallow replacing a builtin function in l: and g:.
11050 * Check the key to be valid when adding to any
11051 * scope. */
11052 if (d1->dv_scope == VAR_DEF_SCOPE
11053 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11054 && var_check_func_name(hi2->hi_key,
11055 di1 == NULL))
11056 break;
11057 if (!valid_varname(hi2->hi_key))
11058 break;
11059 }
11060 if (di1 == NULL)
11061 {
11062 di1 = dictitem_copy(HI2DI(hi2));
11063 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11064 dictitem_free(di1);
11065 }
11066 else if (*action == 'e')
11067 {
11068 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11069 break;
11070 }
11071 else if (*action == 'f' && HI2DI(hi2) != di1)
11072 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011073 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11074 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011075 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011076 clear_tv(&di1->di_tv);
11077 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11078 }
11079 }
11080 }
11081}
11082
11083/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011084 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011085 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011086 */
11087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011088f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011089{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011090 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011091
Bram Moolenaare9a41262005-01-15 22:18:47 +000011092 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011093 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011094 list_T *l1, *l2;
11095 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011096 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011097 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011098
Bram Moolenaare9a41262005-01-15 22:18:47 +000011099 l1 = argvars[0].vval.v_list;
11100 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011101 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011102 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011103 {
11104 if (argvars[2].v_type != VAR_UNKNOWN)
11105 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011106 before = get_tv_number_chk(&argvars[2], &error);
11107 if (error)
11108 return; /* type error; errmsg already given */
11109
Bram Moolenaar758711c2005-02-02 23:11:38 +000011110 if (before == l1->lv_len)
11111 item = NULL;
11112 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011113 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011114 item = list_find(l1, before);
11115 if (item == NULL)
11116 {
11117 EMSGN(_(e_listidx), before);
11118 return;
11119 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011120 }
11121 }
11122 else
11123 item = NULL;
11124 list_extend(l1, l2, item);
11125
Bram Moolenaare9a41262005-01-15 22:18:47 +000011126 copy_tv(&argvars[0], rettv);
11127 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011128 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011129 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11130 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011131 dict_T *d1, *d2;
11132 char_u *action;
11133 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011134
11135 d1 = argvars[0].vval.v_dict;
11136 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011137 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011138 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011139 {
11140 /* Check the third argument. */
11141 if (argvars[2].v_type != VAR_UNKNOWN)
11142 {
11143 static char *(av[]) = {"keep", "force", "error"};
11144
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011145 action = get_tv_string_chk(&argvars[2]);
11146 if (action == NULL)
11147 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011148 for (i = 0; i < 3; ++i)
11149 if (STRCMP(action, av[i]) == 0)
11150 break;
11151 if (i == 3)
11152 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011153 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011154 return;
11155 }
11156 }
11157 else
11158 action = (char_u *)"force";
11159
Bram Moolenaara9922d62013-05-30 13:01:18 +020011160 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011161
Bram Moolenaare9a41262005-01-15 22:18:47 +000011162 copy_tv(&argvars[0], rettv);
11163 }
11164 }
11165 else
11166 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011167}
11168
11169/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011170 * "feedkeys()" function
11171 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011173f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011174{
11175 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011176 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011177 char_u *keys, *flags;
11178 char_u nbuf[NUMBUFLEN];
11179 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011180 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011181 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011182
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011183 /* This is not allowed in the sandbox. If the commands would still be
11184 * executed in the sandbox it would be OK, but it probably happens later,
11185 * when "sandbox" is no longer set. */
11186 if (check_secure())
11187 return;
11188
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011189 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011190
11191 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011192 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011193 flags = get_tv_string_buf(&argvars[1], nbuf);
11194 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011195 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011196 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011197 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011198 case 'n': remap = FALSE; break;
11199 case 'm': remap = TRUE; break;
11200 case 't': typed = TRUE; break;
11201 case 'i': insert = TRUE; break;
11202 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011203 }
11204 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011205 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011206
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011207 if (*keys != NUL || execute)
11208 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011209 /* Need to escape K_SPECIAL and CSI before putting the string in the
11210 * typeahead buffer. */
11211 keys_esc = vim_strsave_escape_csi(keys);
11212 if (keys_esc != NULL)
11213 {
11214 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011215 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011216 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011217 if (vgetc_busy)
11218 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011219 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011220 {
11221 int save_msg_scroll = msg_scroll;
11222
11223 /* Avoid a 1 second delay when the keys start Insert mode. */
11224 msg_scroll = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011225 exec_normal(TRUE);
Bram Moolenaar9e496852016-03-11 19:31:47 +010011226 msg_scroll |= save_msg_scroll;
11227 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011228 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011229 }
11230}
11231
11232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233 * "filereadable()" function
11234 */
11235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011236f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011238 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239 char_u *p;
11240 int n;
11241
Bram Moolenaarc236c162008-07-13 17:41:49 +000011242#ifndef O_NONBLOCK
11243# define O_NONBLOCK 0
11244#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011245 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011246 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11247 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248 {
11249 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011250 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251 }
11252 else
11253 n = FALSE;
11254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011255 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256}
11257
11258/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011259 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 * rights to write into.
11261 */
11262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011263f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011265 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266}
11267
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011268 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011269findfilendir(
11270 typval_T *argvars UNUSED,
11271 typval_T *rettv,
11272 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011273{
11274#ifdef FEAT_SEARCHPATH
11275 char_u *fname;
11276 char_u *fresult = NULL;
11277 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11278 char_u *p;
11279 char_u pathbuf[NUMBUFLEN];
11280 int count = 1;
11281 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011282 int error = FALSE;
11283#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011284
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011285 rettv->vval.v_string = NULL;
11286 rettv->v_type = VAR_STRING;
11287
11288#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011289 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011290
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011291 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011292 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011293 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11294 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011295 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011296 else
11297 {
11298 if (*p != NUL)
11299 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011300
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011301 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011302 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011303 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011304 }
11305
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011306 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11307 error = TRUE;
11308
11309 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011311 do
11312 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011313 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011314 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011315 fresult = find_file_in_path_option(first ? fname : NULL,
11316 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011317 0, first, path,
11318 find_what,
11319 curbuf->b_ffname,
11320 find_what == FINDFILE_DIR
11321 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011322 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011323
11324 if (fresult != NULL && rettv->v_type == VAR_LIST)
11325 list_append_string(rettv->vval.v_list, fresult, -1);
11326
11327 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011328 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011329
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011330 if (rettv->v_type == VAR_STRING)
11331 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011332#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011333}
11334
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011335static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11336static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011337
11338/*
11339 * Implementation of map() and filter().
11340 */
11341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011342filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011343{
11344 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011345 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011346 listitem_T *li, *nli;
11347 list_T *l = NULL;
11348 dictitem_T *di;
11349 hashtab_T *ht;
11350 hashitem_T *hi;
11351 dict_T *d = NULL;
11352 typval_T save_val;
11353 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011354 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011355 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011356 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011357 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011358 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011359 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011360 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011361
Bram Moolenaare9a41262005-01-15 22:18:47 +000011362 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011363 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011364 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011365 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011366 return;
11367 }
11368 else if (argvars[0].v_type == VAR_DICT)
11369 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011370 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011371 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011372 return;
11373 }
11374 else
11375 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011376 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011377 return;
11378 }
11379
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011380 expr = get_tv_string_buf_chk(&argvars[1], buf);
11381 /* On type errors, the preceding call has already displayed an error
11382 * message. Avoid a misleading error message for an empty string that
11383 * was not passed as argument. */
11384 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011386 prepare_vimvar(VV_VAL, &save_val);
11387 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011388
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011389 /* We reset "did_emsg" to be able to detect whether an error
11390 * occurred during evaluation of the expression. */
11391 save_did_emsg = did_emsg;
11392 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011393
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011394 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011395 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011396 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011397 vimvars[VV_KEY].vv_type = VAR_STRING;
11398
11399 ht = &d->dv_hashtab;
11400 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011401 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011402 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011403 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011404 if (!HASHITEM_EMPTY(hi))
11405 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011406 int r;
11407
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011408 --todo;
11409 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011410 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011411 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11412 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011413 break;
11414 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011415 r = filter_map_one(&di->di_tv, expr, map, &rem);
11416 clear_tv(&vimvars[VV_KEY].vv_tv);
11417 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011418 break;
11419 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011420 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011421 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11422 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011423 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011424 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011425 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011426 }
11427 }
11428 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011429 }
11430 else
11431 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011432 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11433
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011434 for (li = l->lv_first; li != NULL; li = nli)
11435 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011436 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011437 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011438 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011439 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011440 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011441 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011442 break;
11443 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011444 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011445 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011446 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011447 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011448
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011449 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011450 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011451
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011452 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011453 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011454
11455 copy_tv(&argvars[0], rettv);
11456}
11457
11458 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011459filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011460{
Bram Moolenaar33570922005-01-25 22:26:29 +000011461 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011462 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011463 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011464
Bram Moolenaar33570922005-01-25 22:26:29 +000011465 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011466 s = expr;
11467 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011468 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011469 if (*s != NUL) /* check for trailing chars after expr */
11470 {
11471 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011472 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011473 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011474 }
11475 if (map)
11476 {
11477 /* map(): replace the list item value */
11478 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011479 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011480 *tv = rettv;
11481 }
11482 else
11483 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011484 int error = FALSE;
11485
Bram Moolenaare9a41262005-01-15 22:18:47 +000011486 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011487 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011488 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011489 /* On type error, nothing has been removed; return FAIL to stop the
11490 * loop. The error message was given by get_tv_number_chk(). */
11491 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011492 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011493 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011494 retval = OK;
11495theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011496 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011497 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011498}
11499
11500/*
11501 * "filter()" function
11502 */
11503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011504f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011505{
11506 filter_map(argvars, rettv, FALSE);
11507}
11508
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011509/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011510 * "finddir({fname}[, {path}[, {count}]])" function
11511 */
11512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011513f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011514{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011515 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011516}
11517
11518/*
11519 * "findfile({fname}[, {path}[, {count}]])" function
11520 */
11521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011522f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011523{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011524 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011525}
11526
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011527#ifdef FEAT_FLOAT
11528/*
11529 * "float2nr({float})" function
11530 */
11531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011532f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011533{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011534 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011535
11536 if (get_float_arg(argvars, &f) == OK)
11537 {
11538 if (f < -0x7fffffff)
11539 rettv->vval.v_number = -0x7fffffff;
11540 else if (f > 0x7fffffff)
11541 rettv->vval.v_number = 0x7fffffff;
11542 else
11543 rettv->vval.v_number = (varnumber_T)f;
11544 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011545}
11546
11547/*
11548 * "floor({float})" function
11549 */
11550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011551f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011552{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011553 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011554
11555 rettv->v_type = VAR_FLOAT;
11556 if (get_float_arg(argvars, &f) == OK)
11557 rettv->vval.v_float = floor(f);
11558 else
11559 rettv->vval.v_float = 0.0;
11560}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011561
11562/*
11563 * "fmod()" function
11564 */
11565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011566f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011567{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011568 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011569
11570 rettv->v_type = VAR_FLOAT;
11571 if (get_float_arg(argvars, &fx) == OK
11572 && get_float_arg(&argvars[1], &fy) == OK)
11573 rettv->vval.v_float = fmod(fx, fy);
11574 else
11575 rettv->vval.v_float = 0.0;
11576}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011577#endif
11578
Bram Moolenaar0d660222005-01-07 21:51:51 +000011579/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011580 * "fnameescape({string})" function
11581 */
11582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011583f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011584{
11585 rettv->vval.v_string = vim_strsave_fnameescape(
11586 get_tv_string(&argvars[0]), FALSE);
11587 rettv->v_type = VAR_STRING;
11588}
11589
11590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011591 * "fnamemodify({fname}, {mods})" function
11592 */
11593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011594f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595{
11596 char_u *fname;
11597 char_u *mods;
11598 int usedlen = 0;
11599 int len;
11600 char_u *fbuf = NULL;
11601 char_u buf[NUMBUFLEN];
11602
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011603 fname = get_tv_string_chk(&argvars[0]);
11604 mods = get_tv_string_buf_chk(&argvars[1], buf);
11605 if (fname == NULL || mods == NULL)
11606 fname = NULL;
11607 else
11608 {
11609 len = (int)STRLEN(fname);
11610 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011613 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011614 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011615 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011617 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618 vim_free(fbuf);
11619}
11620
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011621static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011622
11623/*
11624 * "foldclosed()" function
11625 */
11626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011627foldclosed_both(
11628 typval_T *argvars UNUSED,
11629 typval_T *rettv,
11630 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631{
11632#ifdef FEAT_FOLDING
11633 linenr_T lnum;
11634 linenr_T first, last;
11635
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011636 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011637 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11638 {
11639 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11640 {
11641 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011642 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011644 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645 return;
11646 }
11647 }
11648#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011649 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650}
11651
11652/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011653 * "foldclosed()" function
11654 */
11655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011656f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011657{
11658 foldclosed_both(argvars, rettv, FALSE);
11659}
11660
11661/*
11662 * "foldclosedend()" function
11663 */
11664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011665f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011666{
11667 foldclosed_both(argvars, rettv, TRUE);
11668}
11669
11670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011671 * "foldlevel()" function
11672 */
11673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011674f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011675{
11676#ifdef FEAT_FOLDING
11677 linenr_T lnum;
11678
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011679 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011681 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683}
11684
11685/*
11686 * "foldtext()" function
11687 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011689f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690{
11691#ifdef FEAT_FOLDING
11692 linenr_T lnum;
11693 char_u *s;
11694 char_u *r;
11695 int len;
11696 char *txt;
11697#endif
11698
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011699 rettv->v_type = VAR_STRING;
11700 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011701#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011702 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11703 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11704 <= curbuf->b_ml.ml_line_count
11705 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706 {
11707 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011708 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11709 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710 {
11711 if (!linewhite(lnum))
11712 break;
11713 ++lnum;
11714 }
11715
11716 /* Find interesting text in this line. */
11717 s = skipwhite(ml_get(lnum));
11718 /* skip C comment-start */
11719 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011720 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011721 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011722 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011723 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011724 {
11725 s = skipwhite(ml_get(lnum + 1));
11726 if (*s == '*')
11727 s = skipwhite(s + 1);
11728 }
11729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730 txt = _("+-%s%3ld lines: ");
11731 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011732 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733 + 20 /* for %3ld */
11734 + STRLEN(s))); /* concatenated */
11735 if (r != NULL)
11736 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011737 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11738 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11739 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011740 len = (int)STRLEN(r);
11741 STRCAT(r, s);
11742 /* remove 'foldmarker' and 'commentstring' */
11743 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011744 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011745 }
11746 }
11747#endif
11748}
11749
11750/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011751 * "foldtextresult(lnum)" function
11752 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011754f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011755{
11756#ifdef FEAT_FOLDING
11757 linenr_T lnum;
11758 char_u *text;
11759 char_u buf[51];
11760 foldinfo_T foldinfo;
11761 int fold_count;
11762#endif
11763
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011764 rettv->v_type = VAR_STRING;
11765 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011766#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011767 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011768 /* treat illegal types and illegal string values for {lnum} the same */
11769 if (lnum < 0)
11770 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011771 fold_count = foldedCount(curwin, lnum, &foldinfo);
11772 if (fold_count > 0)
11773 {
11774 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11775 &foldinfo, buf);
11776 if (text == buf)
11777 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011778 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011779 }
11780#endif
11781}
11782
11783/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784 * "foreground()" function
11785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011787f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789#ifdef FEAT_GUI
11790 if (gui.in_use)
11791 gui_mch_set_foreground();
11792#else
11793# ifdef WIN32
11794 win32_set_foreground();
11795# endif
11796#endif
11797}
11798
11799/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011800 * "function()" function
11801 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011803f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011804{
11805 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011806 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011807 int use_string = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011808
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011809 if (argvars[0].v_type == VAR_FUNC)
11810 {
11811 /* function(MyFunc, [arg], dict) */
11812 s = argvars[0].vval.v_string;
11813 }
11814 else if (argvars[0].v_type == VAR_PARTIAL
11815 && argvars[0].vval.v_partial != NULL)
11816 /* function(dict.MyFunc, [arg]) */
11817 s = argvars[0].vval.v_partial->pt_name;
11818 else
11819 {
11820 /* function('MyFunc', [arg], dict) */
11821 s = get_tv_string(&argvars[0]);
11822 use_string = TRUE;
11823 }
11824
11825 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011826 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011827 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011828 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
11829 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011830 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011831 else
11832 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011833 int dict_idx = 0;
11834 int arg_idx = 0;
11835 list_T *list = NULL;
11836
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011837 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011838 {
11839 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011840 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011841
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011842 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11843 * also be called from another script. Using trans_function_name()
11844 * would also work, but some plugins depend on the name being
11845 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011846 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011847 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
11848 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011849 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011850 STRCPY(name, sid_buf);
11851 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011852 }
11853 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011854 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011855 name = vim_strsave(s);
11856
11857 if (argvars[1].v_type != VAR_UNKNOWN)
11858 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011859 if (argvars[2].v_type != VAR_UNKNOWN)
11860 {
11861 /* function(name, [args], dict) */
11862 arg_idx = 1;
11863 dict_idx = 2;
11864 }
11865 else if (argvars[1].v_type == VAR_DICT)
11866 /* function(name, dict) */
11867 dict_idx = 1;
11868 else
11869 /* function(name, [args]) */
11870 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010011871 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011872 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011873 if (argvars[dict_idx].v_type != VAR_DICT)
11874 {
11875 EMSG(_("E922: expected a dict"));
11876 vim_free(name);
11877 return;
11878 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011879 if (argvars[0].v_type == VAR_PARTIAL)
11880 {
11881 EMSG2(_(e_dict_both), name);
11882 vim_free(name);
11883 return;
11884 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011885 if (argvars[dict_idx].vval.v_dict == NULL)
11886 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011887 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011888 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011889 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011890 if (argvars[arg_idx].v_type != VAR_LIST)
11891 {
11892 EMSG(_("E923: Second argument of function() must be a list or a dict"));
11893 vim_free(name);
11894 return;
11895 }
11896 list = argvars[arg_idx].vval.v_list;
11897 if (list == NULL || list->lv_len == 0)
11898 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011899 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011900 }
11901 if (dict_idx > 0 || arg_idx > 0)
11902 {
11903 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011904
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011905 if (pt != NULL)
11906 {
11907 if (arg_idx > 0)
11908 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011909 listitem_T *li;
11910 int i = 0;
11911
11912 pt->pt_argv = (typval_T *)alloc(
11913 sizeof(typval_T) * list->lv_len);
11914 if (pt->pt_argv == NULL)
11915 {
11916 vim_free(pt);
11917 vim_free(name);
11918 return;
11919 }
11920 else
11921 {
11922 pt->pt_argc = list->lv_len;
11923 for (li = list->lv_first; li != NULL; li = li->li_next)
11924 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
11925 }
11926 }
11927
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011928 if (argvars[0].v_type == VAR_PARTIAL)
11929 {
11930 pt->pt_dict = argvars[0].vval.v_partial->pt_dict;
11931 ++pt->pt_dict->dv_refcount;
11932 }
11933 else if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011934 {
11935 pt->pt_dict = argvars[dict_idx].vval.v_dict;
11936 ++pt->pt_dict->dv_refcount;
11937 }
11938
11939 pt->pt_refcount = 1;
11940 pt->pt_name = name;
11941 func_ref(pt->pt_name);
11942 }
11943 rettv->v_type = VAR_PARTIAL;
11944 rettv->vval.v_partial = pt;
11945 }
11946 else
11947 {
11948 rettv->v_type = VAR_FUNC;
11949 rettv->vval.v_string = name;
11950 func_ref(name);
11951 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011952 }
11953}
11954
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011955 static void
11956partial_free(partial_T *pt)
11957{
11958 int i;
11959
11960 for (i = 0; i < pt->pt_argc; ++i)
11961 clear_tv(&pt->pt_argv[i]);
11962 vim_free(pt->pt_argv);
11963 func_unref(pt->pt_name);
11964 vim_free(pt->pt_name);
11965 vim_free(pt);
11966}
11967
11968/*
11969 * Unreference a closure: decrement the reference count and free it when it
11970 * becomes zero.
11971 */
11972 void
11973partial_unref(partial_T *pt)
11974{
11975 if (pt != NULL && --pt->pt_refcount <= 0)
11976 partial_free(pt);
11977}
11978
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011979/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011980 * "garbagecollect()" function
11981 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011983f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011984{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011985 /* This is postponed until we are back at the toplevel, because we may be
11986 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11987 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011988
11989 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11990 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011991}
11992
11993/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011994 * "get()" function
11995 */
11996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011997f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011998{
Bram Moolenaar33570922005-01-25 22:26:29 +000011999 listitem_T *li;
12000 list_T *l;
12001 dictitem_T *di;
12002 dict_T *d;
12003 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012004
Bram Moolenaare9a41262005-01-15 22:18:47 +000012005 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012006 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012007 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012008 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012009 int error = FALSE;
12010
12011 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12012 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012013 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012014 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012015 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012016 else if (argvars[0].v_type == VAR_DICT)
12017 {
12018 if ((d = argvars[0].vval.v_dict) != NULL)
12019 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012020 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012021 if (di != NULL)
12022 tv = &di->di_tv;
12023 }
12024 }
12025 else
12026 EMSG2(_(e_listdictarg), "get()");
12027
12028 if (tv == NULL)
12029 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012030 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012031 copy_tv(&argvars[2], rettv);
12032 }
12033 else
12034 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012035}
12036
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012037static 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 +000012038
12039/*
12040 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012041 * Return a range (from start to end) of lines in rettv from the specified
12042 * buffer.
12043 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012044 */
12045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012046get_buffer_lines(
12047 buf_T *buf,
12048 linenr_T start,
12049 linenr_T end,
12050 int retlist,
12051 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012052{
12053 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012054
Bram Moolenaar959a1432013-12-14 12:17:38 +010012055 rettv->v_type = VAR_STRING;
12056 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012057 if (retlist && rettv_list_alloc(rettv) == FAIL)
12058 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012059
12060 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12061 return;
12062
12063 if (!retlist)
12064 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012065 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12066 p = ml_get_buf(buf, start, FALSE);
12067 else
12068 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012069 rettv->vval.v_string = vim_strsave(p);
12070 }
12071 else
12072 {
12073 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012074 return;
12075
12076 if (start < 1)
12077 start = 1;
12078 if (end > buf->b_ml.ml_line_count)
12079 end = buf->b_ml.ml_line_count;
12080 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012081 if (list_append_string(rettv->vval.v_list,
12082 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012083 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012084 }
12085}
12086
12087/*
12088 * "getbufline()" function
12089 */
12090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012091f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012092{
12093 linenr_T lnum;
12094 linenr_T end;
12095 buf_T *buf;
12096
12097 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12098 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012099 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012100 --emsg_off;
12101
Bram Moolenaar661b1822005-07-28 22:36:45 +000012102 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012103 if (argvars[2].v_type == VAR_UNKNOWN)
12104 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012105 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012106 end = get_tv_lnum_buf(&argvars[2], buf);
12107
Bram Moolenaar342337a2005-07-21 21:11:17 +000012108 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012109}
12110
Bram Moolenaar0d660222005-01-07 21:51:51 +000012111/*
12112 * "getbufvar()" function
12113 */
12114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012115f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012116{
12117 buf_T *buf;
12118 buf_T *save_curbuf;
12119 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012120 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012121 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012122
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012123 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12124 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012125 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012126 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012127
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012128 rettv->v_type = VAR_STRING;
12129 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012130
12131 if (buf != NULL && varname != NULL)
12132 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012133 /* set curbuf to be our buf, temporarily */
12134 save_curbuf = curbuf;
12135 curbuf = buf;
12136
Bram Moolenaar0d660222005-01-07 21:51:51 +000012137 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012138 {
12139 if (get_option_tv(&varname, rettv, TRUE) == OK)
12140 done = TRUE;
12141 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012142 else if (STRCMP(varname, "changedtick") == 0)
12143 {
12144 rettv->v_type = VAR_NUMBER;
12145 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012146 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012147 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012148 else
12149 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012150 /* Look up the variable. */
12151 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12152 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12153 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012154 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012155 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012156 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012157 done = TRUE;
12158 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012159 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012160
12161 /* restore previous notion of curbuf */
12162 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012163 }
12164
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012165 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12166 /* use the default value */
12167 copy_tv(&argvars[2], rettv);
12168
Bram Moolenaar0d660222005-01-07 21:51:51 +000012169 --emsg_off;
12170}
12171
12172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012173 * "getchar()" function
12174 */
12175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012176f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177{
12178 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012179 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012180
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012181 /* Position the cursor. Needed after a message that ends in a space. */
12182 windgoto(msg_row, msg_col);
12183
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184 ++no_mapping;
12185 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012186 for (;;)
12187 {
12188 if (argvars[0].v_type == VAR_UNKNOWN)
12189 /* getchar(): blocking wait. */
12190 n = safe_vgetc();
12191 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12192 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012193 n = vpeekc_any();
12194 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012195 /* illegal argument or getchar(0) and no char avail: return zero */
12196 n = 0;
12197 else
12198 /* getchar(0) and char avail: return char */
12199 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012200
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012201 if (n == K_IGNORE)
12202 continue;
12203 break;
12204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205 --no_mapping;
12206 --allow_keys;
12207
Bram Moolenaar219b8702006-11-01 14:32:36 +000012208 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12209 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12210 vimvars[VV_MOUSE_COL].vv_nr = 0;
12211
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012212 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012213 if (IS_SPECIAL(n) || mod_mask != 0)
12214 {
12215 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12216 int i = 0;
12217
12218 /* Turn a special key into three bytes, plus modifier. */
12219 if (mod_mask != 0)
12220 {
12221 temp[i++] = K_SPECIAL;
12222 temp[i++] = KS_MODIFIER;
12223 temp[i++] = mod_mask;
12224 }
12225 if (IS_SPECIAL(n))
12226 {
12227 temp[i++] = K_SPECIAL;
12228 temp[i++] = K_SECOND(n);
12229 temp[i++] = K_THIRD(n);
12230 }
12231#ifdef FEAT_MBYTE
12232 else if (has_mbyte)
12233 i += (*mb_char2bytes)(n, temp + i);
12234#endif
12235 else
12236 temp[i++] = n;
12237 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012238 rettv->v_type = VAR_STRING;
12239 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012240
12241#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012242 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012243 {
12244 int row = mouse_row;
12245 int col = mouse_col;
12246 win_T *win;
12247 linenr_T lnum;
12248# ifdef FEAT_WINDOWS
12249 win_T *wp;
12250# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012251 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012252
12253 if (row >= 0 && col >= 0)
12254 {
12255 /* Find the window at the mouse coordinates and compute the
12256 * text position. */
12257 win = mouse_find_win(&row, &col);
12258 (void)mouse_comp_pos(win, &row, &col, &lnum);
12259# ifdef FEAT_WINDOWS
12260 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012261 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012262# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012263 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012264 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12265 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12266 }
12267 }
12268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269 }
12270}
12271
12272/*
12273 * "getcharmod()" function
12274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012276f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012277{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012278 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012279}
12280
12281/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012282 * "getcharsearch()" function
12283 */
12284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012285f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012286{
12287 if (rettv_dict_alloc(rettv) != FAIL)
12288 {
12289 dict_T *dict = rettv->vval.v_dict;
12290
12291 dict_add_nr_str(dict, "char", 0L, last_csearch());
12292 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12293 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12294 }
12295}
12296
12297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298 * "getcmdline()" function
12299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012301f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012302{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012303 rettv->v_type = VAR_STRING;
12304 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012305}
12306
12307/*
12308 * "getcmdpos()" function
12309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012311f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012313 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314}
12315
12316/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012317 * "getcmdtype()" function
12318 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012320f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012321{
12322 rettv->v_type = VAR_STRING;
12323 rettv->vval.v_string = alloc(2);
12324 if (rettv->vval.v_string != NULL)
12325 {
12326 rettv->vval.v_string[0] = get_cmdline_type();
12327 rettv->vval.v_string[1] = NUL;
12328 }
12329}
12330
12331/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012332 * "getcmdwintype()" function
12333 */
12334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012335f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012336{
12337 rettv->v_type = VAR_STRING;
12338 rettv->vval.v_string = NULL;
12339#ifdef FEAT_CMDWIN
12340 rettv->vval.v_string = alloc(2);
12341 if (rettv->vval.v_string != NULL)
12342 {
12343 rettv->vval.v_string[0] = cmdwin_type;
12344 rettv->vval.v_string[1] = NUL;
12345 }
12346#endif
12347}
12348
12349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350 * "getcwd()" function
12351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012353f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012355 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012356 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012358 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012359 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012360
12361 wp = find_tabwin(&argvars[0], &argvars[1]);
12362 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012363 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012364 if (wp->w_localdir != NULL)
12365 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12366 else if(globaldir != NULL)
12367 rettv->vval.v_string = vim_strsave(globaldir);
12368 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012369 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012370 cwd = alloc(MAXPATHL);
12371 if (cwd != NULL)
12372 {
12373 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12374 rettv->vval.v_string = vim_strsave(cwd);
12375 vim_free(cwd);
12376 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012377 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012378#ifdef BACKSLASH_IN_FILENAME
12379 if (rettv->vval.v_string != NULL)
12380 slash_adjust(rettv->vval.v_string);
12381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382 }
12383}
12384
12385/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012386 * "getfontname()" function
12387 */
12388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012389f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012390{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012391 rettv->v_type = VAR_STRING;
12392 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012393#ifdef FEAT_GUI
12394 if (gui.in_use)
12395 {
12396 GuiFont font;
12397 char_u *name = NULL;
12398
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012399 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012400 {
12401 /* Get the "Normal" font. Either the name saved by
12402 * hl_set_font_name() or from the font ID. */
12403 font = gui.norm_font;
12404 name = hl_get_font_name();
12405 }
12406 else
12407 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012408 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012409 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12410 return;
12411 font = gui_mch_get_font(name, FALSE);
12412 if (font == NOFONT)
12413 return; /* Invalid font name, return empty string. */
12414 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012415 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012416 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012417 gui_mch_free_font(font);
12418 }
12419#endif
12420}
12421
12422/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012423 * "getfperm({fname})" function
12424 */
12425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012426f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012427{
12428 char_u *fname;
12429 struct stat st;
12430 char_u *perm = NULL;
12431 char_u flags[] = "rwx";
12432 int i;
12433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012434 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012435
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012436 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012437 if (mch_stat((char *)fname, &st) >= 0)
12438 {
12439 perm = vim_strsave((char_u *)"---------");
12440 if (perm != NULL)
12441 {
12442 for (i = 0; i < 9; i++)
12443 {
12444 if (st.st_mode & (1 << (8 - i)))
12445 perm[i] = flags[i % 3];
12446 }
12447 }
12448 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012449 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012450}
12451
12452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012453 * "getfsize({fname})" function
12454 */
12455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012456f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457{
12458 char_u *fname;
12459 struct stat st;
12460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012461 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012462
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012463 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012464
12465 if (mch_stat((char *)fname, &st) >= 0)
12466 {
12467 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012468 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012469 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012470 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012471 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012472
12473 /* non-perfect check for overflow */
12474 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12475 rettv->vval.v_number = -2;
12476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477 }
12478 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012479 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480}
12481
12482/*
12483 * "getftime({fname})" function
12484 */
12485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012486f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487{
12488 char_u *fname;
12489 struct stat st;
12490
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012491 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012492
12493 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012494 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012495 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012496 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012497}
12498
12499/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012500 * "getftype({fname})" function
12501 */
12502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012503f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012504{
12505 char_u *fname;
12506 struct stat st;
12507 char_u *type = NULL;
12508 char *t;
12509
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012510 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012511
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012512 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012513 if (mch_lstat((char *)fname, &st) >= 0)
12514 {
12515#ifdef S_ISREG
12516 if (S_ISREG(st.st_mode))
12517 t = "file";
12518 else if (S_ISDIR(st.st_mode))
12519 t = "dir";
12520# ifdef S_ISLNK
12521 else if (S_ISLNK(st.st_mode))
12522 t = "link";
12523# endif
12524# ifdef S_ISBLK
12525 else if (S_ISBLK(st.st_mode))
12526 t = "bdev";
12527# endif
12528# ifdef S_ISCHR
12529 else if (S_ISCHR(st.st_mode))
12530 t = "cdev";
12531# endif
12532# ifdef S_ISFIFO
12533 else if (S_ISFIFO(st.st_mode))
12534 t = "fifo";
12535# endif
12536# ifdef S_ISSOCK
12537 else if (S_ISSOCK(st.st_mode))
12538 t = "fifo";
12539# endif
12540 else
12541 t = "other";
12542#else
12543# ifdef S_IFMT
12544 switch (st.st_mode & S_IFMT)
12545 {
12546 case S_IFREG: t = "file"; break;
12547 case S_IFDIR: t = "dir"; break;
12548# ifdef S_IFLNK
12549 case S_IFLNK: t = "link"; break;
12550# endif
12551# ifdef S_IFBLK
12552 case S_IFBLK: t = "bdev"; break;
12553# endif
12554# ifdef S_IFCHR
12555 case S_IFCHR: t = "cdev"; break;
12556# endif
12557# ifdef S_IFIFO
12558 case S_IFIFO: t = "fifo"; break;
12559# endif
12560# ifdef S_IFSOCK
12561 case S_IFSOCK: t = "socket"; break;
12562# endif
12563 default: t = "other";
12564 }
12565# else
12566 if (mch_isdir(fname))
12567 t = "dir";
12568 else
12569 t = "file";
12570# endif
12571#endif
12572 type = vim_strsave((char_u *)t);
12573 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012574 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012575}
12576
12577/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012578 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012579 */
12580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012581f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012582{
12583 linenr_T lnum;
12584 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012585 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012586
12587 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012588 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012589 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012590 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012591 retlist = FALSE;
12592 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012593 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012594 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012595 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012596 retlist = TRUE;
12597 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012598
Bram Moolenaar342337a2005-07-21 21:11:17 +000012599 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012600}
12601
12602/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012603 * "getmatches()" function
12604 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012606f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012607{
12608#ifdef FEAT_SEARCH_EXTRA
12609 dict_T *dict;
12610 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012611 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012612
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012613 if (rettv_list_alloc(rettv) == OK)
12614 {
12615 while (cur != NULL)
12616 {
12617 dict = dict_alloc();
12618 if (dict == NULL)
12619 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012620 if (cur->match.regprog == NULL)
12621 {
12622 /* match added with matchaddpos() */
12623 for (i = 0; i < MAXPOSMATCH; ++i)
12624 {
12625 llpos_T *llpos;
12626 char buf[6];
12627 list_T *l;
12628
12629 llpos = &cur->pos.pos[i];
12630 if (llpos->lnum == 0)
12631 break;
12632 l = list_alloc();
12633 if (l == NULL)
12634 break;
12635 list_append_number(l, (varnumber_T)llpos->lnum);
12636 if (llpos->col > 0)
12637 {
12638 list_append_number(l, (varnumber_T)llpos->col);
12639 list_append_number(l, (varnumber_T)llpos->len);
12640 }
12641 sprintf(buf, "pos%d", i + 1);
12642 dict_add_list(dict, buf, l);
12643 }
12644 }
12645 else
12646 {
12647 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12648 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012649 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012650 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12651 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012652# ifdef FEAT_CONCEAL
12653 if (cur->conceal_char)
12654 {
12655 char_u buf[MB_MAXBYTES + 1];
12656
12657 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12658 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12659 }
12660# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012661 list_append_dict(rettv->vval.v_list, dict);
12662 cur = cur->next;
12663 }
12664 }
12665#endif
12666}
12667
12668/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012669 * "getpid()" function
12670 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012672f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012673{
12674 rettv->vval.v_number = mch_get_pid();
12675}
12676
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012677static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012678
12679/*
12680 * "getcurpos()" function
12681 */
12682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012683f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012684{
12685 getpos_both(argvars, rettv, TRUE);
12686}
12687
Bram Moolenaar18081e32008-02-20 19:11:07 +000012688/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012689 * "getpos(string)" function
12690 */
12691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012692f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012693{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012694 getpos_both(argvars, rettv, FALSE);
12695}
12696
12697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012698getpos_both(
12699 typval_T *argvars,
12700 typval_T *rettv,
12701 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012702{
Bram Moolenaara5525202006-03-02 22:52:09 +000012703 pos_T *fp;
12704 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012705 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012706
12707 if (rettv_list_alloc(rettv) == OK)
12708 {
12709 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012710 if (getcurpos)
12711 fp = &curwin->w_cursor;
12712 else
12713 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012714 if (fnum != -1)
12715 list_append_number(l, (varnumber_T)fnum);
12716 else
12717 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012718 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12719 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012720 list_append_number(l, (fp != NULL)
12721 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012722 : (varnumber_T)0);
12723 list_append_number(l,
12724#ifdef FEAT_VIRTUALEDIT
12725 (fp != NULL) ? (varnumber_T)fp->coladd :
12726#endif
12727 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012728 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012729 {
12730 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012731 list_append_number(l, curwin->w_curswant == MAXCOL ?
12732 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012733 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012734 }
12735 else
12736 rettv->vval.v_number = FALSE;
12737}
12738
12739/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012740 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012741 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012743f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012744{
12745#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012746 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012747#endif
12748
Bram Moolenaar2641f772005-03-25 21:58:17 +000012749#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012750 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012751 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012752 wp = NULL;
12753 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12754 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012755 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012756 if (wp == NULL)
12757 return;
12758 }
12759
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012760 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012761 }
12762#endif
12763}
12764
12765/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766 * "getreg()" function
12767 */
12768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012769f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770{
12771 char_u *strregname;
12772 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012773 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012774 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012775 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012777 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012779 strregname = get_tv_string_chk(&argvars[0]);
12780 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012781 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012782 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012783 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012784 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12785 return_list = get_tv_number_chk(&argvars[2], &error);
12786 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012789 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012790
12791 if (error)
12792 return;
12793
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794 regname = (strregname == NULL ? '"' : *strregname);
12795 if (regname == 0)
12796 regname = '"';
12797
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012798 if (return_list)
12799 {
12800 rettv->v_type = VAR_LIST;
12801 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12802 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012803 if (rettv->vval.v_list != NULL)
12804 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012805 }
12806 else
12807 {
12808 rettv->v_type = VAR_STRING;
12809 rettv->vval.v_string = get_reg_contents(regname,
12810 arg2 ? GREG_EXPR_SRC : 0);
12811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812}
12813
12814/*
12815 * "getregtype()" function
12816 */
12817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012818f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819{
12820 char_u *strregname;
12821 int regname;
12822 char_u buf[NUMBUFLEN + 2];
12823 long reglen = 0;
12824
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012825 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012826 {
12827 strregname = get_tv_string_chk(&argvars[0]);
12828 if (strregname == NULL) /* type error; errmsg already given */
12829 {
12830 rettv->v_type = VAR_STRING;
12831 rettv->vval.v_string = NULL;
12832 return;
12833 }
12834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835 else
12836 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012837 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838
12839 regname = (strregname == NULL ? '"' : *strregname);
12840 if (regname == 0)
12841 regname = '"';
12842
12843 buf[0] = NUL;
12844 buf[1] = NUL;
12845 switch (get_reg_type(regname, &reglen))
12846 {
12847 case MLINE: buf[0] = 'V'; break;
12848 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849 case MBLOCK:
12850 buf[0] = Ctrl_V;
12851 sprintf((char *)buf + 1, "%ld", reglen + 1);
12852 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012853 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012854 rettv->v_type = VAR_STRING;
12855 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856}
12857
12858/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012859 * "gettabvar()" function
12860 */
12861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012862f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012863{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012864 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012865 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012866 dictitem_T *v;
12867 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012868 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012869
12870 rettv->v_type = VAR_STRING;
12871 rettv->vval.v_string = NULL;
12872
12873 varname = get_tv_string_chk(&argvars[1]);
12874 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12875 if (tp != NULL && varname != NULL)
12876 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012877 /* Set tp to be our tabpage, temporarily. Also set the window to the
12878 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012879 if (switch_win(&oldcurwin, &oldtabpage,
12880 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012881 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012882 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012883 /* look up the variable */
12884 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12885 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12886 if (v != NULL)
12887 {
12888 copy_tv(&v->di_tv, rettv);
12889 done = TRUE;
12890 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012891 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012892
12893 /* restore previous notion of curwin */
12894 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012895 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012896
12897 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012898 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012899}
12900
12901/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012902 * "gettabwinvar()" function
12903 */
12904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012905f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012906{
12907 getwinvar(argvars, rettv, 1);
12908}
12909
12910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911 * "getwinposx()" function
12912 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012914f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012915{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012916 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917#ifdef FEAT_GUI
12918 if (gui.in_use)
12919 {
12920 int x, y;
12921
12922 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012923 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012924 }
12925#endif
12926}
12927
12928/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010012929 * "win_findbuf()" function
12930 */
12931 static void
12932f_win_findbuf(typval_T *argvars, typval_T *rettv)
12933{
12934 if (rettv_list_alloc(rettv) != FAIL)
12935 win_findbuf(argvars, rettv->vval.v_list);
12936}
12937
12938/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010012939 * "win_getid()" function
12940 */
12941 static void
12942f_win_getid(typval_T *argvars, typval_T *rettv)
12943{
12944 rettv->vval.v_number = win_getid(argvars);
12945}
12946
12947/*
12948 * "win_gotoid()" function
12949 */
12950 static void
12951f_win_gotoid(typval_T *argvars, typval_T *rettv)
12952{
12953 rettv->vval.v_number = win_gotoid(argvars);
12954}
12955
12956/*
12957 * "win_id2tabwin()" function
12958 */
12959 static void
12960f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
12961{
12962 if (rettv_list_alloc(rettv) != FAIL)
12963 win_id2tabwin(argvars, rettv->vval.v_list);
12964}
12965
12966/*
12967 * "win_id2win()" function
12968 */
12969 static void
12970f_win_id2win(typval_T *argvars, typval_T *rettv)
12971{
12972 rettv->vval.v_number = win_id2win(argvars);
12973}
12974
12975/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012976 * "getwinposy()" function
12977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012979f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012980{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012981 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012982#ifdef FEAT_GUI
12983 if (gui.in_use)
12984 {
12985 int x, y;
12986
12987 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012988 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989 }
12990#endif
12991}
12992
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012993/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012994 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012995 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012996 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012997find_win_by_nr(
12998 typval_T *vp,
12999 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013000{
13001#ifdef FEAT_WINDOWS
13002 win_T *wp;
13003#endif
13004 int nr;
13005
13006 nr = get_tv_number_chk(vp, NULL);
13007
13008#ifdef FEAT_WINDOWS
13009 if (nr < 0)
13010 return NULL;
13011 if (nr == 0)
13012 return curwin;
13013
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013014 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13015 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013016 if (--nr <= 0)
13017 break;
13018 return wp;
13019#else
13020 if (nr == 0 || nr == 1)
13021 return curwin;
13022 return NULL;
13023#endif
13024}
13025
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013027 * Find window specified by "wvp" in tabpage "tvp".
13028 */
13029 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013030find_tabwin(
13031 typval_T *wvp, /* VAR_UNKNOWN for current window */
13032 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013033{
13034 win_T *wp = NULL;
13035 tabpage_T *tp = NULL;
13036 long n;
13037
13038 if (wvp->v_type != VAR_UNKNOWN)
13039 {
13040 if (tvp->v_type != VAR_UNKNOWN)
13041 {
13042 n = get_tv_number(tvp);
13043 if (n >= 0)
13044 tp = find_tabpage(n);
13045 }
13046 else
13047 tp = curtab;
13048
13049 if (tp != NULL)
13050 wp = find_win_by_nr(wvp, tp);
13051 }
13052 else
13053 wp = curwin;
13054
13055 return wp;
13056}
13057
13058/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059 * "getwinvar()" function
13060 */
13061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013062f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013063{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013064 getwinvar(argvars, rettv, 0);
13065}
13066
13067/*
13068 * getwinvar() and gettabwinvar()
13069 */
13070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013071getwinvar(
13072 typval_T *argvars,
13073 typval_T *rettv,
13074 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013075{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013076 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013077 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013078 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013079 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013080 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013081#ifdef FEAT_WINDOWS
13082 win_T *oldcurwin;
13083 tabpage_T *oldtabpage;
13084 int need_switch_win;
13085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013087#ifdef FEAT_WINDOWS
13088 if (off == 1)
13089 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13090 else
13091 tp = curtab;
13092#endif
13093 win = find_win_by_nr(&argvars[off], tp);
13094 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013095 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013097 rettv->v_type = VAR_STRING;
13098 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099
13100 if (win != NULL && varname != NULL)
13101 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013102#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013103 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013104 * otherwise the window is not valid. Only do this when needed,
13105 * autocommands get blocked. */
13106 need_switch_win = !(tp == curtab && win == curwin);
13107 if (!need_switch_win
13108 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13109#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013110 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013111 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013112 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013113 if (get_option_tv(&varname, rettv, 1) == OK)
13114 done = TRUE;
13115 }
13116 else
13117 {
13118 /* Look up the variable. */
13119 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13120 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13121 varname, FALSE);
13122 if (v != NULL)
13123 {
13124 copy_tv(&v->di_tv, rettv);
13125 done = TRUE;
13126 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013128 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013129
Bram Moolenaarba117c22015-09-29 16:53:22 +020013130#ifdef FEAT_WINDOWS
13131 if (need_switch_win)
13132 /* restore previous notion of curwin */
13133 restore_win(oldcurwin, oldtabpage, TRUE);
13134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135 }
13136
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013137 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13138 /* use the default return value */
13139 copy_tv(&argvars[off + 2], rettv);
13140
Bram Moolenaar071d4272004-06-13 20:20:40 +000013141 --emsg_off;
13142}
13143
13144/*
13145 * "glob()" function
13146 */
13147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013148f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013150 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013151 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013152 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013153
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013154 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013155 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013156 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013157 if (argvars[1].v_type != VAR_UNKNOWN)
13158 {
13159 if (get_tv_number_chk(&argvars[1], &error))
13160 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013161 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013162 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013163 if (get_tv_number_chk(&argvars[2], &error))
13164 {
13165 rettv->v_type = VAR_LIST;
13166 rettv->vval.v_list = NULL;
13167 }
13168 if (argvars[3].v_type != VAR_UNKNOWN
13169 && get_tv_number_chk(&argvars[3], &error))
13170 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013171 }
13172 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013173 if (!error)
13174 {
13175 ExpandInit(&xpc);
13176 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013177 if (p_wic)
13178 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013179 if (rettv->v_type == VAR_STRING)
13180 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013181 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013182 else if (rettv_list_alloc(rettv) != FAIL)
13183 {
13184 int i;
13185
13186 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13187 NULL, options, WILD_ALL_KEEP);
13188 for (i = 0; i < xpc.xp_numfiles; i++)
13189 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13190
13191 ExpandCleanup(&xpc);
13192 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013193 }
13194 else
13195 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196}
13197
13198/*
13199 * "globpath()" function
13200 */
13201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013202f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013203{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013204 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013206 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013207 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013208 garray_T ga;
13209 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013211 /* When the optional second argument is non-zero, don't remove matches
13212 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013213 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013214 if (argvars[2].v_type != VAR_UNKNOWN)
13215 {
13216 if (get_tv_number_chk(&argvars[2], &error))
13217 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013218 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013219 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013220 if (get_tv_number_chk(&argvars[3], &error))
13221 {
13222 rettv->v_type = VAR_LIST;
13223 rettv->vval.v_list = NULL;
13224 }
13225 if (argvars[4].v_type != VAR_UNKNOWN
13226 && get_tv_number_chk(&argvars[4], &error))
13227 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013228 }
13229 }
13230 if (file != NULL && !error)
13231 {
13232 ga_init2(&ga, (int)sizeof(char_u *), 10);
13233 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13234 if (rettv->v_type == VAR_STRING)
13235 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13236 else if (rettv_list_alloc(rettv) != FAIL)
13237 for (i = 0; i < ga.ga_len; ++i)
13238 list_append_string(rettv->vval.v_list,
13239 ((char_u **)(ga.ga_data))[i], -1);
13240 ga_clear_strings(&ga);
13241 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013242 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013243 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244}
13245
13246/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013247 * "glob2regpat()" function
13248 */
13249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013250f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013251{
13252 char_u *pat = get_tv_string_chk(&argvars[0]);
13253
13254 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013255 rettv->vval.v_string = (pat == NULL)
13256 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013257}
13258
13259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260 * "has()" function
13261 */
13262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013263f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013264{
13265 int i;
13266 char_u *name;
13267 int n = FALSE;
13268 static char *(has_list[]) =
13269 {
13270#ifdef AMIGA
13271 "amiga",
13272# ifdef FEAT_ARP
13273 "arp",
13274# endif
13275#endif
13276#ifdef __BEOS__
13277 "beos",
13278#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013279#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280 "mac",
13281#endif
13282#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013283 "macunix", /* built with 'darwin' enabled */
13284#endif
13285#if defined(__APPLE__) && __APPLE__ == 1
13286 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013288#ifdef __QNX__
13289 "qnx",
13290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013291#ifdef UNIX
13292 "unix",
13293#endif
13294#ifdef VMS
13295 "vms",
13296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297#ifdef WIN32
13298 "win32",
13299#endif
13300#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13301 "win32unix",
13302#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013303#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304 "win64",
13305#endif
13306#ifdef EBCDIC
13307 "ebcdic",
13308#endif
13309#ifndef CASE_INSENSITIVE_FILENAME
13310 "fname_case",
13311#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013312#ifdef HAVE_ACL
13313 "acl",
13314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315#ifdef FEAT_ARABIC
13316 "arabic",
13317#endif
13318#ifdef FEAT_AUTOCMD
13319 "autocmd",
13320#endif
13321#ifdef FEAT_BEVAL
13322 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013323# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13324 "balloon_multiline",
13325# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013326#endif
13327#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13328 "builtin_terms",
13329# ifdef ALL_BUILTIN_TCAPS
13330 "all_builtin_terms",
13331# endif
13332#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013333#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13334 || defined(FEAT_GUI_W32) \
13335 || defined(FEAT_GUI_MOTIF))
13336 "browsefilter",
13337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338#ifdef FEAT_BYTEOFF
13339 "byte_offset",
13340#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013341#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013342 "channel",
13343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344#ifdef FEAT_CINDENT
13345 "cindent",
13346#endif
13347#ifdef FEAT_CLIENTSERVER
13348 "clientserver",
13349#endif
13350#ifdef FEAT_CLIPBOARD
13351 "clipboard",
13352#endif
13353#ifdef FEAT_CMDL_COMPL
13354 "cmdline_compl",
13355#endif
13356#ifdef FEAT_CMDHIST
13357 "cmdline_hist",
13358#endif
13359#ifdef FEAT_COMMENTS
13360 "comments",
13361#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013362#ifdef FEAT_CONCEAL
13363 "conceal",
13364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013365#ifdef FEAT_CRYPT
13366 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013367 "crypt-blowfish",
13368 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369#endif
13370#ifdef FEAT_CSCOPE
13371 "cscope",
13372#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013373#ifdef FEAT_CURSORBIND
13374 "cursorbind",
13375#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013376#ifdef CURSOR_SHAPE
13377 "cursorshape",
13378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379#ifdef DEBUG
13380 "debug",
13381#endif
13382#ifdef FEAT_CON_DIALOG
13383 "dialog_con",
13384#endif
13385#ifdef FEAT_GUI_DIALOG
13386 "dialog_gui",
13387#endif
13388#ifdef FEAT_DIFF
13389 "diff",
13390#endif
13391#ifdef FEAT_DIGRAPHS
13392 "digraphs",
13393#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013394#ifdef FEAT_DIRECTX
13395 "directx",
13396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013397#ifdef FEAT_DND
13398 "dnd",
13399#endif
13400#ifdef FEAT_EMACS_TAGS
13401 "emacs_tags",
13402#endif
13403 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013404 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013405#ifdef FEAT_SEARCH_EXTRA
13406 "extra_search",
13407#endif
13408#ifdef FEAT_FKMAP
13409 "farsi",
13410#endif
13411#ifdef FEAT_SEARCHPATH
13412 "file_in_path",
13413#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013414#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013415 "filterpipe",
13416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013417#ifdef FEAT_FIND_ID
13418 "find_in_path",
13419#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013420#ifdef FEAT_FLOAT
13421 "float",
13422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423#ifdef FEAT_FOLDING
13424 "folding",
13425#endif
13426#ifdef FEAT_FOOTER
13427 "footer",
13428#endif
13429#if !defined(USE_SYSTEM) && defined(UNIX)
13430 "fork",
13431#endif
13432#ifdef FEAT_GETTEXT
13433 "gettext",
13434#endif
13435#ifdef FEAT_GUI
13436 "gui",
13437#endif
13438#ifdef FEAT_GUI_ATHENA
13439# ifdef FEAT_GUI_NEXTAW
13440 "gui_neXtaw",
13441# else
13442 "gui_athena",
13443# endif
13444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445#ifdef FEAT_GUI_GTK
13446 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013447# ifdef USE_GTK3
13448 "gui_gtk3",
13449# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013451# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013453#ifdef FEAT_GUI_GNOME
13454 "gui_gnome",
13455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013456#ifdef FEAT_GUI_MAC
13457 "gui_mac",
13458#endif
13459#ifdef FEAT_GUI_MOTIF
13460 "gui_motif",
13461#endif
13462#ifdef FEAT_GUI_PHOTON
13463 "gui_photon",
13464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465#ifdef FEAT_GUI_W32
13466 "gui_win32",
13467#endif
13468#ifdef FEAT_HANGULIN
13469 "hangul_input",
13470#endif
13471#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13472 "iconv",
13473#endif
13474#ifdef FEAT_INS_EXPAND
13475 "insert_expand",
13476#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013477#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013478 "job",
13479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480#ifdef FEAT_JUMPLIST
13481 "jumplist",
13482#endif
13483#ifdef FEAT_KEYMAP
13484 "keymap",
13485#endif
13486#ifdef FEAT_LANGMAP
13487 "langmap",
13488#endif
13489#ifdef FEAT_LIBCALL
13490 "libcall",
13491#endif
13492#ifdef FEAT_LINEBREAK
13493 "linebreak",
13494#endif
13495#ifdef FEAT_LISP
13496 "lispindent",
13497#endif
13498#ifdef FEAT_LISTCMDS
13499 "listcmds",
13500#endif
13501#ifdef FEAT_LOCALMAP
13502 "localmap",
13503#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013504#ifdef FEAT_LUA
13505# ifndef DYNAMIC_LUA
13506 "lua",
13507# endif
13508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509#ifdef FEAT_MENU
13510 "menu",
13511#endif
13512#ifdef FEAT_SESSION
13513 "mksession",
13514#endif
13515#ifdef FEAT_MODIFY_FNAME
13516 "modify_fname",
13517#endif
13518#ifdef FEAT_MOUSE
13519 "mouse",
13520#endif
13521#ifdef FEAT_MOUSESHAPE
13522 "mouseshape",
13523#endif
13524#if defined(UNIX) || defined(VMS)
13525# ifdef FEAT_MOUSE_DEC
13526 "mouse_dec",
13527# endif
13528# ifdef FEAT_MOUSE_GPM
13529 "mouse_gpm",
13530# endif
13531# ifdef FEAT_MOUSE_JSB
13532 "mouse_jsbterm",
13533# endif
13534# ifdef FEAT_MOUSE_NET
13535 "mouse_netterm",
13536# endif
13537# ifdef FEAT_MOUSE_PTERM
13538 "mouse_pterm",
13539# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013540# ifdef FEAT_MOUSE_SGR
13541 "mouse_sgr",
13542# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013543# ifdef FEAT_SYSMOUSE
13544 "mouse_sysmouse",
13545# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013546# ifdef FEAT_MOUSE_URXVT
13547 "mouse_urxvt",
13548# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549# ifdef FEAT_MOUSE_XTERM
13550 "mouse_xterm",
13551# endif
13552#endif
13553#ifdef FEAT_MBYTE
13554 "multi_byte",
13555#endif
13556#ifdef FEAT_MBYTE_IME
13557 "multi_byte_ime",
13558#endif
13559#ifdef FEAT_MULTI_LANG
13560 "multi_lang",
13561#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013562#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013563#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013564 "mzscheme",
13565#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567#ifdef FEAT_OLE
13568 "ole",
13569#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013570 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571#ifdef FEAT_PATH_EXTRA
13572 "path_extra",
13573#endif
13574#ifdef FEAT_PERL
13575#ifndef DYNAMIC_PERL
13576 "perl",
13577#endif
13578#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013579#ifdef FEAT_PERSISTENT_UNDO
13580 "persistent_undo",
13581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582#ifdef FEAT_PYTHON
13583#ifndef DYNAMIC_PYTHON
13584 "python",
13585#endif
13586#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013587#ifdef FEAT_PYTHON3
13588#ifndef DYNAMIC_PYTHON3
13589 "python3",
13590#endif
13591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592#ifdef FEAT_POSTSCRIPT
13593 "postscript",
13594#endif
13595#ifdef FEAT_PRINTER
13596 "printer",
13597#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013598#ifdef FEAT_PROFILE
13599 "profile",
13600#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013601#ifdef FEAT_RELTIME
13602 "reltime",
13603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604#ifdef FEAT_QUICKFIX
13605 "quickfix",
13606#endif
13607#ifdef FEAT_RIGHTLEFT
13608 "rightleft",
13609#endif
13610#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13611 "ruby",
13612#endif
13613#ifdef FEAT_SCROLLBIND
13614 "scrollbind",
13615#endif
13616#ifdef FEAT_CMDL_INFO
13617 "showcmd",
13618 "cmdline_info",
13619#endif
13620#ifdef FEAT_SIGNS
13621 "signs",
13622#endif
13623#ifdef FEAT_SMARTINDENT
13624 "smartindent",
13625#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013626#ifdef STARTUPTIME
13627 "startuptime",
13628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629#ifdef FEAT_STL_OPT
13630 "statusline",
13631#endif
13632#ifdef FEAT_SUN_WORKSHOP
13633 "sun_workshop",
13634#endif
13635#ifdef FEAT_NETBEANS_INTG
13636 "netbeans_intg",
13637#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013638#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013639 "spell",
13640#endif
13641#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642 "syntax",
13643#endif
13644#if defined(USE_SYSTEM) || !defined(UNIX)
13645 "system",
13646#endif
13647#ifdef FEAT_TAG_BINS
13648 "tag_binary",
13649#endif
13650#ifdef FEAT_TAG_OLDSTATIC
13651 "tag_old_static",
13652#endif
13653#ifdef FEAT_TAG_ANYWHITE
13654 "tag_any_white",
13655#endif
13656#ifdef FEAT_TCL
13657# ifndef DYNAMIC_TCL
13658 "tcl",
13659# endif
13660#endif
13661#ifdef TERMINFO
13662 "terminfo",
13663#endif
13664#ifdef FEAT_TERMRESPONSE
13665 "termresponse",
13666#endif
13667#ifdef FEAT_TEXTOBJ
13668 "textobjects",
13669#endif
13670#ifdef HAVE_TGETENT
13671 "tgetent",
13672#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010013673#ifdef FEAT_TIMERS
13674 "timers",
13675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676#ifdef FEAT_TITLE
13677 "title",
13678#endif
13679#ifdef FEAT_TOOLBAR
13680 "toolbar",
13681#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013682#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13683 "unnamedplus",
13684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685#ifdef FEAT_USR_CMDS
13686 "user-commands", /* was accidentally included in 5.4 */
13687 "user_commands",
13688#endif
13689#ifdef FEAT_VIMINFO
13690 "viminfo",
13691#endif
13692#ifdef FEAT_VERTSPLIT
13693 "vertsplit",
13694#endif
13695#ifdef FEAT_VIRTUALEDIT
13696 "virtualedit",
13697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013698 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699#ifdef FEAT_VISUALEXTRA
13700 "visualextra",
13701#endif
13702#ifdef FEAT_VREPLACE
13703 "vreplace",
13704#endif
13705#ifdef FEAT_WILDIGN
13706 "wildignore",
13707#endif
13708#ifdef FEAT_WILDMENU
13709 "wildmenu",
13710#endif
13711#ifdef FEAT_WINDOWS
13712 "windows",
13713#endif
13714#ifdef FEAT_WAK
13715 "winaltkeys",
13716#endif
13717#ifdef FEAT_WRITEBACKUP
13718 "writebackup",
13719#endif
13720#ifdef FEAT_XIM
13721 "xim",
13722#endif
13723#ifdef FEAT_XFONTSET
13724 "xfontset",
13725#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013726#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013727 "xpm",
13728 "xpm_w32", /* for backward compatibility */
13729#else
13730# if defined(HAVE_XPM)
13731 "xpm",
13732# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734#ifdef USE_XSMP
13735 "xsmp",
13736#endif
13737#ifdef USE_XSMP_INTERACT
13738 "xsmp_interact",
13739#endif
13740#ifdef FEAT_XCLIPBOARD
13741 "xterm_clipboard",
13742#endif
13743#ifdef FEAT_XTERM_SAVE
13744 "xterm_save",
13745#endif
13746#if defined(UNIX) && defined(FEAT_X11)
13747 "X11",
13748#endif
13749 NULL
13750 };
13751
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013752 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753 for (i = 0; has_list[i] != NULL; ++i)
13754 if (STRICMP(name, has_list[i]) == 0)
13755 {
13756 n = TRUE;
13757 break;
13758 }
13759
13760 if (n == FALSE)
13761 {
13762 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013763 {
13764 if (name[5] == '-'
13765 && STRLEN(name) > 11
13766 && vim_isdigit(name[6])
13767 && vim_isdigit(name[8])
13768 && vim_isdigit(name[10]))
13769 {
13770 int major = atoi((char *)name + 6);
13771 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013772
13773 /* Expect "patch-9.9.01234". */
13774 n = (major < VIM_VERSION_MAJOR
13775 || (major == VIM_VERSION_MAJOR
13776 && (minor < VIM_VERSION_MINOR
13777 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013778 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013779 }
13780 else
13781 n = has_patch(atoi((char *)name + 5));
13782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013783 else if (STRICMP(name, "vim_starting") == 0)
13784 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013785#ifdef FEAT_MBYTE
13786 else if (STRICMP(name, "multi_byte_encoding") == 0)
13787 n = has_mbyte;
13788#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013789#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13790 else if (STRICMP(name, "balloon_multiline") == 0)
13791 n = multiline_balloon_available();
13792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013793#ifdef DYNAMIC_TCL
13794 else if (STRICMP(name, "tcl") == 0)
13795 n = tcl_enabled(FALSE);
13796#endif
13797#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13798 else if (STRICMP(name, "iconv") == 0)
13799 n = iconv_enabled(FALSE);
13800#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013801#ifdef DYNAMIC_LUA
13802 else if (STRICMP(name, "lua") == 0)
13803 n = lua_enabled(FALSE);
13804#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013805#ifdef DYNAMIC_MZSCHEME
13806 else if (STRICMP(name, "mzscheme") == 0)
13807 n = mzscheme_enabled(FALSE);
13808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809#ifdef DYNAMIC_RUBY
13810 else if (STRICMP(name, "ruby") == 0)
13811 n = ruby_enabled(FALSE);
13812#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013813#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814#ifdef DYNAMIC_PYTHON
13815 else if (STRICMP(name, "python") == 0)
13816 n = python_enabled(FALSE);
13817#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013818#endif
13819#ifdef FEAT_PYTHON3
13820#ifdef DYNAMIC_PYTHON3
13821 else if (STRICMP(name, "python3") == 0)
13822 n = python3_enabled(FALSE);
13823#endif
13824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825#ifdef DYNAMIC_PERL
13826 else if (STRICMP(name, "perl") == 0)
13827 n = perl_enabled(FALSE);
13828#endif
13829#ifdef FEAT_GUI
13830 else if (STRICMP(name, "gui_running") == 0)
13831 n = (gui.in_use || gui.starting);
13832# ifdef FEAT_GUI_W32
13833 else if (STRICMP(name, "gui_win32s") == 0)
13834 n = gui_is_win32s();
13835# endif
13836# ifdef FEAT_BROWSE
13837 else if (STRICMP(name, "browse") == 0)
13838 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13839# endif
13840#endif
13841#ifdef FEAT_SYN_HL
13842 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013843 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844#endif
13845#if defined(WIN3264)
13846 else if (STRICMP(name, "win95") == 0)
13847 n = mch_windows95();
13848#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013849#ifdef FEAT_NETBEANS_INTG
13850 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013851 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853 }
13854
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013855 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856}
13857
13858/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013859 * "has_key()" function
13860 */
13861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013862f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013863{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013864 if (argvars[0].v_type != VAR_DICT)
13865 {
13866 EMSG(_(e_dictreq));
13867 return;
13868 }
13869 if (argvars[0].vval.v_dict == NULL)
13870 return;
13871
13872 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013873 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013874}
13875
13876/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013877 * "haslocaldir()" function
13878 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013880f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013881{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013882 win_T *wp = NULL;
13883
13884 wp = find_tabwin(&argvars[0], &argvars[1]);
13885 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013886}
13887
13888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889 * "hasmapto()" function
13890 */
13891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013892f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013893{
13894 char_u *name;
13895 char_u *mode;
13896 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013897 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013899 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013900 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901 mode = (char_u *)"nvo";
13902 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013904 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013905 if (argvars[2].v_type != VAR_UNKNOWN)
13906 abbr = get_tv_number(&argvars[2]);
13907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013908
Bram Moolenaar2c932302006-03-18 21:42:09 +000013909 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013910 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013912 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913}
13914
13915/*
13916 * "histadd()" function
13917 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013919f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013920{
13921#ifdef FEAT_CMDHIST
13922 int histype;
13923 char_u *str;
13924 char_u buf[NUMBUFLEN];
13925#endif
13926
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013927 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928 if (check_restricted() || check_secure())
13929 return;
13930#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013931 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13932 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933 if (histype >= 0)
13934 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013935 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936 if (*str != NUL)
13937 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013938 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013940 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941 return;
13942 }
13943 }
13944#endif
13945}
13946
13947/*
13948 * "histdel()" function
13949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013951f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013952{
13953#ifdef FEAT_CMDHIST
13954 int n;
13955 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013956 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013958 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13959 if (str == NULL)
13960 n = 0;
13961 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013963 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013964 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013966 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013967 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968 else
13969 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013970 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013971 get_tv_string_buf(&argvars[1], buf));
13972 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973#endif
13974}
13975
13976/*
13977 * "histget()" function
13978 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013980f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013981{
13982#ifdef FEAT_CMDHIST
13983 int type;
13984 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013985 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013987 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13988 if (str == NULL)
13989 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013991 {
13992 type = get_histtype(str);
13993 if (argvars[1].v_type == VAR_UNKNOWN)
13994 idx = get_history_idx(type);
13995 else
13996 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13997 /* -1 on type error */
13998 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014001 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014003 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014004}
14005
14006/*
14007 * "histnr()" function
14008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014010f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011{
14012 int i;
14013
14014#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014015 char_u *history = get_tv_string_chk(&argvars[0]);
14016
14017 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018 if (i >= HIST_CMD && i < HIST_COUNT)
14019 i = get_history_idx(i);
14020 else
14021#endif
14022 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014023 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024}
14025
14026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014027 * "highlightID(name)" function
14028 */
14029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014030f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014032 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033}
14034
14035/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014036 * "highlight_exists()" function
14037 */
14038 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014039f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014040{
14041 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14042}
14043
14044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014045 * "hostname()" function
14046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014048f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049{
14050 char_u hostname[256];
14051
14052 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014053 rettv->v_type = VAR_STRING;
14054 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055}
14056
14057/*
14058 * iconv() function
14059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014061f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062{
14063#ifdef FEAT_MBYTE
14064 char_u buf1[NUMBUFLEN];
14065 char_u buf2[NUMBUFLEN];
14066 char_u *from, *to, *str;
14067 vimconv_T vimconv;
14068#endif
14069
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014070 rettv->v_type = VAR_STRING;
14071 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014072
14073#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014074 str = get_tv_string(&argvars[0]);
14075 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14076 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014077 vimconv.vc_type = CONV_NONE;
14078 convert_setup(&vimconv, from, to);
14079
14080 /* If the encodings are equal, no conversion needed. */
14081 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014082 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014084 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014085
14086 convert_setup(&vimconv, NULL, NULL);
14087 vim_free(from);
14088 vim_free(to);
14089#endif
14090}
14091
14092/*
14093 * "indent()" function
14094 */
14095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014096f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097{
14098 linenr_T lnum;
14099
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014100 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014101 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014102 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014103 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014104 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105}
14106
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014107/*
14108 * "index()" function
14109 */
14110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014111f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014112{
Bram Moolenaar33570922005-01-25 22:26:29 +000014113 list_T *l;
14114 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014115 long idx = 0;
14116 int ic = FALSE;
14117
14118 rettv->vval.v_number = -1;
14119 if (argvars[0].v_type != VAR_LIST)
14120 {
14121 EMSG(_(e_listreq));
14122 return;
14123 }
14124 l = argvars[0].vval.v_list;
14125 if (l != NULL)
14126 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014127 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014128 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014129 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014130 int error = FALSE;
14131
Bram Moolenaar758711c2005-02-02 23:11:38 +000014132 /* Start at specified item. Use the cached index that list_find()
14133 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014134 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014135 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014136 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014137 ic = get_tv_number_chk(&argvars[3], &error);
14138 if (error)
14139 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014140 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014141
Bram Moolenaar758711c2005-02-02 23:11:38 +000014142 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014143 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014144 {
14145 rettv->vval.v_number = idx;
14146 break;
14147 }
14148 }
14149}
14150
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151static int inputsecret_flag = 0;
14152
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014153static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014154
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014156 * This function is used by f_input() and f_inputdialog() functions. The third
14157 * argument to f_input() specifies the type of completion to use at the
14158 * prompt. The third argument to f_inputdialog() specifies the value to return
14159 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160 */
14161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014162get_user_input(
14163 typval_T *argvars,
14164 typval_T *rettv,
14165 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014166{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014167 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014168 char_u *p = NULL;
14169 int c;
14170 char_u buf[NUMBUFLEN];
14171 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014172 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014173 int xp_type = EXPAND_NOTHING;
14174 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014176 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014177 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178
14179#ifdef NO_CONSOLE_INPUT
14180 /* While starting up, there is no place to enter text. */
14181 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014182 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014183#endif
14184
14185 cmd_silent = FALSE; /* Want to see the prompt. */
14186 if (prompt != NULL)
14187 {
14188 /* Only the part of the message after the last NL is considered as
14189 * prompt for the command line */
14190 p = vim_strrchr(prompt, '\n');
14191 if (p == NULL)
14192 p = prompt;
14193 else
14194 {
14195 ++p;
14196 c = *p;
14197 *p = NUL;
14198 msg_start();
14199 msg_clr_eos();
14200 msg_puts_attr(prompt, echo_attr);
14201 msg_didout = FALSE;
14202 msg_starthere();
14203 *p = c;
14204 }
14205 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014206
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014207 if (argvars[1].v_type != VAR_UNKNOWN)
14208 {
14209 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14210 if (defstr != NULL)
14211 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014212
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014213 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014214 {
14215 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014216 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014217 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014218
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014219 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014220 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014221
Bram Moolenaar4463f292005-09-25 22:20:24 +000014222 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14223 if (xp_name == NULL)
14224 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014225
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014226 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014227
Bram Moolenaar4463f292005-09-25 22:20:24 +000014228 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14229 &xp_arg) == FAIL)
14230 return;
14231 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014232 }
14233
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014234 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014235 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014236 int save_ex_normal_busy = ex_normal_busy;
14237 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014238 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014239 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14240 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014241 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014242 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014243 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014244 && argvars[1].v_type != VAR_UNKNOWN
14245 && argvars[2].v_type != VAR_UNKNOWN)
14246 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14247 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014248
14249 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014250
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014251 /* since the user typed this, no need to wait for return */
14252 need_wait_return = FALSE;
14253 msg_didout = FALSE;
14254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255 cmd_silent = cmd_silent_save;
14256}
14257
14258/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014259 * "input()" function
14260 * Also handles inputsecret() when inputsecret is set.
14261 */
14262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014263f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014264{
14265 get_user_input(argvars, rettv, FALSE);
14266}
14267
14268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269 * "inputdialog()" function
14270 */
14271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014272f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273{
14274#if defined(FEAT_GUI_TEXTDIALOG)
14275 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14276 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14277 {
14278 char_u *message;
14279 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014280 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014282 message = get_tv_string_chk(&argvars[0]);
14283 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014284 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014285 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286 else
14287 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014288 if (message != NULL && defstr != NULL
14289 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014290 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014291 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014292 else
14293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014294 if (message != NULL && defstr != NULL
14295 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014296 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014297 rettv->vval.v_string = vim_strsave(
14298 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014299 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014300 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014302 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014303 }
14304 else
14305#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014306 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014307}
14308
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014309/*
14310 * "inputlist()" function
14311 */
14312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014313f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014314{
14315 listitem_T *li;
14316 int selected;
14317 int mouse_used;
14318
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014319#ifdef NO_CONSOLE_INPUT
14320 /* While starting up, there is no place to enter text. */
14321 if (no_console_input())
14322 return;
14323#endif
14324 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14325 {
14326 EMSG2(_(e_listarg), "inputlist()");
14327 return;
14328 }
14329
14330 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014331 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014332 lines_left = Rows; /* avoid more prompt */
14333 msg_scroll = TRUE;
14334 msg_clr_eos();
14335
14336 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14337 {
14338 msg_puts(get_tv_string(&li->li_tv));
14339 msg_putchar('\n');
14340 }
14341
14342 /* Ask for choice. */
14343 selected = prompt_for_number(&mouse_used);
14344 if (mouse_used)
14345 selected -= lines_left;
14346
14347 rettv->vval.v_number = selected;
14348}
14349
14350
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14352
14353/*
14354 * "inputrestore()" function
14355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014357f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358{
14359 if (ga_userinput.ga_len > 0)
14360 {
14361 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14363 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014364 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365 }
14366 else if (p_verbose > 1)
14367 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014368 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014369 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370 }
14371}
14372
14373/*
14374 * "inputsave()" function
14375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014377f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014379 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014380 if (ga_grow(&ga_userinput, 1) == OK)
14381 {
14382 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14383 + ga_userinput.ga_len);
14384 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014385 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386 }
14387 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014388 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014389}
14390
14391/*
14392 * "inputsecret()" function
14393 */
14394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014395f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396{
14397 ++cmdline_star;
14398 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014399 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400 --cmdline_star;
14401 --inputsecret_flag;
14402}
14403
14404/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014405 * "insert()" function
14406 */
14407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014408f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014409{
14410 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014411 listitem_T *item;
14412 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014413 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014414
14415 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014416 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014417 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014418 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014419 {
14420 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014421 before = get_tv_number_chk(&argvars[2], &error);
14422 if (error)
14423 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014424
Bram Moolenaar758711c2005-02-02 23:11:38 +000014425 if (before == l->lv_len)
14426 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014427 else
14428 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014429 item = list_find(l, before);
14430 if (item == NULL)
14431 {
14432 EMSGN(_(e_listidx), before);
14433 l = NULL;
14434 }
14435 }
14436 if (l != NULL)
14437 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014438 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014439 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014440 }
14441 }
14442}
14443
14444/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014445 * "invert(expr)" function
14446 */
14447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014448f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014449{
14450 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14451}
14452
14453/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454 * "isdirectory()" function
14455 */
14456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014457f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014459 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014460}
14461
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014462/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014463 * "islocked()" function
14464 */
14465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014466f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014467{
14468 lval_T lv;
14469 char_u *end;
14470 dictitem_T *di;
14471
14472 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014473 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14474 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014475 if (end != NULL && lv.ll_name != NULL)
14476 {
14477 if (*end != NUL)
14478 EMSG(_(e_trailing));
14479 else
14480 {
14481 if (lv.ll_tv == NULL)
14482 {
14483 if (check_changedtick(lv.ll_name))
14484 rettv->vval.v_number = 1; /* always locked */
14485 else
14486 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014487 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014488 if (di != NULL)
14489 {
14490 /* Consider a variable locked when:
14491 * 1. the variable itself is locked
14492 * 2. the value of the variable is locked.
14493 * 3. the List or Dict value is locked.
14494 */
14495 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14496 || tv_islocked(&di->di_tv));
14497 }
14498 }
14499 }
14500 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014501 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014502 else if (lv.ll_newkey != NULL)
14503 EMSG2(_(e_dictkey), lv.ll_newkey);
14504 else if (lv.ll_list != NULL)
14505 /* List item. */
14506 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14507 else
14508 /* Dictionary item. */
14509 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14510 }
14511 }
14512
14513 clear_lval(&lv);
14514}
14515
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014516#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14517/*
14518 * "isnan()" function
14519 */
14520 static void
14521f_isnan(typval_T *argvars, typval_T *rettv)
14522{
14523 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14524 && isnan(argvars[0].vval.v_float);
14525}
14526#endif
14527
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014528static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014529
14530/*
14531 * Turn a dict into a list:
14532 * "what" == 0: list of keys
14533 * "what" == 1: list of values
14534 * "what" == 2: list of items
14535 */
14536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014537dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014538{
Bram Moolenaar33570922005-01-25 22:26:29 +000014539 list_T *l2;
14540 dictitem_T *di;
14541 hashitem_T *hi;
14542 listitem_T *li;
14543 listitem_T *li2;
14544 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014545 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014546
Bram Moolenaar8c711452005-01-14 21:53:12 +000014547 if (argvars[0].v_type != VAR_DICT)
14548 {
14549 EMSG(_(e_dictreq));
14550 return;
14551 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014552 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014553 return;
14554
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014555 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014556 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014557
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014558 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014559 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014560 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014561 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014562 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014563 --todo;
14564 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014565
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014566 li = listitem_alloc();
14567 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014568 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014569 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014570
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014571 if (what == 0)
14572 {
14573 /* keys() */
14574 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014575 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014576 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14577 }
14578 else if (what == 1)
14579 {
14580 /* values() */
14581 copy_tv(&di->di_tv, &li->li_tv);
14582 }
14583 else
14584 {
14585 /* items() */
14586 l2 = list_alloc();
14587 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014588 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014589 li->li_tv.vval.v_list = l2;
14590 if (l2 == NULL)
14591 break;
14592 ++l2->lv_refcount;
14593
14594 li2 = listitem_alloc();
14595 if (li2 == NULL)
14596 break;
14597 list_append(l2, li2);
14598 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014599 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014600 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14601
14602 li2 = listitem_alloc();
14603 if (li2 == NULL)
14604 break;
14605 list_append(l2, li2);
14606 copy_tv(&di->di_tv, &li2->li_tv);
14607 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014608 }
14609 }
14610}
14611
14612/*
14613 * "items(dict)" function
14614 */
14615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014616f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014617{
14618 dict_list(argvars, rettv, 2);
14619}
14620
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014621#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014622/*
14623 * Get the job from the argument.
14624 * Returns NULL if the job is invalid.
14625 */
14626 static job_T *
14627get_job_arg(typval_T *tv)
14628{
14629 job_T *job;
14630
14631 if (tv->v_type != VAR_JOB)
14632 {
14633 EMSG2(_(e_invarg2), get_tv_string(tv));
14634 return NULL;
14635 }
14636 job = tv->vval.v_job;
14637
14638 if (job == NULL)
14639 EMSG(_("E916: not a valid job"));
14640 return job;
14641}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014642
Bram Moolenaar835dc632016-02-07 14:27:38 +010014643/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014644 * "job_getchannel()" function
14645 */
14646 static void
14647f_job_getchannel(typval_T *argvars, typval_T *rettv)
14648{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014649 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014650
Bram Moolenaar65edff82016-02-21 16:40:11 +010014651 if (job != NULL)
14652 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014653 rettv->v_type = VAR_CHANNEL;
14654 rettv->vval.v_channel = job->jv_channel;
14655 if (job->jv_channel != NULL)
14656 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014657 }
14658}
14659
14660/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010014661 * "job_info()" function
14662 */
14663 static void
14664f_job_info(typval_T *argvars, typval_T *rettv)
14665{
14666 job_T *job = get_job_arg(&argvars[0]);
14667
14668 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
14669 job_info(job, rettv->vval.v_dict);
14670}
14671
14672/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014673 * "job_setoptions()" function
14674 */
14675 static void
14676f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14677{
14678 job_T *job = get_job_arg(&argvars[0]);
14679 jobopt_T opt;
14680
14681 if (job == NULL)
14682 return;
14683 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014684 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014685 return;
14686 job_set_options(job, &opt);
14687}
14688
14689/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014690 * "job_start()" function
14691 */
14692 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010014693f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014694{
Bram Moolenaar835dc632016-02-07 14:27:38 +010014695 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014696 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014697}
14698
14699/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014700 * "job_status()" function
14701 */
14702 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014703f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014704{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014705 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014706
Bram Moolenaar65edff82016-02-21 16:40:11 +010014707 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014708 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010014709 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010014710 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010014711 }
14712}
14713
14714/*
14715 * "job_stop()" function
14716 */
14717 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014718f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014719{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014720 job_T *job = get_job_arg(&argvars[0]);
14721
14722 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014723 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014724}
14725#endif
14726
Bram Moolenaar071d4272004-06-13 20:20:40 +000014727/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014728 * "join()" function
14729 */
14730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014731f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014732{
14733 garray_T ga;
14734 char_u *sep;
14735
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014736 if (argvars[0].v_type != VAR_LIST)
14737 {
14738 EMSG(_(e_listreq));
14739 return;
14740 }
14741 if (argvars[0].vval.v_list == NULL)
14742 return;
14743 if (argvars[1].v_type == VAR_UNKNOWN)
14744 sep = (char_u *)" ";
14745 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014746 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014747
14748 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014749
14750 if (sep != NULL)
14751 {
14752 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014753 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014754 ga_append(&ga, NUL);
14755 rettv->vval.v_string = (char_u *)ga.ga_data;
14756 }
14757 else
14758 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014759}
14760
14761/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014762 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014763 */
14764 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014765f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014766{
14767 js_read_T reader;
14768
14769 reader.js_buf = get_tv_string(&argvars[0]);
14770 reader.js_fill = NULL;
14771 reader.js_used = 0;
14772 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14773 EMSG(_(e_invarg));
14774}
14775
14776/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014777 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014778 */
14779 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014780f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014781{
14782 rettv->v_type = VAR_STRING;
14783 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14784}
14785
14786/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014787 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014788 */
14789 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014790f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014791{
14792 js_read_T reader;
14793
14794 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014795 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014796 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014797 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014798 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014799}
14800
14801/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014802 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014803 */
14804 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014805f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014806{
14807 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014808 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014809}
14810
14811/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014812 * "keys()" function
14813 */
14814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014815f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014816{
14817 dict_list(argvars, rettv, 0);
14818}
14819
14820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014821 * "last_buffer_nr()" function.
14822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014824f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014825{
14826 int n = 0;
14827 buf_T *buf;
14828
14829 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14830 if (n < buf->b_fnum)
14831 n = buf->b_fnum;
14832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014833 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014834}
14835
14836/*
14837 * "len()" function
14838 */
14839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014840f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014841{
14842 switch (argvars[0].v_type)
14843 {
14844 case VAR_STRING:
14845 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014846 rettv->vval.v_number = (varnumber_T)STRLEN(
14847 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014848 break;
14849 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014850 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014851 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014852 case VAR_DICT:
14853 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14854 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014855 case VAR_UNKNOWN:
14856 case VAR_SPECIAL:
14857 case VAR_FLOAT:
14858 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010014859 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014860 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014861 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014862 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014863 break;
14864 }
14865}
14866
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014867static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014868
14869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014870libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014871{
14872#ifdef FEAT_LIBCALL
14873 char_u *string_in;
14874 char_u **string_result;
14875 int nr_result;
14876#endif
14877
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014878 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014879 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014880 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014881
14882 if (check_restricted() || check_secure())
14883 return;
14884
14885#ifdef FEAT_LIBCALL
14886 /* The first two args must be strings, otherwise its meaningless */
14887 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14888 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014889 string_in = NULL;
14890 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014891 string_in = argvars[2].vval.v_string;
14892 if (type == VAR_NUMBER)
14893 string_result = NULL;
14894 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014895 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014896 if (mch_libcall(argvars[0].vval.v_string,
14897 argvars[1].vval.v_string,
14898 string_in,
14899 argvars[2].vval.v_number,
14900 string_result,
14901 &nr_result) == OK
14902 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014903 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014904 }
14905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014906}
14907
14908/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014909 * "libcall()" function
14910 */
14911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014912f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014913{
14914 libcall_common(argvars, rettv, VAR_STRING);
14915}
14916
14917/*
14918 * "libcallnr()" function
14919 */
14920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014921f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014922{
14923 libcall_common(argvars, rettv, VAR_NUMBER);
14924}
14925
14926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014927 * "line(string)" function
14928 */
14929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014930f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014931{
14932 linenr_T lnum = 0;
14933 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014934 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014935
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014936 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014937 if (fp != NULL)
14938 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014939 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014940}
14941
14942/*
14943 * "line2byte(lnum)" function
14944 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014946f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014947{
14948#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014949 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014950#else
14951 linenr_T lnum;
14952
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014953 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014954 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014955 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014956 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014957 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14958 if (rettv->vval.v_number >= 0)
14959 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960#endif
14961}
14962
14963/*
14964 * "lispindent(lnum)" function
14965 */
14966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014967f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014968{
14969#ifdef FEAT_LISP
14970 pos_T pos;
14971 linenr_T lnum;
14972
14973 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014974 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014975 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14976 {
14977 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014978 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014979 curwin->w_cursor = pos;
14980 }
14981 else
14982#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014983 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014984}
14985
14986/*
14987 * "localtime()" function
14988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014990f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014991{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014992 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014993}
14994
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014995static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014996
14997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014998get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014999{
15000 char_u *keys;
15001 char_u *which;
15002 char_u buf[NUMBUFLEN];
15003 char_u *keys_buf = NULL;
15004 char_u *rhs;
15005 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015006 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015007 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015008 mapblock_T *mp;
15009 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015010
15011 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015012 rettv->v_type = VAR_STRING;
15013 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015014
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015015 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015016 if (*keys == NUL)
15017 return;
15018
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015019 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015020 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015021 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015022 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015023 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015024 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015025 if (argvars[3].v_type != VAR_UNKNOWN)
15026 get_dict = get_tv_number(&argvars[3]);
15027 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029 else
15030 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015031 if (which == NULL)
15032 return;
15033
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034 mode = get_map_mode(&which, 0);
15035
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015036 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015037 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015038 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015039
15040 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015042 /* Return a string. */
15043 if (rhs != NULL)
15044 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015045
Bram Moolenaarbd743252010-10-20 21:23:33 +020015046 }
15047 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15048 {
15049 /* Return a dictionary. */
15050 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15051 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15052 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015053
Bram Moolenaarbd743252010-10-20 21:23:33 +020015054 dict_add_nr_str(dict, "lhs", 0L, lhs);
15055 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15056 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15057 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15058 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15059 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15060 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015061 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015062 dict_add_nr_str(dict, "mode", 0L, mapmode);
15063
15064 vim_free(lhs);
15065 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015066 }
15067}
15068
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015069#ifdef FEAT_FLOAT
15070/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015071 * "log()" function
15072 */
15073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015074f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015075{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015076 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015077
15078 rettv->v_type = VAR_FLOAT;
15079 if (get_float_arg(argvars, &f) == OK)
15080 rettv->vval.v_float = log(f);
15081 else
15082 rettv->vval.v_float = 0.0;
15083}
15084
15085/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015086 * "log10()" function
15087 */
15088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015089f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015090{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015091 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015092
15093 rettv->v_type = VAR_FLOAT;
15094 if (get_float_arg(argvars, &f) == OK)
15095 rettv->vval.v_float = log10(f);
15096 else
15097 rettv->vval.v_float = 0.0;
15098}
15099#endif
15100
Bram Moolenaar1dced572012-04-05 16:54:08 +020015101#ifdef FEAT_LUA
15102/*
15103 * "luaeval()" function
15104 */
15105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015106f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015107{
15108 char_u *str;
15109 char_u buf[NUMBUFLEN];
15110
15111 str = get_tv_string_buf(&argvars[0], buf);
15112 do_luaeval(str, argvars + 1, rettv);
15113}
15114#endif
15115
Bram Moolenaar071d4272004-06-13 20:20:40 +000015116/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015117 * "map()" function
15118 */
15119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015120f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015121{
15122 filter_map(argvars, rettv, TRUE);
15123}
15124
15125/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015126 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015127 */
15128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015129f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015130{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015131 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015132}
15133
15134/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015135 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015136 */
15137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015138f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015139{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015140 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141}
15142
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015143static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015144
15145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015146find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015148 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015149 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015150 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015151 char_u *pat;
15152 regmatch_T regmatch;
15153 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015154 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155 char_u *save_cpo;
15156 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015157 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015158 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015159 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015160 list_T *l = NULL;
15161 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015162 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015163 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015164
15165 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15166 save_cpo = p_cpo;
15167 p_cpo = (char_u *)"";
15168
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015169 rettv->vval.v_number = -1;
15170 if (type == 3)
15171 {
15172 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015173 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015174 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015175 }
15176 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015177 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015178 rettv->v_type = VAR_STRING;
15179 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015181
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015182 if (argvars[0].v_type == VAR_LIST)
15183 {
15184 if ((l = argvars[0].vval.v_list) == NULL)
15185 goto theend;
15186 li = l->lv_first;
15187 }
15188 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015189 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015190 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015191 len = (long)STRLEN(str);
15192 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015193
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015194 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15195 if (pat == NULL)
15196 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015197
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015198 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015199 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015200 int error = FALSE;
15201
15202 start = get_tv_number_chk(&argvars[2], &error);
15203 if (error)
15204 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015205 if (l != NULL)
15206 {
15207 li = list_find(l, start);
15208 if (li == NULL)
15209 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015210 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015211 }
15212 else
15213 {
15214 if (start < 0)
15215 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015216 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015217 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015218 /* When "count" argument is there ignore matches before "start",
15219 * otherwise skip part of the string. Differs when pattern is "^"
15220 * or "\<". */
15221 if (argvars[3].v_type != VAR_UNKNOWN)
15222 startcol = start;
15223 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015224 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015225 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015226 len -= start;
15227 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015228 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015229
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015230 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015231 nth = get_tv_number_chk(&argvars[3], &error);
15232 if (error)
15233 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015234 }
15235
15236 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15237 if (regmatch.regprog != NULL)
15238 {
15239 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015240
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015241 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015242 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015243 if (l != NULL)
15244 {
15245 if (li == NULL)
15246 {
15247 match = FALSE;
15248 break;
15249 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015250 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015251 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015252 if (str == NULL)
15253 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015254 }
15255
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015256 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015257
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015258 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015259 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015260 if (l == NULL && !match)
15261 break;
15262
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015263 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015264 if (l != NULL)
15265 {
15266 li = li->li_next;
15267 ++idx;
15268 }
15269 else
15270 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015271#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015272 startcol = (colnr_T)(regmatch.startp[0]
15273 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015274#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015275 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015276#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015277 if (startcol > (colnr_T)len
15278 || str + startcol <= regmatch.startp[0])
15279 {
15280 match = FALSE;
15281 break;
15282 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015283 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015284 }
15285
15286 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015287 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015288 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015289 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015290 int i;
15291
15292 /* return list with matched string and submatches */
15293 for (i = 0; i < NSUBEXP; ++i)
15294 {
15295 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015296 {
15297 if (list_append_string(rettv->vval.v_list,
15298 (char_u *)"", 0) == FAIL)
15299 break;
15300 }
15301 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015302 regmatch.startp[i],
15303 (int)(regmatch.endp[i] - regmatch.startp[i]))
15304 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015305 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015306 }
15307 }
15308 else if (type == 2)
15309 {
15310 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015311 if (l != NULL)
15312 copy_tv(&li->li_tv, rettv);
15313 else
15314 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015315 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015316 }
15317 else if (l != NULL)
15318 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015319 else
15320 {
15321 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015322 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015323 (varnumber_T)(regmatch.startp[0] - str);
15324 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015325 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015326 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015327 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328 }
15329 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015330 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015331 }
15332
15333theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015334 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015335 p_cpo = save_cpo;
15336}
15337
15338/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015339 * "match()" function
15340 */
15341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015342f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015343{
15344 find_some_match(argvars, rettv, 1);
15345}
15346
15347/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015348 * "matchadd()" function
15349 */
15350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015351f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015352{
15353#ifdef FEAT_SEARCH_EXTRA
15354 char_u buf[NUMBUFLEN];
15355 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15356 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15357 int prio = 10; /* default priority */
15358 int id = -1;
15359 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015360 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015361
15362 rettv->vval.v_number = -1;
15363
15364 if (grp == NULL || pat == NULL)
15365 return;
15366 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015367 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015368 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015369 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015370 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015371 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015372 if (argvars[4].v_type != VAR_UNKNOWN)
15373 {
15374 if (argvars[4].v_type != VAR_DICT)
15375 {
15376 EMSG(_(e_dictreq));
15377 return;
15378 }
15379 if (dict_find(argvars[4].vval.v_dict,
15380 (char_u *)"conceal", -1) != NULL)
15381 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15382 (char_u *)"conceal", FALSE);
15383 }
15384 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015385 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015386 if (error == TRUE)
15387 return;
15388 if (id >= 1 && id <= 3)
15389 {
15390 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15391 return;
15392 }
15393
Bram Moolenaar6561d522015-07-21 15:48:27 +020015394 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15395 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015396#endif
15397}
15398
15399/*
15400 * "matchaddpos()" function
15401 */
15402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015403f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015404{
15405#ifdef FEAT_SEARCH_EXTRA
15406 char_u buf[NUMBUFLEN];
15407 char_u *group;
15408 int prio = 10;
15409 int id = -1;
15410 int error = FALSE;
15411 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015412 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015413
15414 rettv->vval.v_number = -1;
15415
15416 group = get_tv_string_buf_chk(&argvars[0], buf);
15417 if (group == NULL)
15418 return;
15419
15420 if (argvars[1].v_type != VAR_LIST)
15421 {
15422 EMSG2(_(e_listarg), "matchaddpos()");
15423 return;
15424 }
15425 l = argvars[1].vval.v_list;
15426 if (l == NULL)
15427 return;
15428
15429 if (argvars[2].v_type != VAR_UNKNOWN)
15430 {
15431 prio = get_tv_number_chk(&argvars[2], &error);
15432 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015433 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015434 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015435 if (argvars[4].v_type != VAR_UNKNOWN)
15436 {
15437 if (argvars[4].v_type != VAR_DICT)
15438 {
15439 EMSG(_(e_dictreq));
15440 return;
15441 }
15442 if (dict_find(argvars[4].vval.v_dict,
15443 (char_u *)"conceal", -1) != NULL)
15444 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15445 (char_u *)"conceal", FALSE);
15446 }
15447 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015448 }
15449 if (error == TRUE)
15450 return;
15451
15452 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15453 if (id == 1 || id == 2)
15454 {
15455 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15456 return;
15457 }
15458
Bram Moolenaar6561d522015-07-21 15:48:27 +020015459 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15460 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015461#endif
15462}
15463
15464/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015465 * "matcharg()" function
15466 */
15467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015468f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015469{
15470 if (rettv_list_alloc(rettv) == OK)
15471 {
15472#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015473 int id = get_tv_number(&argvars[0]);
15474 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015475
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015476 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015477 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015478 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15479 {
15480 list_append_string(rettv->vval.v_list,
15481 syn_id2name(m->hlg_id), -1);
15482 list_append_string(rettv->vval.v_list, m->pattern, -1);
15483 }
15484 else
15485 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015486 list_append_string(rettv->vval.v_list, NULL, -1);
15487 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015488 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015489 }
15490#endif
15491 }
15492}
15493
15494/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015495 * "matchdelete()" function
15496 */
15497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015498f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015499{
15500#ifdef FEAT_SEARCH_EXTRA
15501 rettv->vval.v_number = match_delete(curwin,
15502 (int)get_tv_number(&argvars[0]), TRUE);
15503#endif
15504}
15505
15506/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015507 * "matchend()" function
15508 */
15509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015510f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015511{
15512 find_some_match(argvars, rettv, 0);
15513}
15514
15515/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015516 * "matchlist()" function
15517 */
15518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015519f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015520{
15521 find_some_match(argvars, rettv, 3);
15522}
15523
15524/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015525 * "matchstr()" function
15526 */
15527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015528f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015529{
15530 find_some_match(argvars, rettv, 2);
15531}
15532
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015533static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015534
15535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015536max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015537{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015538 long n = 0;
15539 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015540 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015541
15542 if (argvars[0].v_type == VAR_LIST)
15543 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015544 list_T *l;
15545 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015546
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015547 l = argvars[0].vval.v_list;
15548 if (l != NULL)
15549 {
15550 li = l->lv_first;
15551 if (li != NULL)
15552 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015553 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015554 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015555 {
15556 li = li->li_next;
15557 if (li == NULL)
15558 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015559 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015560 if (domax ? i > n : i < n)
15561 n = i;
15562 }
15563 }
15564 }
15565 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015566 else if (argvars[0].v_type == VAR_DICT)
15567 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015568 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015569 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015570 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015571 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015572
15573 d = argvars[0].vval.v_dict;
15574 if (d != NULL)
15575 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015576 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015577 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015578 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015579 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015580 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015581 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015582 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015583 if (first)
15584 {
15585 n = i;
15586 first = FALSE;
15587 }
15588 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015589 n = i;
15590 }
15591 }
15592 }
15593 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015594 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015595 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015596 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015597}
15598
15599/*
15600 * "max()" function
15601 */
15602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015603f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015604{
15605 max_min(argvars, rettv, TRUE);
15606}
15607
15608/*
15609 * "min()" function
15610 */
15611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015612f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015613{
15614 max_min(argvars, rettv, FALSE);
15615}
15616
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015617static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015618
15619/*
15620 * Create the directory in which "dir" is located, and higher levels when
15621 * needed.
15622 */
15623 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015624mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015625{
15626 char_u *p;
15627 char_u *updir;
15628 int r = FAIL;
15629
15630 /* Get end of directory name in "dir".
15631 * We're done when it's "/" or "c:/". */
15632 p = gettail_sep(dir);
15633 if (p <= get_past_head(dir))
15634 return OK;
15635
15636 /* If the directory exists we're done. Otherwise: create it.*/
15637 updir = vim_strnsave(dir, (int)(p - dir));
15638 if (updir == NULL)
15639 return FAIL;
15640 if (mch_isdir(updir))
15641 r = OK;
15642 else if (mkdir_recurse(updir, prot) == OK)
15643 r = vim_mkdir_emsg(updir, prot);
15644 vim_free(updir);
15645 return r;
15646}
15647
15648#ifdef vim_mkdir
15649/*
15650 * "mkdir()" function
15651 */
15652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015653f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015654{
15655 char_u *dir;
15656 char_u buf[NUMBUFLEN];
15657 int prot = 0755;
15658
15659 rettv->vval.v_number = FAIL;
15660 if (check_restricted() || check_secure())
15661 return;
15662
15663 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015664 if (*dir == NUL)
15665 rettv->vval.v_number = FAIL;
15666 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015667 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015668 if (*gettail(dir) == NUL)
15669 /* remove trailing slashes */
15670 *gettail_sep(dir) = NUL;
15671
15672 if (argvars[1].v_type != VAR_UNKNOWN)
15673 {
15674 if (argvars[2].v_type != VAR_UNKNOWN)
15675 prot = get_tv_number_chk(&argvars[2], NULL);
15676 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15677 mkdir_recurse(dir, prot);
15678 }
15679 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015680 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015681}
15682#endif
15683
Bram Moolenaar0d660222005-01-07 21:51:51 +000015684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685 * "mode()" function
15686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015688f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015689{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015690 char_u buf[3];
15691
15692 buf[1] = NUL;
15693 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694
Bram Moolenaar071d4272004-06-13 20:20:40 +000015695 if (VIsual_active)
15696 {
15697 if (VIsual_select)
15698 buf[0] = VIsual_mode + 's' - 'v';
15699 else
15700 buf[0] = VIsual_mode;
15701 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015702 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015703 || State == CONFIRM)
15704 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015706 if (State == ASKMORE)
15707 buf[1] = 'm';
15708 else if (State == CONFIRM)
15709 buf[1] = '?';
15710 }
15711 else if (State == EXTERNCMD)
15712 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015713 else if (State & INSERT)
15714 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015715#ifdef FEAT_VREPLACE
15716 if (State & VREPLACE_FLAG)
15717 {
15718 buf[0] = 'R';
15719 buf[1] = 'v';
15720 }
15721 else
15722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015723 if (State & REPLACE_FLAG)
15724 buf[0] = 'R';
15725 else
15726 buf[0] = 'i';
15727 }
15728 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015729 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015731 if (exmode_active)
15732 buf[1] = 'v';
15733 }
15734 else if (exmode_active)
15735 {
15736 buf[0] = 'c';
15737 buf[1] = 'e';
15738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015740 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015742 if (finish_op)
15743 buf[1] = 'o';
15744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015746 /* Clear out the minor mode when the argument is not a non-zero number or
15747 * non-empty string. */
15748 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015749 buf[1] = NUL;
15750
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015751 rettv->vval.v_string = vim_strsave(buf);
15752 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015753}
15754
Bram Moolenaar429fa852013-04-15 12:27:36 +020015755#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015756/*
15757 * "mzeval()" function
15758 */
15759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015760f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015761{
15762 char_u *str;
15763 char_u buf[NUMBUFLEN];
15764
15765 str = get_tv_string_buf(&argvars[0], buf);
15766 do_mzeval(str, rettv);
15767}
Bram Moolenaar75676462013-01-30 14:55:42 +010015768
15769 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015770mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015771{
15772 typval_T argvars[3];
15773
15774 argvars[0].v_type = VAR_STRING;
15775 argvars[0].vval.v_string = name;
15776 copy_tv(args, &argvars[1]);
15777 argvars[2].v_type = VAR_UNKNOWN;
15778 f_call(argvars, rettv);
15779 clear_tv(&argvars[1]);
15780}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015781#endif
15782
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015784 * "nextnonblank()" function
15785 */
15786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015787f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015788{
15789 linenr_T lnum;
15790
15791 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15792 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015793 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015794 {
15795 lnum = 0;
15796 break;
15797 }
15798 if (*skipwhite(ml_get(lnum)) != NUL)
15799 break;
15800 }
15801 rettv->vval.v_number = lnum;
15802}
15803
15804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015805 * "nr2char()" function
15806 */
15807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015808f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015809{
15810 char_u buf[NUMBUFLEN];
15811
15812#ifdef FEAT_MBYTE
15813 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015814 {
15815 int utf8 = 0;
15816
15817 if (argvars[1].v_type != VAR_UNKNOWN)
15818 utf8 = get_tv_number_chk(&argvars[1], NULL);
15819 if (utf8)
15820 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15821 else
15822 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015824 else
15825#endif
15826 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015827 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015828 buf[1] = NUL;
15829 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015830 rettv->v_type = VAR_STRING;
15831 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015832}
15833
15834/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015835 * "or(expr, expr)" function
15836 */
15837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015838f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015839{
15840 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15841 | get_tv_number_chk(&argvars[1], NULL);
15842}
15843
15844/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015845 * "pathshorten()" function
15846 */
15847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015848f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015849{
15850 char_u *p;
15851
15852 rettv->v_type = VAR_STRING;
15853 p = get_tv_string_chk(&argvars[0]);
15854 if (p == NULL)
15855 rettv->vval.v_string = NULL;
15856 else
15857 {
15858 p = vim_strsave(p);
15859 rettv->vval.v_string = p;
15860 if (p != NULL)
15861 shorten_dir(p);
15862 }
15863}
15864
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015865#ifdef FEAT_PERL
15866/*
15867 * "perleval()" function
15868 */
15869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015870f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015871{
15872 char_u *str;
15873 char_u buf[NUMBUFLEN];
15874
15875 str = get_tv_string_buf(&argvars[0], buf);
15876 do_perleval(str, rettv);
15877}
15878#endif
15879
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015880#ifdef FEAT_FLOAT
15881/*
15882 * "pow()" function
15883 */
15884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015885f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015886{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015887 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015888
15889 rettv->v_type = VAR_FLOAT;
15890 if (get_float_arg(argvars, &fx) == OK
15891 && get_float_arg(&argvars[1], &fy) == OK)
15892 rettv->vval.v_float = pow(fx, fy);
15893 else
15894 rettv->vval.v_float = 0.0;
15895}
15896#endif
15897
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015898/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015899 * "prevnonblank()" function
15900 */
15901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015902f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015903{
15904 linenr_T lnum;
15905
15906 lnum = get_tv_lnum(argvars);
15907 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15908 lnum = 0;
15909 else
15910 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15911 --lnum;
15912 rettv->vval.v_number = lnum;
15913}
15914
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015915/* This dummy va_list is here because:
15916 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15917 * - locally in the function results in a "used before set" warning
15918 * - using va_start() to initialize it gives "function with fixed args" error */
15919static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015920
Bram Moolenaar8c711452005-01-14 21:53:12 +000015921/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015922 * "printf()" function
15923 */
15924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015925f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015926{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015927 char_u buf[NUMBUFLEN];
15928 int len;
15929 char_u *s;
15930 int saved_did_emsg = did_emsg;
15931 char *fmt;
15932
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015933 rettv->v_type = VAR_STRING;
15934 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015935
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015936 /* Get the required length, allocate the buffer and do it for real. */
15937 did_emsg = FALSE;
15938 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15939 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15940 if (!did_emsg)
15941 {
15942 s = alloc(len + 1);
15943 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015944 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015945 rettv->vval.v_string = s;
15946 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015947 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015948 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015949 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015950}
15951
15952/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015953 * "pumvisible()" function
15954 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015956f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015957{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015958#ifdef FEAT_INS_EXPAND
15959 if (pum_visible())
15960 rettv->vval.v_number = 1;
15961#endif
15962}
15963
Bram Moolenaardb913952012-06-29 12:54:53 +020015964#ifdef FEAT_PYTHON3
15965/*
15966 * "py3eval()" function
15967 */
15968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015969f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015970{
15971 char_u *str;
15972 char_u buf[NUMBUFLEN];
15973
15974 str = get_tv_string_buf(&argvars[0], buf);
15975 do_py3eval(str, rettv);
15976}
15977#endif
15978
15979#ifdef FEAT_PYTHON
15980/*
15981 * "pyeval()" function
15982 */
15983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015984f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015985{
15986 char_u *str;
15987 char_u buf[NUMBUFLEN];
15988
15989 str = get_tv_string_buf(&argvars[0], buf);
15990 do_pyeval(str, rettv);
15991}
15992#endif
15993
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015994/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015995 * "range()" function
15996 */
15997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015998f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015999{
16000 long start;
16001 long end;
16002 long stride = 1;
16003 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016004 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016005
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016006 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016007 if (argvars[1].v_type == VAR_UNKNOWN)
16008 {
16009 end = start - 1;
16010 start = 0;
16011 }
16012 else
16013 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016014 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016015 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016016 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016017 }
16018
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016019 if (error)
16020 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016021 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016022 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016023 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016024 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016025 else
16026 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016027 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016028 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016029 if (list_append_number(rettv->vval.v_list,
16030 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016031 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016032 }
16033}
16034
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016035/*
16036 * "readfile()" function
16037 */
16038 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016039f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016040{
16041 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016042 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016043 char_u *fname;
16044 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016045 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16046 int io_size = sizeof(buf);
16047 int readlen; /* size of last fread() */
16048 char_u *prev = NULL; /* previously read bytes, if any */
16049 long prevlen = 0; /* length of data in prev */
16050 long prevsize = 0; /* size of prev buffer */
16051 long maxline = MAXLNUM;
16052 long cnt = 0;
16053 char_u *p; /* position in buf */
16054 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016055
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016056 if (argvars[1].v_type != VAR_UNKNOWN)
16057 {
16058 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16059 binary = TRUE;
16060 if (argvars[2].v_type != VAR_UNKNOWN)
16061 maxline = get_tv_number(&argvars[2]);
16062 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016063
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016064 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016065 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016066
16067 /* Always open the file in binary mode, library functions have a mind of
16068 * their own about CR-LF conversion. */
16069 fname = get_tv_string(&argvars[0]);
16070 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16071 {
16072 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16073 return;
16074 }
16075
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016076 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016077 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016078 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016079
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016080 /* This for loop processes what was read, but is also entered at end
16081 * of file so that either:
16082 * - an incomplete line gets written
16083 * - a "binary" file gets an empty line at the end if it ends in a
16084 * newline. */
16085 for (p = buf, start = buf;
16086 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16087 ++p)
16088 {
16089 if (*p == '\n' || readlen <= 0)
16090 {
16091 listitem_T *li;
16092 char_u *s = NULL;
16093 long_u len = p - start;
16094
16095 /* Finished a line. Remove CRs before NL. */
16096 if (readlen > 0 && !binary)
16097 {
16098 while (len > 0 && start[len - 1] == '\r')
16099 --len;
16100 /* removal may cross back to the "prev" string */
16101 if (len == 0)
16102 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16103 --prevlen;
16104 }
16105 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016106 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016107 else
16108 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016109 /* Change "prev" buffer to be the right size. This way
16110 * the bytes are only copied once, and very long lines are
16111 * allocated only once. */
16112 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016113 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016114 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016115 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016116 prev = NULL; /* the list will own the string */
16117 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016118 }
16119 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016120 if (s == NULL)
16121 {
16122 do_outofmem_msg((long_u) prevlen + len + 1);
16123 failed = TRUE;
16124 break;
16125 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016126
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016127 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016128 {
16129 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016130 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016131 break;
16132 }
16133 li->li_tv.v_type = VAR_STRING;
16134 li->li_tv.v_lock = 0;
16135 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016136 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016137
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016138 start = p + 1; /* step over newline */
16139 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016140 break;
16141 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016142 else if (*p == NUL)
16143 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016144#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016145 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16146 * when finding the BF and check the previous two bytes. */
16147 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016148 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016149 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16150 * + 1, these may be in the "prev" string. */
16151 char_u back1 = p >= buf + 1 ? p[-1]
16152 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16153 char_u back2 = p >= buf + 2 ? p[-2]
16154 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16155 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016156
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016157 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016158 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016159 char_u *dest = p - 2;
16160
16161 /* Usually a BOM is at the beginning of a file, and so at
16162 * the beginning of a line; then we can just step over it.
16163 */
16164 if (start == dest)
16165 start = p + 1;
16166 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016167 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016168 /* have to shuffle buf to close gap */
16169 int adjust_prevlen = 0;
16170
16171 if (dest < buf)
16172 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016173 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016174 dest = buf;
16175 }
16176 if (readlen > p - buf + 1)
16177 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16178 readlen -= 3 - adjust_prevlen;
16179 prevlen -= adjust_prevlen;
16180 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016181 }
16182 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016183 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016184#endif
16185 } /* for */
16186
16187 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16188 break;
16189 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016190 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016191 /* There's part of a line in buf, store it in "prev". */
16192 if (p - start + prevlen >= prevsize)
16193 {
16194 /* need bigger "prev" buffer */
16195 char_u *newprev;
16196
16197 /* A common use case is ordinary text files and "prev" gets a
16198 * fragment of a line, so the first allocation is made
16199 * small, to avoid repeatedly 'allocing' large and
16200 * 'reallocing' small. */
16201 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016202 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016203 else
16204 {
16205 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016206 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016207 prevsize = grow50pc > growmin ? grow50pc : growmin;
16208 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016209 newprev = prev == NULL ? alloc(prevsize)
16210 : vim_realloc(prev, prevsize);
16211 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016212 {
16213 do_outofmem_msg((long_u)prevsize);
16214 failed = TRUE;
16215 break;
16216 }
16217 prev = newprev;
16218 }
16219 /* Add the line part to end of "prev". */
16220 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016221 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016222 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016223 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016224
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016225 /*
16226 * For a negative line count use only the lines at the end of the file,
16227 * free the rest.
16228 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016229 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016230 while (cnt > -maxline)
16231 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016232 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016233 --cnt;
16234 }
16235
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016236 if (failed)
16237 {
16238 list_free(rettv->vval.v_list, TRUE);
16239 /* readfile doc says an empty list is returned on error */
16240 rettv->vval.v_list = list_alloc();
16241 }
16242
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016243 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016244 fclose(fd);
16245}
16246
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016247#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016248static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016249
16250/*
16251 * Convert a List to proftime_T.
16252 * Return FAIL when there is something wrong.
16253 */
16254 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016255list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016256{
16257 long n1, n2;
16258 int error = FALSE;
16259
16260 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16261 || arg->vval.v_list->lv_len != 2)
16262 return FAIL;
16263 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16264 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16265# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016266 tm->HighPart = n1;
16267 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016268# else
16269 tm->tv_sec = n1;
16270 tm->tv_usec = n2;
16271# endif
16272 return error ? FAIL : OK;
16273}
16274#endif /* FEAT_RELTIME */
16275
16276/*
16277 * "reltime()" function
16278 */
16279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016280f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016281{
16282#ifdef FEAT_RELTIME
16283 proftime_T res;
16284 proftime_T start;
16285
16286 if (argvars[0].v_type == VAR_UNKNOWN)
16287 {
16288 /* No arguments: get current time. */
16289 profile_start(&res);
16290 }
16291 else if (argvars[1].v_type == VAR_UNKNOWN)
16292 {
16293 if (list2proftime(&argvars[0], &res) == FAIL)
16294 return;
16295 profile_end(&res);
16296 }
16297 else
16298 {
16299 /* Two arguments: compute the difference. */
16300 if (list2proftime(&argvars[0], &start) == FAIL
16301 || list2proftime(&argvars[1], &res) == FAIL)
16302 return;
16303 profile_sub(&res, &start);
16304 }
16305
16306 if (rettv_list_alloc(rettv) == OK)
16307 {
16308 long n1, n2;
16309
16310# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016311 n1 = res.HighPart;
16312 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016313# else
16314 n1 = res.tv_sec;
16315 n2 = res.tv_usec;
16316# endif
16317 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16318 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16319 }
16320#endif
16321}
16322
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016323#ifdef FEAT_FLOAT
16324/*
16325 * "reltimefloat()" function
16326 */
16327 static void
16328f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16329{
16330# ifdef FEAT_RELTIME
16331 proftime_T tm;
16332# endif
16333
16334 rettv->v_type = VAR_FLOAT;
16335 rettv->vval.v_float = 0;
16336# ifdef FEAT_RELTIME
16337 if (list2proftime(&argvars[0], &tm) == OK)
16338 rettv->vval.v_float = profile_float(&tm);
16339# endif
16340}
16341#endif
16342
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016343/*
16344 * "reltimestr()" function
16345 */
16346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016347f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016348{
16349#ifdef FEAT_RELTIME
16350 proftime_T tm;
16351#endif
16352
16353 rettv->v_type = VAR_STRING;
16354 rettv->vval.v_string = NULL;
16355#ifdef FEAT_RELTIME
16356 if (list2proftime(&argvars[0], &tm) == OK)
16357 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16358#endif
16359}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016360
Bram Moolenaar0d660222005-01-07 21:51:51 +000016361#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016362static void make_connection(void);
16363static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016364
16365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016366make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016367{
16368 if (X_DISPLAY == NULL
16369# ifdef FEAT_GUI
16370 && !gui.in_use
16371# endif
16372 )
16373 {
16374 x_force_connect = TRUE;
16375 setup_term_clip();
16376 x_force_connect = FALSE;
16377 }
16378}
16379
16380 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016381check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016382{
16383 make_connection();
16384 if (X_DISPLAY == NULL)
16385 {
16386 EMSG(_("E240: No connection to Vim server"));
16387 return FAIL;
16388 }
16389 return OK;
16390}
16391#endif
16392
16393#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016394static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016395
16396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016397remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016398{
16399 char_u *server_name;
16400 char_u *keys;
16401 char_u *r = NULL;
16402 char_u buf[NUMBUFLEN];
16403# ifdef WIN32
16404 HWND w;
16405# else
16406 Window w;
16407# endif
16408
16409 if (check_restricted() || check_secure())
16410 return;
16411
16412# ifdef FEAT_X11
16413 if (check_connection() == FAIL)
16414 return;
16415# endif
16416
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016417 server_name = get_tv_string_chk(&argvars[0]);
16418 if (server_name == NULL)
16419 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016420 keys = get_tv_string_buf(&argvars[1], buf);
16421# ifdef WIN32
16422 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16423# else
16424 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16425 < 0)
16426# endif
16427 {
16428 if (r != NULL)
16429 EMSG(r); /* sending worked but evaluation failed */
16430 else
16431 EMSG2(_("E241: Unable to send to %s"), server_name);
16432 return;
16433 }
16434
16435 rettv->vval.v_string = r;
16436
16437 if (argvars[2].v_type != VAR_UNKNOWN)
16438 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016439 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016440 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016441 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016442
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016443 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016444 v.di_tv.v_type = VAR_STRING;
16445 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016446 idvar = get_tv_string_chk(&argvars[2]);
16447 if (idvar != NULL)
16448 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016449 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016450 }
16451}
16452#endif
16453
16454/*
16455 * "remote_expr()" function
16456 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016458f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016459{
16460 rettv->v_type = VAR_STRING;
16461 rettv->vval.v_string = NULL;
16462#ifdef FEAT_CLIENTSERVER
16463 remote_common(argvars, rettv, TRUE);
16464#endif
16465}
16466
16467/*
16468 * "remote_foreground()" function
16469 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016471f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016472{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016473#ifdef FEAT_CLIENTSERVER
16474# ifdef WIN32
16475 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016476 {
16477 char_u *server_name = get_tv_string_chk(&argvars[0]);
16478
16479 if (server_name != NULL)
16480 serverForeground(server_name);
16481 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016482# else
16483 /* Send a foreground() expression to the server. */
16484 argvars[1].v_type = VAR_STRING;
16485 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16486 argvars[2].v_type = VAR_UNKNOWN;
16487 remote_common(argvars, rettv, TRUE);
16488 vim_free(argvars[1].vval.v_string);
16489# endif
16490#endif
16491}
16492
Bram Moolenaar0d660222005-01-07 21:51:51 +000016493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016494f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016495{
16496#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016497 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016498 char_u *s = NULL;
16499# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016500 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016501# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016502 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016503
16504 if (check_restricted() || check_secure())
16505 {
16506 rettv->vval.v_number = -1;
16507 return;
16508 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016509 serverid = get_tv_string_chk(&argvars[0]);
16510 if (serverid == NULL)
16511 {
16512 rettv->vval.v_number = -1;
16513 return; /* type error; errmsg already given */
16514 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016515# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016516 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016517 if (n == 0)
16518 rettv->vval.v_number = -1;
16519 else
16520 {
16521 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16522 rettv->vval.v_number = (s != NULL);
16523 }
16524# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016525 if (check_connection() == FAIL)
16526 return;
16527
16528 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016529 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016530# endif
16531
16532 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16533 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016534 char_u *retvar;
16535
Bram Moolenaar33570922005-01-25 22:26:29 +000016536 v.di_tv.v_type = VAR_STRING;
16537 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016538 retvar = get_tv_string_chk(&argvars[1]);
16539 if (retvar != NULL)
16540 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016541 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016542 }
16543#else
16544 rettv->vval.v_number = -1;
16545#endif
16546}
16547
Bram Moolenaar0d660222005-01-07 21:51:51 +000016548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016549f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016550{
16551 char_u *r = NULL;
16552
16553#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016554 char_u *serverid = get_tv_string_chk(&argvars[0]);
16555
16556 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016557 {
16558# ifdef WIN32
16559 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016560 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016561
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016562 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016563 if (n != 0)
16564 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16565 if (r == NULL)
16566# else
16567 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016568 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016569# endif
16570 EMSG(_("E277: Unable to read a server reply"));
16571 }
16572#endif
16573 rettv->v_type = VAR_STRING;
16574 rettv->vval.v_string = r;
16575}
16576
16577/*
16578 * "remote_send()" function
16579 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016581f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016582{
16583 rettv->v_type = VAR_STRING;
16584 rettv->vval.v_string = NULL;
16585#ifdef FEAT_CLIENTSERVER
16586 remote_common(argvars, rettv, FALSE);
16587#endif
16588}
16589
16590/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016591 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016592 */
16593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016594f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016595{
Bram Moolenaar33570922005-01-25 22:26:29 +000016596 list_T *l;
16597 listitem_T *item, *item2;
16598 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016599 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016600 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016601 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016602 dict_T *d;
16603 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016604 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016605
Bram Moolenaar8c711452005-01-14 21:53:12 +000016606 if (argvars[0].v_type == VAR_DICT)
16607 {
16608 if (argvars[2].v_type != VAR_UNKNOWN)
16609 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016610 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016611 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016612 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016613 key = get_tv_string_chk(&argvars[1]);
16614 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016615 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016616 di = dict_find(d, key, -1);
16617 if (di == NULL)
16618 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016619 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16620 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016621 {
16622 *rettv = di->di_tv;
16623 init_tv(&di->di_tv);
16624 dictitem_remove(d, di);
16625 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016626 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016627 }
16628 }
16629 else if (argvars[0].v_type != VAR_LIST)
16630 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016631 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016632 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016633 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016634 int error = FALSE;
16635
16636 idx = get_tv_number_chk(&argvars[1], &error);
16637 if (error)
16638 ; /* type error: do nothing, errmsg already given */
16639 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016640 EMSGN(_(e_listidx), idx);
16641 else
16642 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016643 if (argvars[2].v_type == VAR_UNKNOWN)
16644 {
16645 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016646 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016647 *rettv = item->li_tv;
16648 vim_free(item);
16649 }
16650 else
16651 {
16652 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016653 end = get_tv_number_chk(&argvars[2], &error);
16654 if (error)
16655 ; /* type error: do nothing */
16656 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016657 EMSGN(_(e_listidx), end);
16658 else
16659 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016660 int cnt = 0;
16661
16662 for (li = item; li != NULL; li = li->li_next)
16663 {
16664 ++cnt;
16665 if (li == item2)
16666 break;
16667 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016668 if (li == NULL) /* didn't find "item2" after "item" */
16669 EMSG(_(e_invrange));
16670 else
16671 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016672 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016673 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016674 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016675 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016676 l->lv_first = item;
16677 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016678 item->li_prev = NULL;
16679 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016680 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016681 }
16682 }
16683 }
16684 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016685 }
16686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687}
16688
16689/*
16690 * "rename({from}, {to})" function
16691 */
16692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016693f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016694{
16695 char_u buf[NUMBUFLEN];
16696
16697 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016698 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016699 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016700 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16701 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016702}
16703
16704/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016705 * "repeat()" function
16706 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016708f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016709{
16710 char_u *p;
16711 int n;
16712 int slen;
16713 int len;
16714 char_u *r;
16715 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016716
16717 n = get_tv_number(&argvars[1]);
16718 if (argvars[0].v_type == VAR_LIST)
16719 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016720 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016721 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016722 if (list_extend(rettv->vval.v_list,
16723 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016724 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016725 }
16726 else
16727 {
16728 p = get_tv_string(&argvars[0]);
16729 rettv->v_type = VAR_STRING;
16730 rettv->vval.v_string = NULL;
16731
16732 slen = (int)STRLEN(p);
16733 len = slen * n;
16734 if (len <= 0)
16735 return;
16736
16737 r = alloc(len + 1);
16738 if (r != NULL)
16739 {
16740 for (i = 0; i < n; i++)
16741 mch_memmove(r + i * slen, p, (size_t)slen);
16742 r[len] = NUL;
16743 }
16744
16745 rettv->vval.v_string = r;
16746 }
16747}
16748
16749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016750 * "resolve()" function
16751 */
16752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016753f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016754{
16755 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016756#ifdef HAVE_READLINK
16757 char_u *buf = NULL;
16758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016760 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016761#ifdef FEAT_SHORTCUT
16762 {
16763 char_u *v = NULL;
16764
16765 v = mch_resolve_shortcut(p);
16766 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016767 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016768 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016769 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016770 }
16771#else
16772# ifdef HAVE_READLINK
16773 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016774 char_u *cpy;
16775 int len;
16776 char_u *remain = NULL;
16777 char_u *q;
16778 int is_relative_to_current = FALSE;
16779 int has_trailing_pathsep = FALSE;
16780 int limit = 100;
16781
16782 p = vim_strsave(p);
16783
16784 if (p[0] == '.' && (vim_ispathsep(p[1])
16785 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16786 is_relative_to_current = TRUE;
16787
16788 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016789 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016790 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016791 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016792 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016794
16795 q = getnextcomp(p);
16796 if (*q != NUL)
16797 {
16798 /* Separate the first path component in "p", and keep the
16799 * remainder (beginning with the path separator). */
16800 remain = vim_strsave(q - 1);
16801 q[-1] = NUL;
16802 }
16803
Bram Moolenaard9462e32011-04-11 21:35:11 +020016804 buf = alloc(MAXPATHL + 1);
16805 if (buf == NULL)
16806 goto fail;
16807
Bram Moolenaar071d4272004-06-13 20:20:40 +000016808 for (;;)
16809 {
16810 for (;;)
16811 {
16812 len = readlink((char *)p, (char *)buf, MAXPATHL);
16813 if (len <= 0)
16814 break;
16815 buf[len] = NUL;
16816
16817 if (limit-- == 0)
16818 {
16819 vim_free(p);
16820 vim_free(remain);
16821 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016822 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016823 goto fail;
16824 }
16825
16826 /* Ensure that the result will have a trailing path separator
16827 * if the argument has one. */
16828 if (remain == NULL && has_trailing_pathsep)
16829 add_pathsep(buf);
16830
16831 /* Separate the first path component in the link value and
16832 * concatenate the remainders. */
16833 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16834 if (*q != NUL)
16835 {
16836 if (remain == NULL)
16837 remain = vim_strsave(q - 1);
16838 else
16839 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016840 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841 if (cpy != NULL)
16842 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016843 vim_free(remain);
16844 remain = cpy;
16845 }
16846 }
16847 q[-1] = NUL;
16848 }
16849
16850 q = gettail(p);
16851 if (q > p && *q == NUL)
16852 {
16853 /* Ignore trailing path separator. */
16854 q[-1] = NUL;
16855 q = gettail(p);
16856 }
16857 if (q > p && !mch_isFullName(buf))
16858 {
16859 /* symlink is relative to directory of argument */
16860 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16861 if (cpy != NULL)
16862 {
16863 STRCPY(cpy, p);
16864 STRCPY(gettail(cpy), buf);
16865 vim_free(p);
16866 p = cpy;
16867 }
16868 }
16869 else
16870 {
16871 vim_free(p);
16872 p = vim_strsave(buf);
16873 }
16874 }
16875
16876 if (remain == NULL)
16877 break;
16878
16879 /* Append the first path component of "remain" to "p". */
16880 q = getnextcomp(remain + 1);
16881 len = q - remain - (*q != NUL);
16882 cpy = vim_strnsave(p, STRLEN(p) + len);
16883 if (cpy != NULL)
16884 {
16885 STRNCAT(cpy, remain, len);
16886 vim_free(p);
16887 p = cpy;
16888 }
16889 /* Shorten "remain". */
16890 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016891 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016892 else
16893 {
16894 vim_free(remain);
16895 remain = NULL;
16896 }
16897 }
16898
16899 /* If the result is a relative path name, make it explicitly relative to
16900 * the current directory if and only if the argument had this form. */
16901 if (!vim_ispathsep(*p))
16902 {
16903 if (is_relative_to_current
16904 && *p != NUL
16905 && !(p[0] == '.'
16906 && (p[1] == NUL
16907 || vim_ispathsep(p[1])
16908 || (p[1] == '.'
16909 && (p[2] == NUL
16910 || vim_ispathsep(p[2]))))))
16911 {
16912 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016913 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016914 if (cpy != NULL)
16915 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016916 vim_free(p);
16917 p = cpy;
16918 }
16919 }
16920 else if (!is_relative_to_current)
16921 {
16922 /* Strip leading "./". */
16923 q = p;
16924 while (q[0] == '.' && vim_ispathsep(q[1]))
16925 q += 2;
16926 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016927 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928 }
16929 }
16930
16931 /* Ensure that the result will have no trailing path separator
16932 * if the argument had none. But keep "/" or "//". */
16933 if (!has_trailing_pathsep)
16934 {
16935 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016936 if (after_pathsep(p, q))
16937 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016938 }
16939
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016940 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941 }
16942# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016943 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944# endif
16945#endif
16946
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016947 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948
16949#ifdef HAVE_READLINK
16950fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016951 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016952#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016953 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954}
16955
16956/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016957 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016958 */
16959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016960f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961{
Bram Moolenaar33570922005-01-25 22:26:29 +000016962 list_T *l;
16963 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016964
Bram Moolenaar0d660222005-01-07 21:51:51 +000016965 if (argvars[0].v_type != VAR_LIST)
16966 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016967 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016968 && !tv_check_lock(l->lv_lock,
16969 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016970 {
16971 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016972 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016973 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016974 while (li != NULL)
16975 {
16976 ni = li->li_prev;
16977 list_append(l, li);
16978 li = ni;
16979 }
16980 rettv->vval.v_list = l;
16981 rettv->v_type = VAR_LIST;
16982 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016983 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016985}
16986
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016987#define SP_NOMOVE 0x01 /* don't move cursor */
16988#define SP_REPEAT 0x02 /* repeat to find outer pair */
16989#define SP_RETCOUNT 0x04 /* return matchcount */
16990#define SP_SETPCMARK 0x08 /* set previous context mark */
16991#define SP_START 0x10 /* accept match at start position */
16992#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16993#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016994#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016995
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016996static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016997
16998/*
16999 * Get flags for a search function.
17000 * Possibly sets "p_ws".
17001 * Returns BACKWARD, FORWARD or zero (for an error).
17002 */
17003 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017004get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017005{
17006 int dir = FORWARD;
17007 char_u *flags;
17008 char_u nbuf[NUMBUFLEN];
17009 int mask;
17010
17011 if (varp->v_type != VAR_UNKNOWN)
17012 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017013 flags = get_tv_string_buf_chk(varp, nbuf);
17014 if (flags == NULL)
17015 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017016 while (*flags != NUL)
17017 {
17018 switch (*flags)
17019 {
17020 case 'b': dir = BACKWARD; break;
17021 case 'w': p_ws = TRUE; break;
17022 case 'W': p_ws = FALSE; break;
17023 default: mask = 0;
17024 if (flagsp != NULL)
17025 switch (*flags)
17026 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017027 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017028 case 'e': mask = SP_END; break;
17029 case 'm': mask = SP_RETCOUNT; break;
17030 case 'n': mask = SP_NOMOVE; break;
17031 case 'p': mask = SP_SUBPAT; break;
17032 case 'r': mask = SP_REPEAT; break;
17033 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017034 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017035 }
17036 if (mask == 0)
17037 {
17038 EMSG2(_(e_invarg2), flags);
17039 dir = 0;
17040 }
17041 else
17042 *flagsp |= mask;
17043 }
17044 if (dir == 0)
17045 break;
17046 ++flags;
17047 }
17048 }
17049 return dir;
17050}
17051
Bram Moolenaar071d4272004-06-13 20:20:40 +000017052/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017053 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017054 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017055 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017056search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017057{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017058 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059 char_u *pat;
17060 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017061 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062 int save_p_ws = p_ws;
17063 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017064 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017065 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017066 proftime_T tm;
17067#ifdef FEAT_RELTIME
17068 long time_limit = 0;
17069#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017070 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017071 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017072
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017073 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017074 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017075 if (dir == 0)
17076 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017077 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017078 if (flags & SP_START)
17079 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017080 if (flags & SP_END)
17081 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017082 if (flags & SP_COLUMN)
17083 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017084
Bram Moolenaar76929292008-01-06 19:07:36 +000017085 /* Optional arguments: line number to stop searching and timeout. */
17086 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017087 {
17088 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17089 if (lnum_stop < 0)
17090 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017091#ifdef FEAT_RELTIME
17092 if (argvars[3].v_type != VAR_UNKNOWN)
17093 {
17094 time_limit = get_tv_number_chk(&argvars[3], NULL);
17095 if (time_limit < 0)
17096 goto theend;
17097 }
17098#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017099 }
17100
Bram Moolenaar76929292008-01-06 19:07:36 +000017101#ifdef FEAT_RELTIME
17102 /* Set the time limit, if there is one. */
17103 profile_setlimit(time_limit, &tm);
17104#endif
17105
Bram Moolenaar231334e2005-07-25 20:46:57 +000017106 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017107 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017108 * Check to make sure only those flags are set.
17109 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17110 * flags cannot be set. Check for that condition also.
17111 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017112 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017113 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017114 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017115 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017116 goto theend;
17117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017118
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017119 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017120 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017121 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017122 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017123 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017124 if (flags & SP_SUBPAT)
17125 retval = subpatnum;
17126 else
17127 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017128 if (flags & SP_SETPCMARK)
17129 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017130 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017131 if (match_pos != NULL)
17132 {
17133 /* Store the match cursor position */
17134 match_pos->lnum = pos.lnum;
17135 match_pos->col = pos.col + 1;
17136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017137 /* "/$" will put the cursor after the end of the line, may need to
17138 * correct that here */
17139 check_cursor();
17140 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017141
17142 /* If 'n' flag is used: restore cursor position. */
17143 if (flags & SP_NOMOVE)
17144 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017145 else
17146 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017147theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017148 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017149
17150 return retval;
17151}
17152
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017153#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017154
17155/*
17156 * round() is not in C90, use ceil() or floor() instead.
17157 */
17158 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017159vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017160{
17161 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17162}
17163
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017164/*
17165 * "round({float})" function
17166 */
17167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017168f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017169{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017170 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017171
17172 rettv->v_type = VAR_FLOAT;
17173 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017174 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017175 else
17176 rettv->vval.v_float = 0.0;
17177}
17178#endif
17179
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017180/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017181 * "screenattr()" function
17182 */
17183 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017184f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017185{
17186 int row;
17187 int col;
17188 int c;
17189
17190 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17191 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17192 if (row < 0 || row >= screen_Rows
17193 || col < 0 || col >= screen_Columns)
17194 c = -1;
17195 else
17196 c = ScreenAttrs[LineOffset[row] + col];
17197 rettv->vval.v_number = c;
17198}
17199
17200/*
17201 * "screenchar()" function
17202 */
17203 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017204f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017205{
17206 int row;
17207 int col;
17208 int off;
17209 int c;
17210
17211 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17212 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17213 if (row < 0 || row >= screen_Rows
17214 || col < 0 || col >= screen_Columns)
17215 c = -1;
17216 else
17217 {
17218 off = LineOffset[row] + col;
17219#ifdef FEAT_MBYTE
17220 if (enc_utf8 && ScreenLinesUC[off] != 0)
17221 c = ScreenLinesUC[off];
17222 else
17223#endif
17224 c = ScreenLines[off];
17225 }
17226 rettv->vval.v_number = c;
17227}
17228
17229/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017230 * "screencol()" function
17231 *
17232 * First column is 1 to be consistent with virtcol().
17233 */
17234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017235f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017236{
17237 rettv->vval.v_number = screen_screencol() + 1;
17238}
17239
17240/*
17241 * "screenrow()" function
17242 */
17243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017244f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017245{
17246 rettv->vval.v_number = screen_screenrow() + 1;
17247}
17248
17249/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017250 * "search()" function
17251 */
17252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017253f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017254{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017255 int flags = 0;
17256
17257 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258}
17259
Bram Moolenaar071d4272004-06-13 20:20:40 +000017260/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017261 * "searchdecl()" function
17262 */
17263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017264f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017265{
17266 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017267 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017268 int error = FALSE;
17269 char_u *name;
17270
17271 rettv->vval.v_number = 1; /* default: FAIL */
17272
17273 name = get_tv_string_chk(&argvars[0]);
17274 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017275 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017276 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017277 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17278 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17279 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017280 if (!error && name != NULL)
17281 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017282 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017283}
17284
17285/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017286 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017287 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017288 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017289searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290{
17291 char_u *spat, *mpat, *epat;
17292 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017294 int dir;
17295 int flags = 0;
17296 char_u nbuf1[NUMBUFLEN];
17297 char_u nbuf2[NUMBUFLEN];
17298 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017299 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017300 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017301 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017302
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017304 spat = get_tv_string_chk(&argvars[0]);
17305 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17306 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17307 if (spat == NULL || mpat == NULL || epat == NULL)
17308 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309
Bram Moolenaar071d4272004-06-13 20:20:40 +000017310 /* Handle the optional fourth argument: flags */
17311 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017312 if (dir == 0)
17313 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017314
17315 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017316 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17317 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017318 if ((flags & (SP_END | SP_SUBPAT)) != 0
17319 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017320 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017321 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017322 goto theend;
17323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017325 /* Using 'r' implies 'W', otherwise it doesn't work. */
17326 if (flags & SP_REPEAT)
17327 p_ws = FALSE;
17328
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017329 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017330 if (argvars[3].v_type == VAR_UNKNOWN
17331 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332 skip = (char_u *)"";
17333 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017335 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017336 if (argvars[5].v_type != VAR_UNKNOWN)
17337 {
17338 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17339 if (lnum_stop < 0)
17340 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017341#ifdef FEAT_RELTIME
17342 if (argvars[6].v_type != VAR_UNKNOWN)
17343 {
17344 time_limit = get_tv_number_chk(&argvars[6], NULL);
17345 if (time_limit < 0)
17346 goto theend;
17347 }
17348#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017349 }
17350 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017351 if (skip == NULL)
17352 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017354 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017355 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017356
17357theend:
17358 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017359
17360 return retval;
17361}
17362
17363/*
17364 * "searchpair()" function
17365 */
17366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017367f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017368{
17369 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17370}
17371
17372/*
17373 * "searchpairpos()" function
17374 */
17375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017376f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017377{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017378 pos_T match_pos;
17379 int lnum = 0;
17380 int col = 0;
17381
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017382 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017383 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017384
17385 if (searchpair_cmn(argvars, &match_pos) > 0)
17386 {
17387 lnum = match_pos.lnum;
17388 col = match_pos.col;
17389 }
17390
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017391 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17392 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017393}
17394
17395/*
17396 * Search for a start/middle/end thing.
17397 * Used by searchpair(), see its documentation for the details.
17398 * Returns 0 or -1 for no match,
17399 */
17400 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017401do_searchpair(
17402 char_u *spat, /* start pattern */
17403 char_u *mpat, /* middle pattern */
17404 char_u *epat, /* end pattern */
17405 int dir, /* BACKWARD or FORWARD */
17406 char_u *skip, /* skip expression */
17407 int flags, /* SP_SETPCMARK and other SP_ values */
17408 pos_T *match_pos,
17409 linenr_T lnum_stop, /* stop at this line if not zero */
17410 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017411{
17412 char_u *save_cpo;
17413 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17414 long retval = 0;
17415 pos_T pos;
17416 pos_T firstpos;
17417 pos_T foundpos;
17418 pos_T save_cursor;
17419 pos_T save_pos;
17420 int n;
17421 int r;
17422 int nest = 1;
17423 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017424 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017425 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017426
17427 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17428 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017429 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017430
Bram Moolenaar76929292008-01-06 19:07:36 +000017431#ifdef FEAT_RELTIME
17432 /* Set the time limit, if there is one. */
17433 profile_setlimit(time_limit, &tm);
17434#endif
17435
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017436 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17437 * start/middle/end (pat3, for the top pair). */
17438 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17439 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17440 if (pat2 == NULL || pat3 == NULL)
17441 goto theend;
17442 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17443 if (*mpat == NUL)
17444 STRCPY(pat3, pat2);
17445 else
17446 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17447 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017448 if (flags & SP_START)
17449 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017450
Bram Moolenaar071d4272004-06-13 20:20:40 +000017451 save_cursor = curwin->w_cursor;
17452 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017453 clearpos(&firstpos);
17454 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017455 pat = pat3;
17456 for (;;)
17457 {
17458 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017459 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017460 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17461 /* didn't find it or found the first match again: FAIL */
17462 break;
17463
17464 if (firstpos.lnum == 0)
17465 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017466 if (equalpos(pos, foundpos))
17467 {
17468 /* Found the same position again. Can happen with a pattern that
17469 * has "\zs" at the end and searching backwards. Advance one
17470 * character and try again. */
17471 if (dir == BACKWARD)
17472 decl(&pos);
17473 else
17474 incl(&pos);
17475 }
17476 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017478 /* clear the start flag to avoid getting stuck here */
17479 options &= ~SEARCH_START;
17480
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481 /* If the skip pattern matches, ignore this match. */
17482 if (*skip != NUL)
17483 {
17484 save_pos = curwin->w_cursor;
17485 curwin->w_cursor = pos;
17486 r = eval_to_bool(skip, &err, NULL, FALSE);
17487 curwin->w_cursor = save_pos;
17488 if (err)
17489 {
17490 /* Evaluating {skip} caused an error, break here. */
17491 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017492 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493 break;
17494 }
17495 if (r)
17496 continue;
17497 }
17498
17499 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17500 {
17501 /* Found end when searching backwards or start when searching
17502 * forward: nested pair. */
17503 ++nest;
17504 pat = pat2; /* nested, don't search for middle */
17505 }
17506 else
17507 {
17508 /* Found end when searching forward or start when searching
17509 * backward: end of (nested) pair; or found middle in outer pair. */
17510 if (--nest == 1)
17511 pat = pat3; /* outer level, search for middle */
17512 }
17513
17514 if (nest == 0)
17515 {
17516 /* Found the match: return matchcount or line number. */
17517 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017518 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017520 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017521 if (flags & SP_SETPCMARK)
17522 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523 curwin->w_cursor = pos;
17524 if (!(flags & SP_REPEAT))
17525 break;
17526 nest = 1; /* search for next unmatched */
17527 }
17528 }
17529
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017530 if (match_pos != NULL)
17531 {
17532 /* Store the match cursor position */
17533 match_pos->lnum = curwin->w_cursor.lnum;
17534 match_pos->col = curwin->w_cursor.col + 1;
17535 }
17536
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017538 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017539 curwin->w_cursor = save_cursor;
17540
17541theend:
17542 vim_free(pat2);
17543 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017544 if (p_cpo == empty_option)
17545 p_cpo = save_cpo;
17546 else
17547 /* Darn, evaluating the {skip} expression changed the value. */
17548 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017549
17550 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017551}
17552
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017553/*
17554 * "searchpos()" function
17555 */
17556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017557f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017558{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017559 pos_T match_pos;
17560 int lnum = 0;
17561 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017562 int n;
17563 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017564
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017565 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017566 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017567
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017568 n = search_cmn(argvars, &match_pos, &flags);
17569 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017570 {
17571 lnum = match_pos.lnum;
17572 col = match_pos.col;
17573 }
17574
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017575 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17576 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017577 if (flags & SP_SUBPAT)
17578 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017579}
17580
Bram Moolenaar0d660222005-01-07 21:51:51 +000017581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017582f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017583{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017584#ifdef FEAT_CLIENTSERVER
17585 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017586 char_u *server = get_tv_string_chk(&argvars[0]);
17587 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017588
Bram Moolenaar0d660222005-01-07 21:51:51 +000017589 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017590 if (server == NULL || reply == NULL)
17591 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017592 if (check_restricted() || check_secure())
17593 return;
17594# ifdef FEAT_X11
17595 if (check_connection() == FAIL)
17596 return;
17597# endif
17598
17599 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017601 EMSG(_("E258: Unable to send to client"));
17602 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017603 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017604 rettv->vval.v_number = 0;
17605#else
17606 rettv->vval.v_number = -1;
17607#endif
17608}
17609
Bram Moolenaar0d660222005-01-07 21:51:51 +000017610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017611f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017612{
17613 char_u *r = NULL;
17614
17615#ifdef FEAT_CLIENTSERVER
17616# ifdef WIN32
17617 r = serverGetVimNames();
17618# else
17619 make_connection();
17620 if (X_DISPLAY != NULL)
17621 r = serverGetVimNames(X_DISPLAY);
17622# endif
17623#endif
17624 rettv->v_type = VAR_STRING;
17625 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626}
17627
17628/*
17629 * "setbufvar()" function
17630 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017632f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633{
17634 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017636 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017637 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638 char_u nbuf[NUMBUFLEN];
17639
17640 if (check_restricted() || check_secure())
17641 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017642 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17643 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017644 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645 varp = &argvars[2];
17646
17647 if (buf != NULL && varname != NULL && varp != NULL)
17648 {
17649 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017650 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651
17652 if (*varname == '&')
17653 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017654 long numval;
17655 char_u *strval;
17656 int error = FALSE;
17657
Bram Moolenaar071d4272004-06-13 20:20:40 +000017658 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017659 numval = get_tv_number_chk(varp, &error);
17660 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017661 if (!error && strval != NULL)
17662 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663 }
17664 else
17665 {
17666 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17667 if (bufvarname != NULL)
17668 {
17669 STRCPY(bufvarname, "b:");
17670 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017671 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672 vim_free(bufvarname);
17673 }
17674 }
17675
17676 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017679}
17680
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017682f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017683{
17684 dict_T *d;
17685 dictitem_T *di;
17686 char_u *csearch;
17687
17688 if (argvars[0].v_type != VAR_DICT)
17689 {
17690 EMSG(_(e_dictreq));
17691 return;
17692 }
17693
17694 if ((d = argvars[0].vval.v_dict) != NULL)
17695 {
17696 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17697 if (csearch != NULL)
17698 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017699#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017700 if (enc_utf8)
17701 {
17702 int pcc[MAX_MCO];
17703 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017704
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017705 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17706 }
17707 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017708#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017709 set_last_csearch(PTR2CHAR(csearch),
17710 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017711 }
17712
17713 di = dict_find(d, (char_u *)"forward", -1);
17714 if (di != NULL)
17715 set_csearch_direction(get_tv_number(&di->di_tv)
17716 ? FORWARD : BACKWARD);
17717
17718 di = dict_find(d, (char_u *)"until", -1);
17719 if (di != NULL)
17720 set_csearch_until(!!get_tv_number(&di->di_tv));
17721 }
17722}
17723
Bram Moolenaar071d4272004-06-13 20:20:40 +000017724/*
17725 * "setcmdpos()" function
17726 */
17727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017728f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017730 int pos = (int)get_tv_number(&argvars[0]) - 1;
17731
17732 if (pos >= 0)
17733 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734}
17735
17736/*
Bram Moolenaar80492532016-03-08 17:08:53 +010017737 * "setfperm({fname}, {mode})" function
17738 */
17739 static void
17740f_setfperm(typval_T *argvars, typval_T *rettv)
17741{
17742 char_u *fname;
17743 char_u modebuf[NUMBUFLEN];
17744 char_u *mode_str;
17745 int i;
17746 int mask;
17747 int mode = 0;
17748
17749 rettv->vval.v_number = 0;
17750 fname = get_tv_string_chk(&argvars[0]);
17751 if (fname == NULL)
17752 return;
17753 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
17754 if (mode_str == NULL)
17755 return;
17756 if (STRLEN(mode_str) != 9)
17757 {
17758 EMSG2(_(e_invarg2), mode_str);
17759 return;
17760 }
17761
17762 mask = 1;
17763 for (i = 8; i >= 0; --i)
17764 {
17765 if (mode_str[i] != '-')
17766 mode |= mask;
17767 mask = mask << 1;
17768 }
17769 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
17770}
17771
17772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773 * "setline()" function
17774 */
17775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017776f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777{
17778 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017779 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017780 list_T *l = NULL;
17781 listitem_T *li = NULL;
17782 long added = 0;
17783 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017785 lnum = get_tv_lnum(&argvars[0]);
17786 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017788 l = argvars[1].vval.v_list;
17789 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017790 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017791 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017792 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017793
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017794 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017795 for (;;)
17796 {
17797 if (l != NULL)
17798 {
17799 /* list argument, get next string */
17800 if (li == NULL)
17801 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017802 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017803 li = li->li_next;
17804 }
17805
17806 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017807 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017808 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017809
17810 /* When coming here from Insert mode, sync undo, so that this can be
17811 * undone separately from what was previously inserted. */
17812 if (u_sync_once == 2)
17813 {
17814 u_sync_once = 1; /* notify that u_sync() was called */
17815 u_sync(TRUE);
17816 }
17817
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017818 if (lnum <= curbuf->b_ml.ml_line_count)
17819 {
17820 /* existing line, replace it */
17821 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17822 {
17823 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017824 if (lnum == curwin->w_cursor.lnum)
17825 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017826 rettv->vval.v_number = 0; /* OK */
17827 }
17828 }
17829 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17830 {
17831 /* lnum is one past the last line, append the line */
17832 ++added;
17833 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17834 rettv->vval.v_number = 0; /* OK */
17835 }
17836
17837 if (l == NULL) /* only one string argument */
17838 break;
17839 ++lnum;
17840 }
17841
17842 if (added > 0)
17843 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017844}
17845
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017846static 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 +000017847
Bram Moolenaar071d4272004-06-13 20:20:40 +000017848/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017849 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017850 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017852set_qf_ll_list(
17853 win_T *wp UNUSED,
17854 typval_T *list_arg UNUSED,
17855 typval_T *action_arg UNUSED,
17856 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017857{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017858#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017859 char_u *act;
17860 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017861#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017862
Bram Moolenaar2641f772005-03-25 21:58:17 +000017863 rettv->vval.v_number = -1;
17864
17865#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017866 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017867 EMSG(_(e_listreq));
17868 else
17869 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017870 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017871
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017872 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017873 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017874 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017875 if (act == NULL)
17876 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017877 if (*act == 'a' || *act == 'r')
17878 action = *act;
17879 }
17880
Bram Moolenaar81484f42012-12-05 15:16:47 +010017881 if (l != NULL && set_errorlist(wp, l, action,
17882 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017883 rettv->vval.v_number = 0;
17884 }
17885#endif
17886}
17887
17888/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017889 * "setloclist()" function
17890 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017892f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017893{
17894 win_T *win;
17895
17896 rettv->vval.v_number = -1;
17897
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017898 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017899 if (win != NULL)
17900 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17901}
17902
17903/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017904 * "setmatches()" function
17905 */
17906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017907f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017908{
17909#ifdef FEAT_SEARCH_EXTRA
17910 list_T *l;
17911 listitem_T *li;
17912 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017913 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017914
17915 rettv->vval.v_number = -1;
17916 if (argvars[0].v_type != VAR_LIST)
17917 {
17918 EMSG(_(e_listreq));
17919 return;
17920 }
17921 if ((l = argvars[0].vval.v_list) != NULL)
17922 {
17923
17924 /* To some extent make sure that we are dealing with a list from
17925 * "getmatches()". */
17926 li = l->lv_first;
17927 while (li != NULL)
17928 {
17929 if (li->li_tv.v_type != VAR_DICT
17930 || (d = li->li_tv.vval.v_dict) == NULL)
17931 {
17932 EMSG(_(e_invarg));
17933 return;
17934 }
17935 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017936 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17937 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017938 && dict_find(d, (char_u *)"priority", -1) != NULL
17939 && dict_find(d, (char_u *)"id", -1) != NULL))
17940 {
17941 EMSG(_(e_invarg));
17942 return;
17943 }
17944 li = li->li_next;
17945 }
17946
17947 clear_matches(curwin);
17948 li = l->lv_first;
17949 while (li != NULL)
17950 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017951 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017952 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017953 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017954 char_u *group;
17955 int priority;
17956 int id;
17957 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017958
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017959 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017960 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17961 {
17962 if (s == NULL)
17963 {
17964 s = list_alloc();
17965 if (s == NULL)
17966 return;
17967 }
17968
17969 /* match from matchaddpos() */
17970 for (i = 1; i < 9; i++)
17971 {
17972 sprintf((char *)buf, (char *)"pos%d", i);
17973 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17974 {
17975 if (di->di_tv.v_type != VAR_LIST)
17976 return;
17977
17978 list_append_tv(s, &di->di_tv);
17979 s->lv_refcount++;
17980 }
17981 else
17982 break;
17983 }
17984 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017985
17986 group = get_dict_string(d, (char_u *)"group", FALSE);
17987 priority = (int)get_dict_number(d, (char_u *)"priority");
17988 id = (int)get_dict_number(d, (char_u *)"id");
17989 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17990 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17991 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017992 if (i == 0)
17993 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017994 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017995 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017996 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017997 }
17998 else
17999 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018000 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018001 list_unref(s);
18002 s = NULL;
18003 }
18004
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018005 li = li->li_next;
18006 }
18007 rettv->vval.v_number = 0;
18008 }
18009#endif
18010}
18011
18012/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018013 * "setpos()" function
18014 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018016f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018017{
18018 pos_T pos;
18019 int fnum;
18020 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018021 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018022
Bram Moolenaar08250432008-02-13 11:42:46 +000018023 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018024 name = get_tv_string_chk(argvars);
18025 if (name != NULL)
18026 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018027 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018028 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018029 if (--pos.col < 0)
18030 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018031 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018032 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018033 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018034 if (fnum == curbuf->b_fnum)
18035 {
18036 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018037 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018038 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018039 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018040 curwin->w_set_curswant = FALSE;
18041 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018042 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018043 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018044 }
18045 else
18046 EMSG(_(e_invarg));
18047 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018048 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18049 {
18050 /* set mark */
18051 if (setmark_pos(name[1], &pos, fnum) == OK)
18052 rettv->vval.v_number = 0;
18053 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018054 else
18055 EMSG(_(e_invarg));
18056 }
18057 }
18058}
18059
18060/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018061 * "setqflist()" function
18062 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018064f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018065{
18066 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18067}
18068
18069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070 * "setreg()" function
18071 */
18072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018073f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074{
18075 int regname;
18076 char_u *strregname;
18077 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018078 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079 int append;
18080 char_u yank_type;
18081 long block_len;
18082
18083 block_len = -1;
18084 yank_type = MAUTO;
18085 append = FALSE;
18086
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018087 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018088 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018090 if (strregname == NULL)
18091 return; /* type error; errmsg already given */
18092 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093 if (regname == 0 || regname == '@')
18094 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018095
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018096 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018098 stropt = get_tv_string_chk(&argvars[2]);
18099 if (stropt == NULL)
18100 return; /* type error */
18101 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102 switch (*stropt)
18103 {
18104 case 'a': case 'A': /* append */
18105 append = TRUE;
18106 break;
18107 case 'v': case 'c': /* character-wise selection */
18108 yank_type = MCHAR;
18109 break;
18110 case 'V': case 'l': /* line-wise selection */
18111 yank_type = MLINE;
18112 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113 case 'b': case Ctrl_V: /* block-wise selection */
18114 yank_type = MBLOCK;
18115 if (VIM_ISDIGIT(stropt[1]))
18116 {
18117 ++stropt;
18118 block_len = getdigits(&stropt) - 1;
18119 --stropt;
18120 }
18121 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122 }
18123 }
18124
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018125 if (argvars[1].v_type == VAR_LIST)
18126 {
18127 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018128 char_u **allocval;
18129 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018130 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018131 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018132 int len = argvars[1].vval.v_list->lv_len;
18133 listitem_T *li;
18134
Bram Moolenaar7d647822014-04-05 21:28:56 +020018135 /* First half: use for pointers to result lines; second half: use for
18136 * pointers to allocated copies. */
18137 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018138 if (lstval == NULL)
18139 return;
18140 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018141 allocval = lstval + len + 2;
18142 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018143
18144 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18145 li = li->li_next)
18146 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018147 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018148 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018149 goto free_lstval;
18150 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018151 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018152 /* Need to make a copy, next get_tv_string_buf_chk() will
18153 * overwrite the string. */
18154 strval = vim_strsave(buf);
18155 if (strval == NULL)
18156 goto free_lstval;
18157 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018158 }
18159 *curval++ = strval;
18160 }
18161 *curval++ = NULL;
18162
18163 write_reg_contents_lst(regname, lstval, -1,
18164 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018165free_lstval:
18166 while (curallocval > allocval)
18167 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018168 vim_free(lstval);
18169 }
18170 else
18171 {
18172 strval = get_tv_string_chk(&argvars[1]);
18173 if (strval == NULL)
18174 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018175 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018177 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018178 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018179}
18180
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018181/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018182 * "settabvar()" function
18183 */
18184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018185f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018186{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018187#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018188 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018189 tabpage_T *tp;
18190#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018191 char_u *varname, *tabvarname;
18192 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018193
18194 rettv->vval.v_number = 0;
18195
18196 if (check_restricted() || check_secure())
18197 return;
18198
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018199#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018200 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018201#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018202 varname = get_tv_string_chk(&argvars[1]);
18203 varp = &argvars[2];
18204
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018205 if (varname != NULL && varp != NULL
18206#ifdef FEAT_WINDOWS
18207 && tp != NULL
18208#endif
18209 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018210 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018211#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018212 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018213 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018214#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018215
18216 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18217 if (tabvarname != NULL)
18218 {
18219 STRCPY(tabvarname, "t:");
18220 STRCPY(tabvarname + 2, varname);
18221 set_var(tabvarname, varp, TRUE);
18222 vim_free(tabvarname);
18223 }
18224
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018225#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018226 /* Restore current tabpage */
18227 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018228 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018229#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018230 }
18231}
18232
18233/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018234 * "settabwinvar()" function
18235 */
18236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018237f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018238{
18239 setwinvar(argvars, rettv, 1);
18240}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018241
18242/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018243 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018244 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018246f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018248 setwinvar(argvars, rettv, 0);
18249}
18250
18251/*
18252 * "setwinvar()" and "settabwinvar()" functions
18253 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018254
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018256setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018257{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258 win_T *win;
18259#ifdef FEAT_WINDOWS
18260 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018261 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018262 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263#endif
18264 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018265 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018267 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018268
18269 if (check_restricted() || check_secure())
18270 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018271
18272#ifdef FEAT_WINDOWS
18273 if (off == 1)
18274 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18275 else
18276 tp = curtab;
18277#endif
18278 win = find_win_by_nr(&argvars[off], tp);
18279 varname = get_tv_string_chk(&argvars[off + 1]);
18280 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018281
18282 if (win != NULL && varname != NULL && varp != NULL)
18283 {
18284#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018285 need_switch_win = !(tp == curtab && win == curwin);
18286 if (!need_switch_win
18287 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018289 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018290 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018291 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018292 long numval;
18293 char_u *strval;
18294 int error = FALSE;
18295
18296 ++varname;
18297 numval = get_tv_number_chk(varp, &error);
18298 strval = get_tv_string_buf_chk(varp, nbuf);
18299 if (!error && strval != NULL)
18300 set_option_value(varname, numval, strval, OPT_LOCAL);
18301 }
18302 else
18303 {
18304 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18305 if (winvarname != NULL)
18306 {
18307 STRCPY(winvarname, "w:");
18308 STRCPY(winvarname + 2, varname);
18309 set_var(winvarname, varp, TRUE);
18310 vim_free(winvarname);
18311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018312 }
18313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018315 if (need_switch_win)
18316 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018317#endif
18318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018319}
18320
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018321#ifdef FEAT_CRYPT
18322/*
18323 * "sha256({string})" function
18324 */
18325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018326f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018327{
18328 char_u *p;
18329
18330 p = get_tv_string(&argvars[0]);
18331 rettv->vval.v_string = vim_strsave(
18332 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18333 rettv->v_type = VAR_STRING;
18334}
18335#endif /* FEAT_CRYPT */
18336
Bram Moolenaar071d4272004-06-13 20:20:40 +000018337/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018338 * "shellescape({string})" function
18339 */
18340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018341f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018342{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018343 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018344 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018345 rettv->v_type = VAR_STRING;
18346}
18347
18348/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018349 * shiftwidth() function
18350 */
18351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018352f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018353{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018354 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018355}
18356
18357/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018358 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018359 */
18360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018361f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018362{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018363 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018364
Bram Moolenaar0d660222005-01-07 21:51:51 +000018365 p = get_tv_string(&argvars[0]);
18366 rettv->vval.v_string = vim_strsave(p);
18367 simplify_filename(rettv->vval.v_string); /* simplify in place */
18368 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018369}
18370
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018371#ifdef FEAT_FLOAT
18372/*
18373 * "sin()" function
18374 */
18375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018376f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018377{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018378 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018379
18380 rettv->v_type = VAR_FLOAT;
18381 if (get_float_arg(argvars, &f) == OK)
18382 rettv->vval.v_float = sin(f);
18383 else
18384 rettv->vval.v_float = 0.0;
18385}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018386
18387/*
18388 * "sinh()" function
18389 */
18390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018391f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018392{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018393 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018394
18395 rettv->v_type = VAR_FLOAT;
18396 if (get_float_arg(argvars, &f) == OK)
18397 rettv->vval.v_float = sinh(f);
18398 else
18399 rettv->vval.v_float = 0.0;
18400}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018401#endif
18402
Bram Moolenaar0d660222005-01-07 21:51:51 +000018403static int
18404#ifdef __BORLANDC__
18405 _RTLENTRYF
18406#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018407 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018408static int
18409#ifdef __BORLANDC__
18410 _RTLENTRYF
18411#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018412 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018413
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018414/* struct used in the array that's given to qsort() */
18415typedef struct
18416{
18417 listitem_T *item;
18418 int idx;
18419} sortItem_T;
18420
Bram Moolenaar0b962472016-02-22 22:51:33 +010018421/* struct storing information about current sort */
18422typedef struct
18423{
18424 int item_compare_ic;
18425 int item_compare_numeric;
18426 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018427#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018428 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018429#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018430 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018431 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018432 dict_T *item_compare_selfdict;
18433 int item_compare_func_err;
18434 int item_compare_keep_zero;
18435} sortinfo_T;
18436static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018437static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018438#define ITEM_COMPARE_FAIL 999
18439
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018441 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018442 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018443 static int
18444#ifdef __BORLANDC__
18445_RTLENTRYF
18446#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018447item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018448{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018449 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018450 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018451 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018452 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018453 int res;
18454 char_u numbuf1[NUMBUFLEN];
18455 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018456
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018457 si1 = (sortItem_T *)s1;
18458 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018459 tv1 = &si1->item->li_tv;
18460 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018461
Bram Moolenaar0b962472016-02-22 22:51:33 +010018462 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018463 {
18464 long v1 = get_tv_number(tv1);
18465 long v2 = get_tv_number(tv2);
18466
18467 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18468 }
18469
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018470#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018471 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018472 {
18473 float_T v1 = get_tv_float(tv1);
18474 float_T v2 = get_tv_float(tv2);
18475
18476 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18477 }
18478#endif
18479
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018480 /* tv2string() puts quotes around a string and allocates memory. Don't do
18481 * that for string variables. Use a single quote when comparing with a
18482 * non-string to do what the docs promise. */
18483 if (tv1->v_type == VAR_STRING)
18484 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018485 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018486 p1 = (char_u *)"'";
18487 else
18488 p1 = tv1->vval.v_string;
18489 }
18490 else
18491 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18492 if (tv2->v_type == VAR_STRING)
18493 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018494 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018495 p2 = (char_u *)"'";
18496 else
18497 p2 = tv2->vval.v_string;
18498 }
18499 else
18500 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018501 if (p1 == NULL)
18502 p1 = (char_u *)"";
18503 if (p2 == NULL)
18504 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018505 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018506 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018507 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018508 res = STRICMP(p1, p2);
18509 else
18510 res = STRCMP(p1, p2);
18511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018512 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018513 {
18514 double n1, n2;
18515 n1 = strtod((char *)p1, (char **)&p1);
18516 n2 = strtod((char *)p2, (char **)&p2);
18517 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18518 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018519
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018520 /* When the result would be zero, compare the item indexes. Makes the
18521 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018522 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018523 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018524
Bram Moolenaar0d660222005-01-07 21:51:51 +000018525 vim_free(tofree1);
18526 vim_free(tofree2);
18527 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018528}
18529
18530 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018531#ifdef __BORLANDC__
18532_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018533#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018534item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018535{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018536 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018537 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018538 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018539 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018540 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018541 char_u *func_name;
18542 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018543
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018544 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018545 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018546 return 0;
18547
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018548 si1 = (sortItem_T *)s1;
18549 si2 = (sortItem_T *)s2;
18550
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018551 if (partial == NULL)
18552 func_name = sortinfo->item_compare_func;
18553 else
18554 func_name = partial->pt_name;
18555
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018556 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018557 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018558 copy_tv(&si1->item->li_tv, &argv[0]);
18559 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018560
18561 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018562 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018563 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018564 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018565 clear_tv(&argv[0]);
18566 clear_tv(&argv[1]);
18567
18568 if (res == FAIL)
18569 res = ITEM_COMPARE_FAIL;
18570 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018571 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18572 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018573 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018574 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018575
18576 /* When the result would be zero, compare the pointers themselves. Makes
18577 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018578 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018579 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018580
Bram Moolenaar0d660222005-01-07 21:51:51 +000018581 return res;
18582}
18583
18584/*
18585 * "sort({list})" function
18586 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018588do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018589{
Bram Moolenaar33570922005-01-25 22:26:29 +000018590 list_T *l;
18591 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018592 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018593 sortinfo_T *old_sortinfo;
18594 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018595 long len;
18596 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597
Bram Moolenaar0b962472016-02-22 22:51:33 +010018598 /* Pointer to current info struct used in compare function. Save and
18599 * restore the current one for nested calls. */
18600 old_sortinfo = sortinfo;
18601 sortinfo = &info;
18602
Bram Moolenaar0d660222005-01-07 21:51:51 +000018603 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018604 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018605 else
18606 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018607 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018608 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018609 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18610 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018611 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018612 rettv->vval.v_list = l;
18613 rettv->v_type = VAR_LIST;
18614 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615
Bram Moolenaar0d660222005-01-07 21:51:51 +000018616 len = list_len(l);
18617 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018618 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619
Bram Moolenaar0b962472016-02-22 22:51:33 +010018620 info.item_compare_ic = FALSE;
18621 info.item_compare_numeric = FALSE;
18622 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018623#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018624 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018625#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018626 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018627 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018628 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018629 if (argvars[1].v_type != VAR_UNKNOWN)
18630 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018631 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018632 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018633 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018634 else if (argvars[1].v_type == VAR_PARTIAL)
18635 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018636 else
18637 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018638 int error = FALSE;
18639
18640 i = get_tv_number_chk(&argvars[1], &error);
18641 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018642 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018643 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018644 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010018645 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018646 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010018647 else if (i != 0)
18648 {
18649 EMSG(_(e_invarg));
18650 goto theend;
18651 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018652 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018653 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010018654 if (*info.item_compare_func == NUL)
18655 {
18656 /* empty string means default sort */
18657 info.item_compare_func = NULL;
18658 }
18659 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018660 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018661 info.item_compare_func = NULL;
18662 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018663 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018664 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018665 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018666 info.item_compare_func = NULL;
18667 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018668 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018669#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018670 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018671 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018672 info.item_compare_func = NULL;
18673 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018674 }
18675#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018676 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018677 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018678 info.item_compare_func = NULL;
18679 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018680 }
18681 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018682 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018683
18684 if (argvars[2].v_type != VAR_UNKNOWN)
18685 {
18686 /* optional third argument: {dict} */
18687 if (argvars[2].v_type != VAR_DICT)
18688 {
18689 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010018690 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018691 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018692 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018693 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695
Bram Moolenaar0d660222005-01-07 21:51:51 +000018696 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018697 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018698 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018699 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018700
Bram Moolenaar327aa022014-03-25 18:24:23 +010018701 i = 0;
18702 if (sort)
18703 {
18704 /* sort(): ptrs will be the list to sort */
18705 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018706 {
18707 ptrs[i].item = li;
18708 ptrs[i].idx = i;
18709 ++i;
18710 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018711
Bram Moolenaar0b962472016-02-22 22:51:33 +010018712 info.item_compare_func_err = FALSE;
18713 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018714 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018715 if ((info.item_compare_func != NULL
18716 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018717 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018718 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018719 EMSG(_("E702: Sort compare function failed"));
18720 else
18721 {
18722 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018723 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010018724 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018725 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010018726 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018727
Bram Moolenaar0b962472016-02-22 22:51:33 +010018728 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018729 {
18730 /* Clear the List and append the items in sorted order. */
18731 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18732 l->lv_len = 0;
18733 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018734 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018735 }
18736 }
18737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018739 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018740 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018741
18742 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018743 info.item_compare_func_err = FALSE;
18744 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018745 item_compare_func_ptr = info.item_compare_func != NULL
18746 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010018747 ? item_compare2 : item_compare;
18748
18749 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18750 li = li->li_next)
18751 {
18752 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18753 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018754 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018755 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018756 {
18757 EMSG(_("E882: Uniq compare function failed"));
18758 break;
18759 }
18760 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018761
Bram Moolenaar0b962472016-02-22 22:51:33 +010018762 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018763 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018764 while (--i >= 0)
18765 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018766 li = ptrs[i].item->li_next;
18767 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018768 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018769 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018770 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018771 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018772 list_fix_watch(l, li);
18773 listitem_free(li);
18774 l->lv_len--;
18775 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018776 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018777 }
18778
18779 vim_free(ptrs);
18780 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018781theend:
18782 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018783}
18784
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018785/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018786 * "sort({list})" function
18787 */
18788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018789f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018790{
18791 do_sort_uniq(argvars, rettv, TRUE);
18792}
18793
18794/*
18795 * "uniq({list})" function
18796 */
18797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018798f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018799{
18800 do_sort_uniq(argvars, rettv, FALSE);
18801}
18802
18803/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018804 * "soundfold({word})" function
18805 */
18806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018807f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018808{
18809 char_u *s;
18810
18811 rettv->v_type = VAR_STRING;
18812 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018813#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018814 rettv->vval.v_string = eval_soundfold(s);
18815#else
18816 rettv->vval.v_string = vim_strsave(s);
18817#endif
18818}
18819
18820/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018821 * "spellbadword()" function
18822 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018824f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018825{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018826 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018827 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018828 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018829
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018830 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018831 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018832
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018833#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018834 if (argvars[0].v_type == VAR_UNKNOWN)
18835 {
18836 /* Find the start and length of the badly spelled word. */
18837 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18838 if (len != 0)
18839 word = ml_get_cursor();
18840 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018841 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018842 {
18843 char_u *str = get_tv_string_chk(&argvars[0]);
18844 int capcol = -1;
18845
18846 if (str != NULL)
18847 {
18848 /* Check the argument for spelling. */
18849 while (*str != NUL)
18850 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018851 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018852 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018853 {
18854 word = str;
18855 break;
18856 }
18857 str += len;
18858 }
18859 }
18860 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018861#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018862
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018863 list_append_string(rettv->vval.v_list, word, len);
18864 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018865 attr == HLF_SPB ? "bad" :
18866 attr == HLF_SPR ? "rare" :
18867 attr == HLF_SPL ? "local" :
18868 attr == HLF_SPC ? "caps" :
18869 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018870}
18871
18872/*
18873 * "spellsuggest()" function
18874 */
18875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018876f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018877{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018878#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018879 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018880 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018881 int maxcount;
18882 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018883 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018884 listitem_T *li;
18885 int need_capital = FALSE;
18886#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018887
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018888 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018889 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018890
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018891#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018892 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018893 {
18894 str = get_tv_string(&argvars[0]);
18895 if (argvars[1].v_type != VAR_UNKNOWN)
18896 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018897 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018898 if (maxcount <= 0)
18899 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018900 if (argvars[2].v_type != VAR_UNKNOWN)
18901 {
18902 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18903 if (typeerr)
18904 return;
18905 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018906 }
18907 else
18908 maxcount = 25;
18909
Bram Moolenaar4770d092006-01-12 23:22:24 +000018910 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018911
18912 for (i = 0; i < ga.ga_len; ++i)
18913 {
18914 str = ((char_u **)ga.ga_data)[i];
18915
18916 li = listitem_alloc();
18917 if (li == NULL)
18918 vim_free(str);
18919 else
18920 {
18921 li->li_tv.v_type = VAR_STRING;
18922 li->li_tv.v_lock = 0;
18923 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018924 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018925 }
18926 }
18927 ga_clear(&ga);
18928 }
18929#endif
18930}
18931
Bram Moolenaar0d660222005-01-07 21:51:51 +000018932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018933f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018934{
18935 char_u *str;
18936 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018937 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018938 regmatch_T regmatch;
18939 char_u patbuf[NUMBUFLEN];
18940 char_u *save_cpo;
18941 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018942 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018943 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018944 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018945
18946 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18947 save_cpo = p_cpo;
18948 p_cpo = (char_u *)"";
18949
18950 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018951 if (argvars[1].v_type != VAR_UNKNOWN)
18952 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018953 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18954 if (pat == NULL)
18955 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018956 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018957 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018958 }
18959 if (pat == NULL || *pat == NUL)
18960 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018961
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018962 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018964 if (typeerr)
18965 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018966
Bram Moolenaar0d660222005-01-07 21:51:51 +000018967 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18968 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018970 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018971 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018972 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018973 if (*str == NUL)
18974 match = FALSE; /* empty item at the end */
18975 else
18976 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018977 if (match)
18978 end = regmatch.startp[0];
18979 else
18980 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018981 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18982 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018983 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018984 if (list_append_string(rettv->vval.v_list, str,
18985 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018986 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018987 }
18988 if (!match)
18989 break;
18990 /* Advance to just after the match. */
18991 if (regmatch.endp[0] > str)
18992 col = 0;
18993 else
18994 {
18995 /* Don't get stuck at the same match. */
18996#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018997 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018998#else
18999 col = 1;
19000#endif
19001 }
19002 str = regmatch.endp[0];
19003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019004
Bram Moolenaar473de612013-06-08 18:19:48 +020019005 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007
Bram Moolenaar0d660222005-01-07 21:51:51 +000019008 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019009}
19010
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019011#ifdef FEAT_FLOAT
19012/*
19013 * "sqrt()" function
19014 */
19015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019016f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019017{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019018 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019019
19020 rettv->v_type = VAR_FLOAT;
19021 if (get_float_arg(argvars, &f) == OK)
19022 rettv->vval.v_float = sqrt(f);
19023 else
19024 rettv->vval.v_float = 0.0;
19025}
19026
19027/*
19028 * "str2float()" function
19029 */
19030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019031f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019032{
19033 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19034
19035 if (*p == '+')
19036 p = skipwhite(p + 1);
19037 (void)string2float(p, &rettv->vval.v_float);
19038 rettv->v_type = VAR_FLOAT;
19039}
19040#endif
19041
Bram Moolenaar2c932302006-03-18 21:42:09 +000019042/*
19043 * "str2nr()" function
19044 */
19045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019046f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019047{
19048 int base = 10;
19049 char_u *p;
19050 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019051 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019052
19053 if (argvars[1].v_type != VAR_UNKNOWN)
19054 {
19055 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019056 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019057 {
19058 EMSG(_(e_invarg));
19059 return;
19060 }
19061 }
19062
19063 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019064 if (*p == '+')
19065 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019066 switch (base)
19067 {
19068 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19069 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19070 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19071 default: what = 0;
19072 }
19073 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019074 rettv->vval.v_number = n;
19075}
19076
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077#ifdef HAVE_STRFTIME
19078/*
19079 * "strftime({format}[, {time}])" function
19080 */
19081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019082f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083{
19084 char_u result_buf[256];
19085 struct tm *curtime;
19086 time_t seconds;
19087 char_u *p;
19088
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019089 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019090
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019091 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019092 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093 seconds = time(NULL);
19094 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019095 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019096 curtime = localtime(&seconds);
19097 /* MSVC returns NULL for an invalid value of seconds. */
19098 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019099 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019100 else
19101 {
19102# ifdef FEAT_MBYTE
19103 vimconv_T conv;
19104 char_u *enc;
19105
19106 conv.vc_type = CONV_NONE;
19107 enc = enc_locale();
19108 convert_setup(&conv, p_enc, enc);
19109 if (conv.vc_type != CONV_NONE)
19110 p = string_convert(&conv, p, NULL);
19111# endif
19112 if (p != NULL)
19113 (void)strftime((char *)result_buf, sizeof(result_buf),
19114 (char *)p, curtime);
19115 else
19116 result_buf[0] = NUL;
19117
19118# ifdef FEAT_MBYTE
19119 if (conv.vc_type != CONV_NONE)
19120 vim_free(p);
19121 convert_setup(&conv, enc, p_enc);
19122 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019123 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019124 else
19125# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019126 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019127
19128# ifdef FEAT_MBYTE
19129 /* Release conversion descriptors */
19130 convert_setup(&conv, NULL, NULL);
19131 vim_free(enc);
19132# endif
19133 }
19134}
19135#endif
19136
19137/*
19138 * "stridx()" function
19139 */
19140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019141f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142{
19143 char_u buf[NUMBUFLEN];
19144 char_u *needle;
19145 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019146 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019147 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019148 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019149
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019150 needle = get_tv_string_chk(&argvars[1]);
19151 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019152 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019153 if (needle == NULL || haystack == NULL)
19154 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155
Bram Moolenaar33570922005-01-25 22:26:29 +000019156 if (argvars[2].v_type != VAR_UNKNOWN)
19157 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019158 int error = FALSE;
19159
19160 start_idx = get_tv_number_chk(&argvars[2], &error);
19161 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019162 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019163 if (start_idx >= 0)
19164 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019165 }
19166
19167 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19168 if (pos != NULL)
19169 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019170}
19171
19172/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019173 * "string()" function
19174 */
19175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019176f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019177{
19178 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019179 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019180
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019181 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019182 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019183 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019184 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019185 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019186}
19187
19188/*
19189 * "strlen()" function
19190 */
19191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019192f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019193{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019194 rettv->vval.v_number = (varnumber_T)(STRLEN(
19195 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019196}
19197
19198/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019199 * "strchars()" function
19200 */
19201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019202f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019203{
19204 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019205 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019206#ifdef FEAT_MBYTE
19207 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019208 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019209#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019210
19211 if (argvars[1].v_type != VAR_UNKNOWN)
19212 skipcc = get_tv_number_chk(&argvars[1], NULL);
19213 if (skipcc < 0 || skipcc > 1)
19214 EMSG(_(e_invarg));
19215 else
19216 {
19217#ifdef FEAT_MBYTE
19218 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19219 while (*s != NUL)
19220 {
19221 func_mb_ptr2char_adv(&s);
19222 ++len;
19223 }
19224 rettv->vval.v_number = len;
19225#else
19226 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19227#endif
19228 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019229}
19230
19231/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019232 * "strdisplaywidth()" function
19233 */
19234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019235f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019236{
19237 char_u *s = get_tv_string(&argvars[0]);
19238 int col = 0;
19239
19240 if (argvars[1].v_type != VAR_UNKNOWN)
19241 col = get_tv_number(&argvars[1]);
19242
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019243 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019244}
19245
19246/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019247 * "strwidth()" function
19248 */
19249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019250f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019251{
19252 char_u *s = get_tv_string(&argvars[0]);
19253
19254 rettv->vval.v_number = (varnumber_T)(
19255#ifdef FEAT_MBYTE
19256 mb_string2cells(s, -1)
19257#else
19258 STRLEN(s)
19259#endif
19260 );
19261}
19262
19263/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019264 * "strpart()" function
19265 */
19266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019267f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019268{
19269 char_u *p;
19270 int n;
19271 int len;
19272 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019273 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019275 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019276 slen = (int)STRLEN(p);
19277
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019278 n = get_tv_number_chk(&argvars[1], &error);
19279 if (error)
19280 len = 0;
19281 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019282 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019283 else
19284 len = slen - n; /* default len: all bytes that are available. */
19285
19286 /*
19287 * Only return the overlap between the specified part and the actual
19288 * string.
19289 */
19290 if (n < 0)
19291 {
19292 len += n;
19293 n = 0;
19294 }
19295 else if (n > slen)
19296 n = slen;
19297 if (len < 0)
19298 len = 0;
19299 else if (n + len > slen)
19300 len = slen - n;
19301
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019302 rettv->v_type = VAR_STRING;
19303 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304}
19305
19306/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019307 * "strridx()" function
19308 */
19309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019310f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019311{
19312 char_u buf[NUMBUFLEN];
19313 char_u *needle;
19314 char_u *haystack;
19315 char_u *rest;
19316 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019317 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019318
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019319 needle = get_tv_string_chk(&argvars[1]);
19320 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019321
19322 rettv->vval.v_number = -1;
19323 if (needle == NULL || haystack == NULL)
19324 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019325
19326 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019327 if (argvars[2].v_type != VAR_UNKNOWN)
19328 {
19329 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019330 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019331 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019332 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019333 }
19334 else
19335 end_idx = haystack_len;
19336
Bram Moolenaar0d660222005-01-07 21:51:51 +000019337 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019338 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019339 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019340 lastmatch = haystack + end_idx;
19341 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019342 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019343 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019344 for (rest = haystack; *rest != '\0'; ++rest)
19345 {
19346 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019347 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019348 break;
19349 lastmatch = rest;
19350 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019351 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019352
19353 if (lastmatch == NULL)
19354 rettv->vval.v_number = -1;
19355 else
19356 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19357}
19358
19359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360 * "strtrans()" function
19361 */
19362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019363f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019365 rettv->v_type = VAR_STRING;
19366 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367}
19368
19369/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019370 * "submatch()" function
19371 */
19372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019373f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019374{
Bram Moolenaar41571762014-04-02 19:00:58 +020019375 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019376 int no;
19377 int retList = 0;
19378
19379 no = (int)get_tv_number_chk(&argvars[0], &error);
19380 if (error)
19381 return;
19382 error = FALSE;
19383 if (argvars[1].v_type != VAR_UNKNOWN)
19384 retList = get_tv_number_chk(&argvars[1], &error);
19385 if (error)
19386 return;
19387
19388 if (retList == 0)
19389 {
19390 rettv->v_type = VAR_STRING;
19391 rettv->vval.v_string = reg_submatch(no);
19392 }
19393 else
19394 {
19395 rettv->v_type = VAR_LIST;
19396 rettv->vval.v_list = reg_submatch_list(no);
19397 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019398}
19399
19400/*
19401 * "substitute()" function
19402 */
19403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019404f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019405{
19406 char_u patbuf[NUMBUFLEN];
19407 char_u subbuf[NUMBUFLEN];
19408 char_u flagsbuf[NUMBUFLEN];
19409
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019410 char_u *str = get_tv_string_chk(&argvars[0]);
19411 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19412 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19413 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19414
Bram Moolenaar0d660222005-01-07 21:51:51 +000019415 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019416 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19417 rettv->vval.v_string = NULL;
19418 else
19419 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019420}
19421
19422/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019423 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019426f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019427{
19428 int id = 0;
19429#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019430 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019431 long col;
19432 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019433 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019434
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019435 lnum = get_tv_lnum(argvars); /* -1 on type error */
19436 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19437 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019438
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019439 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019440 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019441 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019442#endif
19443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019444 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019445}
19446
19447/*
19448 * "synIDattr(id, what [, mode])" function
19449 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019451f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019452{
19453 char_u *p = NULL;
19454#ifdef FEAT_SYN_HL
19455 int id;
19456 char_u *what;
19457 char_u *mode;
19458 char_u modebuf[NUMBUFLEN];
19459 int modec;
19460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019461 id = get_tv_number(&argvars[0]);
19462 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019463 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019464 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019465 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019467 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019468 modec = 0; /* replace invalid with current */
19469 }
19470 else
19471 {
19472#ifdef FEAT_GUI
19473 if (gui.in_use)
19474 modec = 'g';
19475 else
19476#endif
19477 if (t_colors > 1)
19478 modec = 'c';
19479 else
19480 modec = 't';
19481 }
19482
19483
19484 switch (TOLOWER_ASC(what[0]))
19485 {
19486 case 'b':
19487 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19488 p = highlight_color(id, what, modec);
19489 else /* bold */
19490 p = highlight_has_attr(id, HL_BOLD, modec);
19491 break;
19492
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019493 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019494 p = highlight_color(id, what, modec);
19495 break;
19496
19497 case 'i':
19498 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19499 p = highlight_has_attr(id, HL_INVERSE, modec);
19500 else /* italic */
19501 p = highlight_has_attr(id, HL_ITALIC, modec);
19502 break;
19503
19504 case 'n': /* name */
19505 p = get_highlight_name(NULL, id - 1);
19506 break;
19507
19508 case 'r': /* reverse */
19509 p = highlight_has_attr(id, HL_INVERSE, modec);
19510 break;
19511
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019512 case 's':
19513 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19514 p = highlight_color(id, what, modec);
19515 else /* standout */
19516 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019517 break;
19518
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019519 case 'u':
19520 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19521 /* underline */
19522 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19523 else
19524 /* undercurl */
19525 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019526 break;
19527 }
19528
19529 if (p != NULL)
19530 p = vim_strsave(p);
19531#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019532 rettv->v_type = VAR_STRING;
19533 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534}
19535
19536/*
19537 * "synIDtrans(id)" function
19538 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019540f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019541{
19542 int id;
19543
19544#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019545 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019546
19547 if (id > 0)
19548 id = syn_get_final_id(id);
19549 else
19550#endif
19551 id = 0;
19552
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019553 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554}
19555
19556/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019557 * "synconcealed(lnum, col)" function
19558 */
19559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019560f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019561{
19562#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19563 long lnum;
19564 long col;
19565 int syntax_flags = 0;
19566 int cchar;
19567 int matchid = 0;
19568 char_u str[NUMBUFLEN];
19569#endif
19570
19571 rettv->v_type = VAR_LIST;
19572 rettv->vval.v_list = NULL;
19573
19574#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19575 lnum = get_tv_lnum(argvars); /* -1 on type error */
19576 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19577
19578 vim_memset(str, NUL, sizeof(str));
19579
19580 if (rettv_list_alloc(rettv) != FAIL)
19581 {
19582 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19583 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19584 && curwin->w_p_cole > 0)
19585 {
19586 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19587 syntax_flags = get_syntax_info(&matchid);
19588
19589 /* get the conceal character */
19590 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19591 {
19592 cchar = syn_get_sub_char();
19593 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19594 cchar = lcs_conceal;
19595 if (cchar != NUL)
19596 {
19597# ifdef FEAT_MBYTE
19598 if (has_mbyte)
19599 (*mb_char2bytes)(cchar, str);
19600 else
19601# endif
19602 str[0] = cchar;
19603 }
19604 }
19605 }
19606
19607 list_append_number(rettv->vval.v_list,
19608 (syntax_flags & HL_CONCEAL) != 0);
19609 /* -1 to auto-determine strlen */
19610 list_append_string(rettv->vval.v_list, str, -1);
19611 list_append_number(rettv->vval.v_list, matchid);
19612 }
19613#endif
19614}
19615
19616/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019617 * "synstack(lnum, col)" function
19618 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019620f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019621{
19622#ifdef FEAT_SYN_HL
19623 long lnum;
19624 long col;
19625 int i;
19626 int id;
19627#endif
19628
19629 rettv->v_type = VAR_LIST;
19630 rettv->vval.v_list = NULL;
19631
19632#ifdef FEAT_SYN_HL
19633 lnum = get_tv_lnum(argvars); /* -1 on type error */
19634 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19635
19636 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019637 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019638 && rettv_list_alloc(rettv) != FAIL)
19639 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019640 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019641 for (i = 0; ; ++i)
19642 {
19643 id = syn_get_stack_item(i);
19644 if (id < 0)
19645 break;
19646 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19647 break;
19648 }
19649 }
19650#endif
19651}
19652
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019654get_cmd_output_as_rettv(
19655 typval_T *argvars,
19656 typval_T *rettv,
19657 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019658{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019659 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019660 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019661 char_u *infile = NULL;
19662 char_u buf[NUMBUFLEN];
19663 int err = FALSE;
19664 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019665 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019666 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019667
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019668 rettv->v_type = VAR_STRING;
19669 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019670 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019671 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019672
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019673 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019674 {
19675 /*
19676 * Write the string to a temp file, to be used for input of the shell
19677 * command.
19678 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019679 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019680 {
19681 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019682 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019683 }
19684
19685 fd = mch_fopen((char *)infile, WRITEBIN);
19686 if (fd == NULL)
19687 {
19688 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019689 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019690 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019691 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019692 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019693 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19694 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019695 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019696 else
19697 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019698 size_t len;
19699
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019700 p = get_tv_string_buf_chk(&argvars[1], buf);
19701 if (p == NULL)
19702 {
19703 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019704 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019705 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019706 len = STRLEN(p);
19707 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019708 err = TRUE;
19709 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019710 if (fclose(fd) != 0)
19711 err = TRUE;
19712 if (err)
19713 {
19714 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019715 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019716 }
19717 }
19718
Bram Moolenaar52a72462014-08-29 15:53:52 +020019719 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19720 * echoes typeahead, that messes up the display. */
19721 if (!msg_silent)
19722 flags += SHELL_COOKED;
19723
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019724 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019725 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019726 int len;
19727 listitem_T *li;
19728 char_u *s = NULL;
19729 char_u *start;
19730 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019731 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732
Bram Moolenaar52a72462014-08-29 15:53:52 +020019733 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019734 if (res == NULL)
19735 goto errret;
19736
19737 list = list_alloc();
19738 if (list == NULL)
19739 goto errret;
19740
19741 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019742 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019743 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019744 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019745 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019746 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019747
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019748 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019749 if (s == NULL)
19750 goto errret;
19751
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019752 for (p = s; start < end; ++p, ++start)
19753 *p = *start == NUL ? NL : *start;
19754 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019755
19756 li = listitem_alloc();
19757 if (li == NULL)
19758 {
19759 vim_free(s);
19760 goto errret;
19761 }
19762 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019763 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019764 li->li_tv.vval.v_string = s;
19765 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019767
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019768 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019769 rettv->v_type = VAR_LIST;
19770 rettv->vval.v_list = list;
19771 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019773 else
19774 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019775 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019776#ifdef USE_CR
19777 /* translate <CR> into <NL> */
19778 if (res != NULL)
19779 {
19780 char_u *s;
19781
19782 for (s = res; *s; ++s)
19783 {
19784 if (*s == CAR)
19785 *s = NL;
19786 }
19787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788#else
19789# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019790 /* translate <CR><NL> into <NL> */
19791 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019793 char_u *s, *d;
19794
19795 d = res;
19796 for (s = res; *s; ++s)
19797 {
19798 if (s[0] == CAR && s[1] == NL)
19799 ++s;
19800 *d++ = *s;
19801 }
19802 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804# endif
19805#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019806 rettv->vval.v_string = res;
19807 res = NULL;
19808 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019809
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019810errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019811 if (infile != NULL)
19812 {
19813 mch_remove(infile);
19814 vim_free(infile);
19815 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019816 if (res != NULL)
19817 vim_free(res);
19818 if (list != NULL)
19819 list_free(list, TRUE);
19820}
19821
19822/*
19823 * "system()" function
19824 */
19825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019826f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019827{
19828 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19829}
19830
19831/*
19832 * "systemlist()" function
19833 */
19834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019835f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019836{
19837 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838}
19839
19840/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019841 * "tabpagebuflist()" function
19842 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019844f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019845{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019846#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019847 tabpage_T *tp;
19848 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019849
19850 if (argvars[0].v_type == VAR_UNKNOWN)
19851 wp = firstwin;
19852 else
19853 {
19854 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19855 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019856 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019857 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019858 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019859 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019860 for (; wp != NULL; wp = wp->w_next)
19861 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019862 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019863 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019864 }
19865#endif
19866}
19867
19868
19869/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019870 * "tabpagenr()" function
19871 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019873f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019874{
19875 int nr = 1;
19876#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019877 char_u *arg;
19878
19879 if (argvars[0].v_type != VAR_UNKNOWN)
19880 {
19881 arg = get_tv_string_chk(&argvars[0]);
19882 nr = 0;
19883 if (arg != NULL)
19884 {
19885 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019886 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019887 else
19888 EMSG2(_(e_invexpr2), arg);
19889 }
19890 }
19891 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019892 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019893#endif
19894 rettv->vval.v_number = nr;
19895}
19896
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019897
19898#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019899static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019900
19901/*
19902 * Common code for tabpagewinnr() and winnr().
19903 */
19904 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019905get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019906{
19907 win_T *twin;
19908 int nr = 1;
19909 win_T *wp;
19910 char_u *arg;
19911
19912 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19913 if (argvar->v_type != VAR_UNKNOWN)
19914 {
19915 arg = get_tv_string_chk(argvar);
19916 if (arg == NULL)
19917 nr = 0; /* type error; errmsg already given */
19918 else if (STRCMP(arg, "$") == 0)
19919 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19920 else if (STRCMP(arg, "#") == 0)
19921 {
19922 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19923 if (twin == NULL)
19924 nr = 0;
19925 }
19926 else
19927 {
19928 EMSG2(_(e_invexpr2), arg);
19929 nr = 0;
19930 }
19931 }
19932
19933 if (nr > 0)
19934 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19935 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019936 {
19937 if (wp == NULL)
19938 {
19939 /* didn't find it in this tabpage */
19940 nr = 0;
19941 break;
19942 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019943 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019944 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019945 return nr;
19946}
19947#endif
19948
19949/*
19950 * "tabpagewinnr()" function
19951 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019953f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019954{
19955 int nr = 1;
19956#ifdef FEAT_WINDOWS
19957 tabpage_T *tp;
19958
19959 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19960 if (tp == NULL)
19961 nr = 0;
19962 else
19963 nr = get_winnr(tp, &argvars[1]);
19964#endif
19965 rettv->vval.v_number = nr;
19966}
19967
19968
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019969/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019970 * "tagfiles()" function
19971 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019973f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019974{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019975 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019976 tagname_T tn;
19977 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019978
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019979 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019980 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019981 fname = alloc(MAXPATHL);
19982 if (fname == NULL)
19983 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019984
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019985 for (first = TRUE; ; first = FALSE)
19986 if (get_tagfname(&tn, first, fname) == FAIL
19987 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019988 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019989 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019990 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019991}
19992
19993/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019994 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019995 */
19996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019997f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019998{
19999 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020000
20001 tag_pattern = get_tv_string(&argvars[0]);
20002
20003 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020004 if (*tag_pattern == NUL)
20005 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020006
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020007 if (rettv_list_alloc(rettv) == OK)
20008 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020009}
20010
20011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020012 * "tempname()" function
20013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020015f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016{
20017 static int x = 'A';
20018
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020019 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020020 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020021
20022 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20023 * names. Skip 'I' and 'O', they are used for shell redirection. */
20024 do
20025 {
20026 if (x == 'Z')
20027 x = '0';
20028 else if (x == '9')
20029 x = 'A';
20030 else
20031 {
20032#ifdef EBCDIC
20033 if (x == 'I')
20034 x = 'J';
20035 else if (x == 'R')
20036 x = 'S';
20037 else
20038#endif
20039 ++x;
20040 }
20041 } while (x == 'I' || x == 'O');
20042}
20043
20044/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020045 * "test(list)" function: Just checking the walls...
20046 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020048f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020049{
20050 /* Used for unit testing. Change the code below to your liking. */
20051#if 0
20052 listitem_T *li;
20053 list_T *l;
20054 char_u *bad, *good;
20055
20056 if (argvars[0].v_type != VAR_LIST)
20057 return;
20058 l = argvars[0].vval.v_list;
20059 if (l == NULL)
20060 return;
20061 li = l->lv_first;
20062 if (li == NULL)
20063 return;
20064 bad = get_tv_string(&li->li_tv);
20065 li = li->li_next;
20066 if (li == NULL)
20067 return;
20068 good = get_tv_string(&li->li_tv);
20069 rettv->vval.v_number = test_edit_score(bad, good);
20070#endif
20071}
20072
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020073#ifdef FEAT_FLOAT
20074/*
20075 * "tan()" function
20076 */
20077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020078f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020079{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020080 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020081
20082 rettv->v_type = VAR_FLOAT;
20083 if (get_float_arg(argvars, &f) == OK)
20084 rettv->vval.v_float = tan(f);
20085 else
20086 rettv->vval.v_float = 0.0;
20087}
20088
20089/*
20090 * "tanh()" function
20091 */
20092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020093f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020094{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020095 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020096
20097 rettv->v_type = VAR_FLOAT;
20098 if (get_float_arg(argvars, &f) == OK)
20099 rettv->vval.v_float = tanh(f);
20100 else
20101 rettv->vval.v_float = 0.0;
20102}
20103#endif
20104
Bram Moolenaar975b5272016-03-15 23:10:59 +010020105#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20106/*
20107 * Get a callback from "arg". It can be a Funcref or a function name.
20108 * When "arg" is zero return an empty string.
20109 * Return NULL for an invalid argument.
20110 */
20111 char_u *
20112get_callback(typval_T *arg, partial_T **pp)
20113{
20114 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20115 {
20116 *pp = arg->vval.v_partial;
20117 return (*pp)->pt_name;
20118 }
20119 *pp = NULL;
20120 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20121 return arg->vval.v_string;
20122 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20123 return (char_u *)"";
20124 EMSG(_("E921: Invalid callback argument"));
20125 return NULL;
20126}
20127#endif
20128
20129#ifdef FEAT_TIMERS
20130/*
20131 * "timer_start(time, callback [, options])" function
20132 */
20133 static void
20134f_timer_start(typval_T *argvars, typval_T *rettv)
20135{
20136 long msec = get_tv_number(&argvars[0]);
20137 timer_T *timer;
20138 int repeat = 0;
20139 char_u *callback;
20140 dict_T *dict;
20141
20142 if (argvars[2].v_type != VAR_UNKNOWN)
20143 {
20144 if (argvars[2].v_type != VAR_DICT
20145 || (dict = argvars[2].vval.v_dict) == NULL)
20146 {
20147 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20148 return;
20149 }
20150 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20151 repeat = get_dict_number(dict, (char_u *)"repeat");
20152 }
20153
20154 timer = create_timer(msec, repeat);
20155 callback = get_callback(&argvars[1], &timer->tr_partial);
20156 if (callback == NULL)
20157 {
20158 stop_timer(timer);
20159 rettv->vval.v_number = -1;
20160 }
20161 else
20162 {
20163 timer->tr_callback = vim_strsave(callback);
20164 rettv->vval.v_number = timer->tr_id;
20165 }
20166}
20167
20168/*
20169 * "timer_stop(timer)" function
20170 */
20171 static void
20172f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20173{
20174 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20175
20176 if (timer != NULL)
20177 stop_timer(timer);
20178}
20179#endif
20180
Bram Moolenaard52d9742005-08-21 22:20:28 +000020181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020182 * "tolower(string)" function
20183 */
20184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020185f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020186{
20187 char_u *p;
20188
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020189 p = vim_strsave(get_tv_string(&argvars[0]));
20190 rettv->v_type = VAR_STRING;
20191 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020192
20193 if (p != NULL)
20194 while (*p != NUL)
20195 {
20196#ifdef FEAT_MBYTE
20197 int l;
20198
20199 if (enc_utf8)
20200 {
20201 int c, lc;
20202
20203 c = utf_ptr2char(p);
20204 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020205 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020206 /* TODO: reallocate string when byte count changes. */
20207 if (utf_char2len(lc) == l)
20208 utf_char2bytes(lc, p);
20209 p += l;
20210 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020211 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212 p += l; /* skip multi-byte character */
20213 else
20214#endif
20215 {
20216 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20217 ++p;
20218 }
20219 }
20220}
20221
20222/*
20223 * "toupper(string)" function
20224 */
20225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020226f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020228 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020229 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020230}
20231
20232/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020233 * "tr(string, fromstr, tostr)" function
20234 */
20235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020236f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020237{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020238 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020239 char_u *fromstr;
20240 char_u *tostr;
20241 char_u *p;
20242#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020243 int inlen;
20244 int fromlen;
20245 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020246 int idx;
20247 char_u *cpstr;
20248 int cplen;
20249 int first = TRUE;
20250#endif
20251 char_u buf[NUMBUFLEN];
20252 char_u buf2[NUMBUFLEN];
20253 garray_T ga;
20254
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020255 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020256 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20257 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020258
20259 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020260 rettv->v_type = VAR_STRING;
20261 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020262 if (fromstr == NULL || tostr == NULL)
20263 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020264 ga_init2(&ga, (int)sizeof(char), 80);
20265
20266#ifdef FEAT_MBYTE
20267 if (!has_mbyte)
20268#endif
20269 /* not multi-byte: fromstr and tostr must be the same length */
20270 if (STRLEN(fromstr) != STRLEN(tostr))
20271 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020272#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020273error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020274#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020275 EMSG2(_(e_invarg2), fromstr);
20276 ga_clear(&ga);
20277 return;
20278 }
20279
20280 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020281 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020282 {
20283#ifdef FEAT_MBYTE
20284 if (has_mbyte)
20285 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020286 inlen = (*mb_ptr2len)(in_str);
20287 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020288 cplen = inlen;
20289 idx = 0;
20290 for (p = fromstr; *p != NUL; p += fromlen)
20291 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020292 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020293 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020294 {
20295 for (p = tostr; *p != NUL; p += tolen)
20296 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020297 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020298 if (idx-- == 0)
20299 {
20300 cplen = tolen;
20301 cpstr = p;
20302 break;
20303 }
20304 }
20305 if (*p == NUL) /* tostr is shorter than fromstr */
20306 goto error;
20307 break;
20308 }
20309 ++idx;
20310 }
20311
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020312 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020313 {
20314 /* Check that fromstr and tostr have the same number of
20315 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020316 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020317 first = FALSE;
20318 for (p = tostr; *p != NUL; p += tolen)
20319 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020320 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020321 --idx;
20322 }
20323 if (idx != 0)
20324 goto error;
20325 }
20326
Bram Moolenaarcde88542015-08-11 19:14:00 +020020327 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020328 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020329 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020330
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020331 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020332 }
20333 else
20334#endif
20335 {
20336 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020337 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020338 if (p != NULL)
20339 ga_append(&ga, tostr[p - fromstr]);
20340 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020341 ga_append(&ga, *in_str);
20342 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020343 }
20344 }
20345
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020346 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020347 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020348 ga_append(&ga, NUL);
20349
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020350 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020351}
20352
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020353#ifdef FEAT_FLOAT
20354/*
20355 * "trunc({float})" function
20356 */
20357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020358f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020359{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020360 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020361
20362 rettv->v_type = VAR_FLOAT;
20363 if (get_float_arg(argvars, &f) == OK)
20364 /* trunc() is not in C90, use floor() or ceil() instead. */
20365 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20366 else
20367 rettv->vval.v_float = 0.0;
20368}
20369#endif
20370
Bram Moolenaar8299df92004-07-10 09:47:34 +000020371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020372 * "type(expr)" function
20373 */
20374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020375f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020376{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020377 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020378
20379 switch (argvars[0].v_type)
20380 {
20381 case VAR_NUMBER: n = 0; break;
20382 case VAR_STRING: n = 1; break;
20383 case VAR_FUNC: n = 2; break;
20384 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020385 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020386 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020387 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020388 if (argvars[0].vval.v_number == VVAL_FALSE
20389 || argvars[0].vval.v_number == VVAL_TRUE)
20390 n = 6;
20391 else
20392 n = 7;
20393 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020394 case VAR_JOB: n = 8; break;
20395 case VAR_CHANNEL: n = 9; break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010020396 case VAR_PARTIAL: n = 10; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020397 case VAR_UNKNOWN:
20398 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20399 n = -1;
20400 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020401 }
20402 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020403}
20404
20405/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020406 * "undofile(name)" function
20407 */
20408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020409f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020410{
20411 rettv->v_type = VAR_STRING;
20412#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020413 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020414 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020415
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020416 if (*fname == NUL)
20417 {
20418 /* If there is no file name there will be no undo file. */
20419 rettv->vval.v_string = NULL;
20420 }
20421 else
20422 {
20423 char_u *ffname = FullName_save(fname, FALSE);
20424
20425 if (ffname != NULL)
20426 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20427 vim_free(ffname);
20428 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020429 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020430#else
20431 rettv->vval.v_string = NULL;
20432#endif
20433}
20434
20435/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020436 * "undotree()" function
20437 */
20438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020439f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020440{
20441 if (rettv_dict_alloc(rettv) == OK)
20442 {
20443 dict_T *dict = rettv->vval.v_dict;
20444 list_T *list;
20445
Bram Moolenaar730cde92010-06-27 05:18:54 +020020446 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020447 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020448 dict_add_nr_str(dict, "save_last",
20449 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020450 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20451 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020452 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020453
20454 list = list_alloc();
20455 if (list != NULL)
20456 {
20457 u_eval_tree(curbuf->b_u_oldhead, list);
20458 dict_add_list(dict, "entries", list);
20459 }
20460 }
20461}
20462
20463/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020464 * "values(dict)" function
20465 */
20466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020467f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020468{
20469 dict_list(argvars, rettv, 1);
20470}
20471
20472/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020473 * "virtcol(string)" function
20474 */
20475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020476f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020477{
20478 colnr_T vcol = 0;
20479 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020480 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020481
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020482 fp = var2fpos(&argvars[0], FALSE, &fnum);
20483 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20484 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020485 {
20486 getvvcol(curwin, fp, NULL, NULL, &vcol);
20487 ++vcol;
20488 }
20489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020490 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020491}
20492
20493/*
20494 * "visualmode()" function
20495 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020496 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010020497f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020498{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499 char_u str[2];
20500
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020501 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020502 str[0] = curbuf->b_visual_mode_eval;
20503 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020504 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020505
20506 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020507 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020508 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020509}
20510
20511/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020512 * "wildmenumode()" function
20513 */
20514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020515f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020516{
20517#ifdef FEAT_WILDMENU
20518 if (wild_menu_showing)
20519 rettv->vval.v_number = 1;
20520#endif
20521}
20522
20523/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020524 * "winbufnr(nr)" function
20525 */
20526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020527f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020528{
20529 win_T *wp;
20530
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020531 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020532 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020533 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020534 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020535 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536}
20537
20538/*
20539 * "wincol()" function
20540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020542f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020543{
20544 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020545 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546}
20547
20548/*
20549 * "winheight(nr)" function
20550 */
20551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020552f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020553{
20554 win_T *wp;
20555
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020556 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020557 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020558 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020559 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020560 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020561}
20562
20563/*
20564 * "winline()" function
20565 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020567f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020568{
20569 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020570 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020571}
20572
20573/*
20574 * "winnr()" function
20575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020577f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020578{
20579 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020580
Bram Moolenaar071d4272004-06-13 20:20:40 +000020581#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020582 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020583#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020584 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020585}
20586
20587/*
20588 * "winrestcmd()" function
20589 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020591f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020592{
20593#ifdef FEAT_WINDOWS
20594 win_T *wp;
20595 int winnr = 1;
20596 garray_T ga;
20597 char_u buf[50];
20598
20599 ga_init2(&ga, (int)sizeof(char), 70);
20600 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20601 {
20602 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20603 ga_concat(&ga, buf);
20604# ifdef FEAT_VERTSPLIT
20605 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20606 ga_concat(&ga, buf);
20607# endif
20608 ++winnr;
20609 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020610 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020612 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020613#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020614 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020615#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020616 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020617}
20618
20619/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020620 * "winrestview()" function
20621 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020623f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020624{
20625 dict_T *dict;
20626
20627 if (argvars[0].v_type != VAR_DICT
20628 || (dict = argvars[0].vval.v_dict) == NULL)
20629 EMSG(_(e_invarg));
20630 else
20631 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020632 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20633 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20634 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20635 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020636#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020637 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20638 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020639#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020640 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20641 {
20642 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20643 curwin->w_set_curswant = FALSE;
20644 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020645
Bram Moolenaar82c25852014-05-28 16:47:16 +020020646 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20647 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020648#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020649 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20650 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020651#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020652 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20653 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20654 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20655 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020656
20657 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020658 win_new_height(curwin, curwin->w_height);
20659# ifdef FEAT_VERTSPLIT
20660 win_new_width(curwin, W_WIDTH(curwin));
20661# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020662 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020663
Bram Moolenaarb851a962014-10-31 15:45:52 +010020664 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020665 curwin->w_topline = 1;
20666 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20667 curwin->w_topline = curbuf->b_ml.ml_line_count;
20668#ifdef FEAT_DIFF
20669 check_topfill(curwin, TRUE);
20670#endif
20671 }
20672}
20673
20674/*
20675 * "winsaveview()" function
20676 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020678f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020679{
20680 dict_T *dict;
20681
Bram Moolenaara800b422010-06-27 01:15:55 +020020682 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020683 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020684 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020685
20686 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20687 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20688#ifdef FEAT_VIRTUALEDIT
20689 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20690#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020691 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020692 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20693
20694 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20695#ifdef FEAT_DIFF
20696 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20697#endif
20698 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20699 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20700}
20701
20702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020703 * "winwidth(nr)" function
20704 */
20705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020706f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020707{
20708 win_T *wp;
20709
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020710 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020711 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020712 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020713 else
20714#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020715 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020716#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020717 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020718#endif
20719}
20720
Bram Moolenaar071d4272004-06-13 20:20:40 +000020721/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020722 * "wordcount()" function
20723 */
20724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020725f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020726{
20727 if (rettv_dict_alloc(rettv) == FAIL)
20728 return;
20729 cursor_pos_info(rettv->vval.v_dict);
20730}
20731
20732/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020733 * Write list of strings to file
20734 */
20735 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020736write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020737{
20738 listitem_T *li;
20739 int c;
20740 int ret = OK;
20741 char_u *s;
20742
20743 for (li = list->lv_first; li != NULL; li = li->li_next)
20744 {
20745 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20746 {
20747 if (*s == '\n')
20748 c = putc(NUL, fd);
20749 else
20750 c = putc(*s, fd);
20751 if (c == EOF)
20752 {
20753 ret = FAIL;
20754 break;
20755 }
20756 }
20757 if (!binary || li->li_next != NULL)
20758 if (putc('\n', fd) == EOF)
20759 {
20760 ret = FAIL;
20761 break;
20762 }
20763 if (ret == FAIL)
20764 {
20765 EMSG(_(e_write));
20766 break;
20767 }
20768 }
20769 return ret;
20770}
20771
20772/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020773 * "writefile()" function
20774 */
20775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020776f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020777{
20778 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020779 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020780 char_u *fname;
20781 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020782 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020783
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020784 if (check_restricted() || check_secure())
20785 return;
20786
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020787 if (argvars[0].v_type != VAR_LIST)
20788 {
20789 EMSG2(_(e_listarg), "writefile()");
20790 return;
20791 }
20792 if (argvars[0].vval.v_list == NULL)
20793 return;
20794
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020795 if (argvars[2].v_type != VAR_UNKNOWN)
20796 {
20797 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20798 binary = TRUE;
20799 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20800 append = TRUE;
20801 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020802
20803 /* Always open the file in binary mode, library functions have a mind of
20804 * their own about CR-LF conversion. */
20805 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020806 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20807 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020808 {
20809 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20810 ret = -1;
20811 }
20812 else
20813 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020814 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20815 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020816 fclose(fd);
20817 }
20818
20819 rettv->vval.v_number = ret;
20820}
20821
20822/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020823 * "xor(expr, expr)" function
20824 */
20825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020826f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020827{
20828 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20829 ^ get_tv_number_chk(&argvars[1], NULL);
20830}
20831
20832
20833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020835 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020836 */
20837 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020838var2fpos(
20839 typval_T *varp,
20840 int dollar_lnum, /* TRUE when $ is last line */
20841 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020843 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020844 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020845 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020846
Bram Moolenaara5525202006-03-02 22:52:09 +000020847 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020848 if (varp->v_type == VAR_LIST)
20849 {
20850 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020851 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020852 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020853 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020854
20855 l = varp->vval.v_list;
20856 if (l == NULL)
20857 return NULL;
20858
20859 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020860 pos.lnum = list_find_nr(l, 0L, &error);
20861 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020862 return NULL; /* invalid line number */
20863
20864 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020865 pos.col = list_find_nr(l, 1L, &error);
20866 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020867 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020868 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020869
20870 /* We accept "$" for the column number: last column. */
20871 li = list_find(l, 1L);
20872 if (li != NULL && li->li_tv.v_type == VAR_STRING
20873 && li->li_tv.vval.v_string != NULL
20874 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20875 pos.col = len + 1;
20876
Bram Moolenaara5525202006-03-02 22:52:09 +000020877 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020878 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020879 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020880 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020881
Bram Moolenaara5525202006-03-02 22:52:09 +000020882#ifdef FEAT_VIRTUALEDIT
20883 /* Get the virtual offset. Defaults to zero. */
20884 pos.coladd = list_find_nr(l, 2L, &error);
20885 if (error)
20886 pos.coladd = 0;
20887#endif
20888
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020889 return &pos;
20890 }
20891
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020892 name = get_tv_string_chk(varp);
20893 if (name == NULL)
20894 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020895 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020896 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020897 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20898 {
20899 if (VIsual_active)
20900 return &VIsual;
20901 return &curwin->w_cursor;
20902 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020903 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020904 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020905 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020906 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20907 return NULL;
20908 return pp;
20909 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020910
20911#ifdef FEAT_VIRTUALEDIT
20912 pos.coladd = 0;
20913#endif
20914
Bram Moolenaar477933c2007-07-17 14:32:23 +000020915 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020916 {
20917 pos.col = 0;
20918 if (name[1] == '0') /* "w0": first visible line */
20919 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020920 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020921 pos.lnum = curwin->w_topline;
20922 return &pos;
20923 }
20924 else if (name[1] == '$') /* "w$": last visible line */
20925 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020926 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020927 pos.lnum = curwin->w_botline - 1;
20928 return &pos;
20929 }
20930 }
20931 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020932 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020933 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020934 {
20935 pos.lnum = curbuf->b_ml.ml_line_count;
20936 pos.col = 0;
20937 }
20938 else
20939 {
20940 pos.lnum = curwin->w_cursor.lnum;
20941 pos.col = (colnr_T)STRLEN(ml_get_curline());
20942 }
20943 return &pos;
20944 }
20945 return NULL;
20946}
20947
20948/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020949 * Convert list in "arg" into a position and optional file number.
20950 * When "fnump" is NULL there is no file number, only 3 items.
20951 * Note that the column is passed on as-is, the caller may want to decrement
20952 * it to use 1 for the first column.
20953 * Return FAIL when conversion is not possible, doesn't check the position for
20954 * validity.
20955 */
20956 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020957list2fpos(
20958 typval_T *arg,
20959 pos_T *posp,
20960 int *fnump,
20961 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020962{
20963 list_T *l = arg->vval.v_list;
20964 long i = 0;
20965 long n;
20966
Bram Moolenaar493c1782014-05-28 14:34:46 +020020967 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20968 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020969 if (arg->v_type != VAR_LIST
20970 || l == NULL
20971 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020972 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020973 return FAIL;
20974
20975 if (fnump != NULL)
20976 {
20977 n = list_find_nr(l, i++, NULL); /* fnum */
20978 if (n < 0)
20979 return FAIL;
20980 if (n == 0)
20981 n = curbuf->b_fnum; /* current buffer */
20982 *fnump = n;
20983 }
20984
20985 n = list_find_nr(l, i++, NULL); /* lnum */
20986 if (n < 0)
20987 return FAIL;
20988 posp->lnum = n;
20989
20990 n = list_find_nr(l, i++, NULL); /* col */
20991 if (n < 0)
20992 return FAIL;
20993 posp->col = n;
20994
20995#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020996 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020997 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020998 posp->coladd = 0;
20999 else
21000 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021001#endif
21002
Bram Moolenaar493c1782014-05-28 14:34:46 +020021003 if (curswantp != NULL)
21004 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21005
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021006 return OK;
21007}
21008
21009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010 * Get the length of an environment variable name.
21011 * Advance "arg" to the first character after the name.
21012 * Return 0 for error.
21013 */
21014 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021015get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021016{
21017 char_u *p;
21018 int len;
21019
21020 for (p = *arg; vim_isIDc(*p); ++p)
21021 ;
21022 if (p == *arg) /* no name found */
21023 return 0;
21024
21025 len = (int)(p - *arg);
21026 *arg = p;
21027 return len;
21028}
21029
21030/*
21031 * Get the length of the name of a function or internal variable.
21032 * "arg" is advanced to the first non-white character after the name.
21033 * Return 0 if something is wrong.
21034 */
21035 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021036get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021037{
21038 char_u *p;
21039 int len;
21040
21041 /* Find the end of the name. */
21042 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021043 {
21044 if (*p == ':')
21045 {
21046 /* "s:" is start of "s:var", but "n:" is not and can be used in
21047 * slice "[n:]". Also "xx:" is not a namespace. */
21048 len = (int)(p - *arg);
21049 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21050 || len > 1)
21051 break;
21052 }
21053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021054 if (p == *arg) /* no name found */
21055 return 0;
21056
21057 len = (int)(p - *arg);
21058 *arg = skipwhite(p);
21059
21060 return len;
21061}
21062
21063/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021064 * Get the length of the name of a variable or function.
21065 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021066 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021067 * Return -1 if curly braces expansion failed.
21068 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021069 * If the name contains 'magic' {}'s, expand them and return the
21070 * expanded name in an allocated string via 'alias' - caller must free.
21071 */
21072 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021073get_name_len(
21074 char_u **arg,
21075 char_u **alias,
21076 int evaluate,
21077 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021078{
21079 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021080 char_u *p;
21081 char_u *expr_start;
21082 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021083
21084 *alias = NULL; /* default to no alias */
21085
21086 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21087 && (*arg)[2] == (int)KE_SNR)
21088 {
21089 /* hard coded <SNR>, already translated */
21090 *arg += 3;
21091 return get_id_len(arg) + 3;
21092 }
21093 len = eval_fname_script(*arg);
21094 if (len > 0)
21095 {
21096 /* literal "<SID>", "s:" or "<SNR>" */
21097 *arg += len;
21098 }
21099
Bram Moolenaar071d4272004-06-13 20:20:40 +000021100 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021101 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021103 p = find_name_end(*arg, &expr_start, &expr_end,
21104 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021105 if (expr_start != NULL)
21106 {
21107 char_u *temp_string;
21108
21109 if (!evaluate)
21110 {
21111 len += (int)(p - *arg);
21112 *arg = skipwhite(p);
21113 return len;
21114 }
21115
21116 /*
21117 * Include any <SID> etc in the expanded string:
21118 * Thus the -len here.
21119 */
21120 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21121 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021122 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021123 *alias = temp_string;
21124 *arg = skipwhite(p);
21125 return (int)STRLEN(temp_string);
21126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021127
21128 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021129 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021130 EMSG2(_(e_invexpr2), *arg);
21131
21132 return len;
21133}
21134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021135/*
21136 * Find the end of a variable or function name, taking care of magic braces.
21137 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21138 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021139 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021140 * Return a pointer to just after the name. Equal to "arg" if there is no
21141 * valid name.
21142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021144find_name_end(
21145 char_u *arg,
21146 char_u **expr_start,
21147 char_u **expr_end,
21148 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021150 int mb_nest = 0;
21151 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021152 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021153 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021154
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021155 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021156 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021157 *expr_start = NULL;
21158 *expr_end = NULL;
21159 }
21160
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021161 /* Quick check for valid starting character. */
21162 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21163 return arg;
21164
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021165 for (p = arg; *p != NUL
21166 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021167 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021168 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021169 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021170 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021171 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021172 if (*p == '\'')
21173 {
21174 /* skip over 'string' to avoid counting [ and ] inside it. */
21175 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21176 ;
21177 if (*p == NUL)
21178 break;
21179 }
21180 else if (*p == '"')
21181 {
21182 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21183 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21184 if (*p == '\\' && p[1] != NUL)
21185 ++p;
21186 if (*p == NUL)
21187 break;
21188 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021189 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21190 {
21191 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021192 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021193 len = (int)(p - arg);
21194 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021195 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021196 break;
21197 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021198
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021199 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021200 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021201 if (*p == '[')
21202 ++br_nest;
21203 else if (*p == ']')
21204 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021205 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021206
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021207 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021208 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021209 if (*p == '{')
21210 {
21211 mb_nest++;
21212 if (expr_start != NULL && *expr_start == NULL)
21213 *expr_start = p;
21214 }
21215 else if (*p == '}')
21216 {
21217 mb_nest--;
21218 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21219 *expr_end = p;
21220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021222 }
21223
21224 return p;
21225}
21226
21227/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021228 * Expands out the 'magic' {}'s in a variable/function name.
21229 * Note that this can call itself recursively, to deal with
21230 * constructs like foo{bar}{baz}{bam}
21231 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21232 * "in_start" ^
21233 * "expr_start" ^
21234 * "expr_end" ^
21235 * "in_end" ^
21236 *
21237 * Returns a new allocated string, which the caller must free.
21238 * Returns NULL for failure.
21239 */
21240 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021241make_expanded_name(
21242 char_u *in_start,
21243 char_u *expr_start,
21244 char_u *expr_end,
21245 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021246{
21247 char_u c1;
21248 char_u *retval = NULL;
21249 char_u *temp_result;
21250 char_u *nextcmd = NULL;
21251
21252 if (expr_end == NULL || in_end == NULL)
21253 return NULL;
21254 *expr_start = NUL;
21255 *expr_end = NUL;
21256 c1 = *in_end;
21257 *in_end = NUL;
21258
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021259 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021260 if (temp_result != NULL && nextcmd == NULL)
21261 {
21262 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21263 + (in_end - expr_end) + 1));
21264 if (retval != NULL)
21265 {
21266 STRCPY(retval, in_start);
21267 STRCAT(retval, temp_result);
21268 STRCAT(retval, expr_end + 1);
21269 }
21270 }
21271 vim_free(temp_result);
21272
21273 *in_end = c1; /* put char back for error messages */
21274 *expr_start = '{';
21275 *expr_end = '}';
21276
21277 if (retval != NULL)
21278 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021279 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021280 if (expr_start != NULL)
21281 {
21282 /* Further expansion! */
21283 temp_result = make_expanded_name(retval, expr_start,
21284 expr_end, temp_result);
21285 vim_free(retval);
21286 retval = temp_result;
21287 }
21288 }
21289
21290 return retval;
21291}
21292
21293/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021294 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021295 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021296 */
21297 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021298eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021299{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021300 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21301}
21302
21303/*
21304 * Return TRUE if character "c" can be used as the first character in a
21305 * variable or function name (excluding '{' and '}').
21306 */
21307 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021308eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021309{
21310 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021311}
21312
21313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314 * Set number v: variable to "val".
21315 */
21316 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021317set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021318{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021319 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021320}
21321
21322/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021323 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021324 */
21325 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021326get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021328 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021329}
21330
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021331/*
21332 * Get string v: variable value. Uses a static buffer, can only be used once.
21333 */
21334 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021335get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021336{
21337 return get_tv_string(&vimvars[idx].vv_tv);
21338}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021339
Bram Moolenaar071d4272004-06-13 20:20:40 +000021340/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021341 * Get List v: variable value. Caller must take care of reference count when
21342 * needed.
21343 */
21344 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021345get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021346{
21347 return vimvars[idx].vv_list;
21348}
21349
21350/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021351 * Set v:char to character "c".
21352 */
21353 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021354set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021355{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021356 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021357
21358#ifdef FEAT_MBYTE
21359 if (has_mbyte)
21360 buf[(*mb_char2bytes)(c, buf)] = NUL;
21361 else
21362#endif
21363 {
21364 buf[0] = c;
21365 buf[1] = NUL;
21366 }
21367 set_vim_var_string(VV_CHAR, buf, -1);
21368}
21369
21370/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021371 * Set v:count to "count" and v:count1 to "count1".
21372 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373 */
21374 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021375set_vcount(
21376 long count,
21377 long count1,
21378 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021380 if (set_prevcount)
21381 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021382 vimvars[VV_COUNT].vv_nr = count;
21383 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021384}
21385
21386/*
21387 * Set string v: variable to a copy of "val".
21388 */
21389 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021390set_vim_var_string(
21391 int idx,
21392 char_u *val,
21393 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021394{
Bram Moolenaara542c682016-01-31 16:28:04 +010021395 clear_tv(&vimvars[idx].vv_di.di_tv);
21396 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021397 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021398 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021399 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021400 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021401 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021402 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403}
21404
21405/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021406 * Set List v: variable to "val".
21407 */
21408 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021409set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021410{
Bram Moolenaara542c682016-01-31 16:28:04 +010021411 clear_tv(&vimvars[idx].vv_di.di_tv);
21412 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021413 vimvars[idx].vv_list = val;
21414 if (val != NULL)
21415 ++val->lv_refcount;
21416}
21417
21418/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021419 * Set Dictionary v: variable to "val".
21420 */
21421 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021422set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021423{
21424 int todo;
21425 hashitem_T *hi;
21426
Bram Moolenaara542c682016-01-31 16:28:04 +010021427 clear_tv(&vimvars[idx].vv_di.di_tv);
21428 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021429 vimvars[idx].vv_dict = val;
21430 if (val != NULL)
21431 {
21432 ++val->dv_refcount;
21433
21434 /* Set readonly */
21435 todo = (int)val->dv_hashtab.ht_used;
21436 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21437 {
21438 if (HASHITEM_EMPTY(hi))
21439 continue;
21440 --todo;
21441 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21442 }
21443 }
21444}
21445
21446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021447 * Set v:register if needed.
21448 */
21449 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021450set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021451{
21452 char_u regname;
21453
21454 if (c == 0 || c == ' ')
21455 regname = '"';
21456 else
21457 regname = c;
21458 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021459 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021460 set_vim_var_string(VV_REG, &regname, 1);
21461}
21462
21463/*
21464 * Get or set v:exception. If "oldval" == NULL, return the current value.
21465 * Otherwise, restore the value to "oldval" and return NULL.
21466 * Must always be called in pairs to save and restore v:exception! Does not
21467 * take care of memory allocations.
21468 */
21469 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021470v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021471{
21472 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021473 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474
Bram Moolenaare9a41262005-01-15 22:18:47 +000021475 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476 return NULL;
21477}
21478
21479/*
21480 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21481 * Otherwise, restore the value to "oldval" and return NULL.
21482 * Must always be called in pairs to save and restore v:throwpoint! Does not
21483 * take care of memory allocations.
21484 */
21485 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021486v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021487{
21488 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021489 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021490
Bram Moolenaare9a41262005-01-15 22:18:47 +000021491 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492 return NULL;
21493}
21494
21495#if defined(FEAT_AUTOCMD) || defined(PROTO)
21496/*
21497 * Set v:cmdarg.
21498 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21499 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21500 * Must always be called in pairs!
21501 */
21502 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021503set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021504{
21505 char_u *oldval;
21506 char_u *newval;
21507 unsigned len;
21508
Bram Moolenaare9a41262005-01-15 22:18:47 +000021509 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021510 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021511 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021512 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021513 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021514 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021515 }
21516
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021517 if (eap->force_bin == FORCE_BIN)
21518 len = 6;
21519 else if (eap->force_bin == FORCE_NOBIN)
21520 len = 8;
21521 else
21522 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021523
21524 if (eap->read_edit)
21525 len += 7;
21526
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021527 if (eap->force_ff != 0)
21528 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21529# ifdef FEAT_MBYTE
21530 if (eap->force_enc != 0)
21531 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021532 if (eap->bad_char != 0)
21533 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021534# endif
21535
21536 newval = alloc(len + 1);
21537 if (newval == NULL)
21538 return NULL;
21539
21540 if (eap->force_bin == FORCE_BIN)
21541 sprintf((char *)newval, " ++bin");
21542 else if (eap->force_bin == FORCE_NOBIN)
21543 sprintf((char *)newval, " ++nobin");
21544 else
21545 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021546
21547 if (eap->read_edit)
21548 STRCAT(newval, " ++edit");
21549
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021550 if (eap->force_ff != 0)
21551 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21552 eap->cmd + eap->force_ff);
21553# ifdef FEAT_MBYTE
21554 if (eap->force_enc != 0)
21555 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21556 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021557 if (eap->bad_char == BAD_KEEP)
21558 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21559 else if (eap->bad_char == BAD_DROP)
21560 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21561 else if (eap->bad_char != 0)
21562 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021563# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021564 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021565 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566}
21567#endif
21568
21569/*
21570 * Get the value of internal variable "name".
21571 * Return OK or FAIL.
21572 */
21573 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021574get_var_tv(
21575 char_u *name,
21576 int len, /* length of "name" */
21577 typval_T *rettv, /* NULL when only checking existence */
21578 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21579 int verbose, /* may give error message */
21580 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021581{
21582 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021583 typval_T *tv = NULL;
21584 typval_T atv;
21585 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021586 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021587
21588 /* truncate the name, so that we can use strcmp() */
21589 cc = name[len];
21590 name[len] = NUL;
21591
21592 /*
21593 * Check for "b:changedtick".
21594 */
21595 if (STRCMP(name, "b:changedtick") == 0)
21596 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021597 atv.v_type = VAR_NUMBER;
21598 atv.vval.v_number = curbuf->b_changedtick;
21599 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021600 }
21601
21602 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021603 * Check for user-defined variables.
21604 */
21605 else
21606 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021607 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021609 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021610 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021611 if (dip != NULL)
21612 *dip = v;
21613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614 }
21615
Bram Moolenaare9a41262005-01-15 22:18:47 +000021616 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021617 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021618 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021619 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620 ret = FAIL;
21621 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021622 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021623 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021624
21625 name[len] = cc;
21626
21627 return ret;
21628}
21629
21630/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021631 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21632 * Also handle function call with Funcref variable: func(expr)
21633 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21634 */
21635 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021636handle_subscript(
21637 char_u **arg,
21638 typval_T *rettv,
21639 int evaluate, /* do more than finding the end */
21640 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021641{
21642 int ret = OK;
21643 dict_T *selfdict = NULL;
21644 char_u *s;
21645 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021646 typval_T functv;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021647 partial_T *pt = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021648
21649 while (ret == OK
21650 && (**arg == '['
21651 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021652 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
21653 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021654 && !vim_iswhite(*(*arg - 1)))
21655 {
21656 if (**arg == '(')
21657 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021658 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021659 if (evaluate)
21660 {
21661 functv = *rettv;
21662 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021663
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021664 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021665 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021666 {
21667 pt = functv.vval.v_partial;
21668 s = pt->pt_name;
21669 }
21670 else
21671 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021672 }
21673 else
21674 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021675 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021676 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021677 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000021678
21679 /* Clear the funcref afterwards, so that deleting it while
21680 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021681 if (evaluate)
21682 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021683
21684 /* Stop the expression evaluation when immediately aborting on
21685 * error, or when an interrupt occurred or an exception was thrown
21686 * but not caught. */
21687 if (aborting())
21688 {
21689 if (ret == OK)
21690 clear_tv(rettv);
21691 ret = FAIL;
21692 }
21693 dict_unref(selfdict);
21694 selfdict = NULL;
21695 }
21696 else /* **arg == '[' || **arg == '.' */
21697 {
21698 dict_unref(selfdict);
21699 if (rettv->v_type == VAR_DICT)
21700 {
21701 selfdict = rettv->vval.v_dict;
21702 if (selfdict != NULL)
21703 ++selfdict->dv_refcount;
21704 }
21705 else
21706 selfdict = NULL;
21707 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21708 {
21709 clear_tv(rettv);
21710 ret = FAIL;
21711 }
21712 }
21713 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021714
21715 if (rettv->v_type == VAR_FUNC && selfdict != NULL)
21716 {
Bram Moolenaar65639032016-03-16 21:40:30 +010021717 ufunc_T *fp = find_func(rettv->vval.v_string);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021718
21719 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010021720 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021721 {
Bram Moolenaar65639032016-03-16 21:40:30 +010021722 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
21723
21724 if (pt != NULL)
21725 {
21726 pt->pt_refcount = 1;
21727 pt->pt_dict = selfdict;
21728 selfdict = NULL;
21729 pt->pt_name = rettv->vval.v_string;
21730 func_ref(pt->pt_name);
21731 rettv->v_type = VAR_PARTIAL;
21732 rettv->vval.v_partial = pt;
21733 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021734 }
21735 }
21736
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021737 dict_unref(selfdict);
21738 return ret;
21739}
21740
21741/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021742 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021743 * value).
21744 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021745 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021746alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021747{
Bram Moolenaar33570922005-01-25 22:26:29 +000021748 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021749}
21750
21751/*
21752 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021753 * The string "s" must have been allocated, it is consumed.
21754 * Return NULL for out of memory, the variable otherwise.
21755 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021756 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021757alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021758{
Bram Moolenaar33570922005-01-25 22:26:29 +000021759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021761 rettv = alloc_tv();
21762 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021763 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021764 rettv->v_type = VAR_STRING;
21765 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021766 }
21767 else
21768 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021769 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021770}
21771
21772/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021773 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021774 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021775 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021776free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021777{
21778 if (varp != NULL)
21779 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021780 switch (varp->v_type)
21781 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021782 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021783 func_unref(varp->vval.v_string);
21784 /*FALLTHROUGH*/
21785 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021786 vim_free(varp->vval.v_string);
21787 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021788 case VAR_PARTIAL:
21789 partial_unref(varp->vval.v_partial);
21790 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021791 case VAR_LIST:
21792 list_unref(varp->vval.v_list);
21793 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021794 case VAR_DICT:
21795 dict_unref(varp->vval.v_dict);
21796 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021797 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021798#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021799 job_unref(varp->vval.v_job);
21800 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021801#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021802 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021803#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021804 channel_unref(varp->vval.v_channel);
21805 break;
21806#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021807 case VAR_NUMBER:
21808 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021809 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021810 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021811 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021813 vim_free(varp);
21814 }
21815}
21816
21817/*
21818 * Free the memory for a variable value and set the value to NULL or 0.
21819 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021820 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021821clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021822{
21823 if (varp != NULL)
21824 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021825 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021826 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021827 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021828 func_unref(varp->vval.v_string);
21829 /*FALLTHROUGH*/
21830 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021831 vim_free(varp->vval.v_string);
21832 varp->vval.v_string = NULL;
21833 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021834 case VAR_PARTIAL:
21835 partial_unref(varp->vval.v_partial);
21836 varp->vval.v_partial = NULL;
21837 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021838 case VAR_LIST:
21839 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021840 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021841 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021842 case VAR_DICT:
21843 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021844 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021845 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021846 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021847 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021848 varp->vval.v_number = 0;
21849 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021850 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021851#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021852 varp->vval.v_float = 0.0;
21853 break;
21854#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021855 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021856#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021857 job_unref(varp->vval.v_job);
21858 varp->vval.v_job = NULL;
21859#endif
21860 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021861 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021862#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021863 channel_unref(varp->vval.v_channel);
21864 varp->vval.v_channel = NULL;
21865#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021866 case VAR_UNKNOWN:
21867 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021868 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021869 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021870 }
21871}
21872
21873/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021874 * Set the value of a variable to NULL without freeing items.
21875 */
21876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021877init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021878{
21879 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021880 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021881}
21882
21883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884 * Get the number value of a variable.
21885 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021886 * For incompatible types, return 0.
21887 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21888 * caller of incompatible types: it sets *denote to TRUE if "denote"
21889 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021890 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010021891 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021892get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021894 int error = FALSE;
21895
21896 return get_tv_number_chk(varp, &error); /* return 0L on error */
21897}
21898
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021899 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021900get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021901{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021902 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021903
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021904 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021905 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021906 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021907 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021908 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021909#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021910 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021911 break;
21912#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021913 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021914 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021915 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021916 break;
21917 case VAR_STRING:
21918 if (varp->vval.v_string != NULL)
21919 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021920 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021921 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021922 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021923 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021924 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021925 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021926 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021927 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021928 case VAR_SPECIAL:
21929 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21930 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021931 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021932#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021933 EMSG(_("E910: Using a Job as a Number"));
21934 break;
21935#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021936 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021937#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021938 EMSG(_("E913: Using a Channel as a Number"));
21939 break;
21940#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021941 case VAR_UNKNOWN:
21942 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021943 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021944 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021945 if (denote == NULL) /* useful for values that must be unsigned */
21946 n = -1;
21947 else
21948 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021949 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021950}
21951
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021952#ifdef FEAT_FLOAT
21953 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021954get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021955{
21956 switch (varp->v_type)
21957 {
21958 case VAR_NUMBER:
21959 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021960 case VAR_FLOAT:
21961 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021962 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021963 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021964 EMSG(_("E891: Using a Funcref as a Float"));
21965 break;
21966 case VAR_STRING:
21967 EMSG(_("E892: Using a String as a Float"));
21968 break;
21969 case VAR_LIST:
21970 EMSG(_("E893: Using a List as a Float"));
21971 break;
21972 case VAR_DICT:
21973 EMSG(_("E894: Using a Dictionary as a Float"));
21974 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021975 case VAR_SPECIAL:
21976 EMSG(_("E907: Using a special value as a Float"));
21977 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021978 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021979# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021980 EMSG(_("E911: Using a Job as a Float"));
21981 break;
21982# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021983 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021984# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021985 EMSG(_("E914: Using a Channel as a Float"));
21986 break;
21987# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021988 case VAR_UNKNOWN:
21989 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021990 break;
21991 }
21992 return 0;
21993}
21994#endif
21995
Bram Moolenaar071d4272004-06-13 20:20:40 +000021996/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021997 * Get the lnum from the first argument.
21998 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021999 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000 */
22001 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022002get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003{
Bram Moolenaar33570922005-01-25 22:26:29 +000022004 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022005 linenr_T lnum;
22006
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022007 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022008 if (lnum == 0) /* no valid number, try using line() */
22009 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022010 rettv.v_type = VAR_NUMBER;
22011 f_line(argvars, &rettv);
22012 lnum = rettv.vval.v_number;
22013 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014 }
22015 return lnum;
22016}
22017
22018/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022019 * Get the lnum from the first argument.
22020 * Also accepts "$", then "buf" is used.
22021 * Returns 0 on error.
22022 */
22023 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022024get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022025{
22026 if (argvars[0].v_type == VAR_STRING
22027 && argvars[0].vval.v_string != NULL
22028 && argvars[0].vval.v_string[0] == '$'
22029 && buf != NULL)
22030 return buf->b_ml.ml_line_count;
22031 return get_tv_number_chk(&argvars[0], NULL);
22032}
22033
22034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022035 * Get the string value of a variable.
22036 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022037 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22038 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022039 * If the String variable has never been set, return an empty string.
22040 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022041 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22042 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022044 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022045get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046{
22047 static char_u mybuf[NUMBUFLEN];
22048
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022049 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050}
22051
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022052 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022053get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022055 char_u *res = get_tv_string_buf_chk(varp, buf);
22056
22057 return res != NULL ? res : (char_u *)"";
22058}
22059
Bram Moolenaar7d647822014-04-05 21:28:56 +020022060/*
22061 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22062 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022063 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022064get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022065{
22066 static char_u mybuf[NUMBUFLEN];
22067
22068 return get_tv_string_buf_chk(varp, mybuf);
22069}
22070
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022071 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022072get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022073{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022074 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022075 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022076 case VAR_NUMBER:
22077 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22078 return buf;
22079 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022080 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022081 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022082 break;
22083 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022084 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022085 break;
22086 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022087 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022088 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022089 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022090#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022091 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022092 break;
22093#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022094 case VAR_STRING:
22095 if (varp->vval.v_string != NULL)
22096 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022097 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022098 case VAR_SPECIAL:
22099 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22100 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022101 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022102#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022103 {
22104 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022105 char *status;
22106
22107 if (job == NULL)
22108 return (char_u *)"no process";
22109 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022110 : job->jv_status == JOB_ENDED ? "dead"
22111 : "run";
22112# ifdef UNIX
22113 vim_snprintf((char *)buf, NUMBUFLEN,
22114 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022115# elif defined(WIN32)
22116 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022117 "process %ld %s",
22118 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022119 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022120# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022121 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022122 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22123# endif
22124 return buf;
22125 }
22126#endif
22127 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022128 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022129#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022130 {
22131 channel_T *channel = varp->vval.v_channel;
22132 char *status = channel_status(channel);
22133
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022134 if (channel == NULL)
22135 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22136 else
22137 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022138 "channel %d %s", channel->ch_id, status);
22139 return buf;
22140 }
22141#endif
22142 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022143 case VAR_UNKNOWN:
22144 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022145 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022147 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022148}
22149
22150/*
22151 * Find variable "name" in the list of variables.
22152 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022153 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022154 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022155 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022156 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022157 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022158find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022159{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022160 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022161 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022162
Bram Moolenaara7043832005-01-21 11:56:39 +000022163 ht = find_var_ht(name, &varname);
22164 if (htp != NULL)
22165 *htp = ht;
22166 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022167 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022168 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169}
22170
22171/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022172 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022173 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022174 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022175 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022176find_var_in_ht(
22177 hashtab_T *ht,
22178 int htname,
22179 char_u *varname,
22180 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022181{
Bram Moolenaar33570922005-01-25 22:26:29 +000022182 hashitem_T *hi;
22183
22184 if (*varname == NUL)
22185 {
22186 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022187 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022188 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022189 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022190 case 'g': return &globvars_var;
22191 case 'v': return &vimvars_var;
22192 case 'b': return &curbuf->b_bufvar;
22193 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022194#ifdef FEAT_WINDOWS
22195 case 't': return &curtab->tp_winvar;
22196#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022197 case 'l': return current_funccal == NULL
22198 ? NULL : &current_funccal->l_vars_var;
22199 case 'a': return current_funccal == NULL
22200 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022201 }
22202 return NULL;
22203 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022204
22205 hi = hash_find(ht, varname);
22206 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022207 {
22208 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022209 * worked find the variable again. Don't auto-load a script if it was
22210 * loaded already, otherwise it would be loaded every time when
22211 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022212 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022213 {
22214 /* Note: script_autoload() may make "hi" invalid. It must either
22215 * be obtained again or not used. */
22216 if (!script_autoload(varname, FALSE) || aborting())
22217 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022218 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022219 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022220 if (HASHITEM_EMPTY(hi))
22221 return NULL;
22222 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022223 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022224}
22225
22226/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022227 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022228 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022229 * Set "varname" to the start of name without ':'.
22230 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022231 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022232find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022233{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022234 hashitem_T *hi;
22235
Bram Moolenaar73627d02015-08-11 15:46:09 +020022236 if (name[0] == NUL)
22237 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022238 if (name[1] != ':')
22239 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022240 /* The name must not start with a colon or #. */
22241 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022242 return NULL;
22243 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022244
22245 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022246 hi = hash_find(&compat_hashtab, name);
22247 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022248 return &compat_hashtab;
22249
Bram Moolenaar071d4272004-06-13 20:20:40 +000022250 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022251 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022252 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022253 }
22254 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022255 if (*name == 'g') /* global variable */
22256 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022257 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22258 */
22259 if (vim_strchr(name + 2, ':') != NULL
22260 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022261 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022263 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022264 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022265 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022266#ifdef FEAT_WINDOWS
22267 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022268 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022269#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022270 if (*name == 'v') /* v: variable */
22271 return &vimvarht;
22272 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022273 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022274 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022275 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276 if (*name == 's' /* script variable */
22277 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22278 return &SCRIPT_VARS(current_SID);
22279 return NULL;
22280}
22281
22282/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022283 * Get function call environment based on bactrace debug level
22284 */
22285 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022286get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022287{
22288 int i;
22289 funccall_T *funccal;
22290 funccall_T *temp_funccal;
22291
22292 funccal = current_funccal;
22293 if (debug_backtrace_level > 0)
22294 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022295 for (i = 0; i < debug_backtrace_level; i++)
22296 {
22297 temp_funccal = funccal->caller;
22298 if (temp_funccal)
22299 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022300 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022301 /* backtrace level overflow. reset to max */
22302 debug_backtrace_level = i;
22303 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022304 }
22305 return funccal;
22306}
22307
22308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022309 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022310 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022311 * Returns NULL when it doesn't exist.
22312 */
22313 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022314get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022315{
Bram Moolenaar33570922005-01-25 22:26:29 +000022316 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022317
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022318 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022319 if (v == NULL)
22320 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022321 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022322}
22323
22324/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022325 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022326 * sourcing this script and when executing functions defined in the script.
22327 */
22328 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022329new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330{
Bram Moolenaara7043832005-01-21 11:56:39 +000022331 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022332 hashtab_T *ht;
22333 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022334
Bram Moolenaar071d4272004-06-13 20:20:40 +000022335 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22336 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022337 /* Re-allocating ga_data means that an ht_array pointing to
22338 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022339 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022340 for (i = 1; i <= ga_scripts.ga_len; ++i)
22341 {
22342 ht = &SCRIPT_VARS(i);
22343 if (ht->ht_mask == HT_INIT_SIZE - 1)
22344 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022345 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022346 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022347 }
22348
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349 while (ga_scripts.ga_len < id)
22350 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022351 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022352 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022353 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022354 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022355 }
22356 }
22357}
22358
22359/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022360 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22361 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022362 */
22363 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022364init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022365{
Bram Moolenaar33570922005-01-25 22:26:29 +000022366 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022367 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022368 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022369 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022370 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022371 dict_var->di_tv.vval.v_dict = dict;
22372 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022373 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022374 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22375 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022376}
22377
22378/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022379 * Unreference a dictionary initialized by init_var_dict().
22380 */
22381 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022382unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022383{
22384 /* Now the dict needs to be freed if no one else is using it, go back to
22385 * normal reference counting. */
22386 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22387 dict_unref(dict);
22388}
22389
22390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022391 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022392 * Frees all allocated variables and the value they contain.
22393 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022394 */
22395 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022396vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022397{
22398 vars_clear_ext(ht, TRUE);
22399}
22400
22401/*
22402 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22403 */
22404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022405vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406{
Bram Moolenaara7043832005-01-21 11:56:39 +000022407 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022408 hashitem_T *hi;
22409 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022410
Bram Moolenaar33570922005-01-25 22:26:29 +000022411 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022412 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022413 for (hi = ht->ht_array; todo > 0; ++hi)
22414 {
22415 if (!HASHITEM_EMPTY(hi))
22416 {
22417 --todo;
22418
Bram Moolenaar33570922005-01-25 22:26:29 +000022419 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022420 * ht_array might change then. hash_clear() takes care of it
22421 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022422 v = HI2DI(hi);
22423 if (free_val)
22424 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022425 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022426 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022427 }
22428 }
22429 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022430 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022431}
22432
Bram Moolenaara7043832005-01-21 11:56:39 +000022433/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022434 * Delete a variable from hashtab "ht" at item "hi".
22435 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022438delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022439{
Bram Moolenaar33570922005-01-25 22:26:29 +000022440 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022441
22442 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022443 clear_tv(&di->di_tv);
22444 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022445}
22446
22447/*
22448 * List the value of one internal variable.
22449 */
22450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022451list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022452{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022453 char_u *tofree;
22454 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022455 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022456
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022457 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022458 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022459 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022460 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022461}
22462
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022464list_one_var_a(
22465 char_u *prefix,
22466 char_u *name,
22467 int type,
22468 char_u *string,
22469 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022470{
Bram Moolenaar31859182007-08-14 20:41:13 +000022471 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22472 msg_start();
22473 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022474 if (name != NULL) /* "a:" vars don't have a name stored */
22475 msg_puts(name);
22476 msg_putchar(' ');
22477 msg_advance(22);
22478 if (type == VAR_NUMBER)
22479 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022480 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022481 msg_putchar('*');
22482 else if (type == VAR_LIST)
22483 {
22484 msg_putchar('[');
22485 if (*string == '[')
22486 ++string;
22487 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022488 else if (type == VAR_DICT)
22489 {
22490 msg_putchar('{');
22491 if (*string == '{')
22492 ++string;
22493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022494 else
22495 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022496
Bram Moolenaar071d4272004-06-13 20:20:40 +000022497 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022498
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022499 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022500 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022501 if (*first)
22502 {
22503 msg_clr_eos();
22504 *first = FALSE;
22505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022506}
22507
22508/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022509 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022510 * If the variable already exists, the value is updated.
22511 * Otherwise the variable is created.
22512 */
22513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022514set_var(
22515 char_u *name,
22516 typval_T *tv,
22517 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022518{
Bram Moolenaar33570922005-01-25 22:26:29 +000022519 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022520 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022521 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022523 ht = find_var_ht(name, &varname);
22524 if (ht == NULL || *varname == NUL)
22525 {
22526 EMSG2(_(e_illvar), name);
22527 return;
22528 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022529 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022530
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022531 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
22532 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022533 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022534
Bram Moolenaar33570922005-01-25 22:26:29 +000022535 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022536 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022537 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022538 if (var_check_ro(v->di_flags, name, FALSE)
22539 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022540 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022541
22542 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022543 * Handle setting internal v: variables separately where needed to
22544 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022545 */
22546 if (ht == &vimvarht)
22547 {
22548 if (v->di_tv.v_type == VAR_STRING)
22549 {
22550 vim_free(v->di_tv.vval.v_string);
22551 if (copy || tv->v_type != VAR_STRING)
22552 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22553 else
22554 {
22555 /* Take over the string to avoid an extra alloc/free. */
22556 v->di_tv.vval.v_string = tv->vval.v_string;
22557 tv->vval.v_string = NULL;
22558 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022559 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022560 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022561 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022562 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022563 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022564 if (STRCMP(varname, "searchforward") == 0)
22565 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022566#ifdef FEAT_SEARCH_EXTRA
22567 else if (STRCMP(varname, "hlsearch") == 0)
22568 {
22569 no_hlsearch = !v->di_tv.vval.v_number;
22570 redraw_all_later(SOME_VALID);
22571 }
22572#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022573 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022574 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022575 else if (v->di_tv.v_type != tv->v_type)
22576 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022577 }
22578
22579 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022580 }
22581 else /* add a new variable */
22582 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022583 /* Can't add "v:" variable. */
22584 if (ht == &vimvarht)
22585 {
22586 EMSG2(_(e_illvar), name);
22587 return;
22588 }
22589
Bram Moolenaar92124a32005-06-17 22:03:40 +000022590 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022591 if (!valid_varname(varname))
22592 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022593
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022594 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22595 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022596 if (v == NULL)
22597 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022598 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022599 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022600 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022601 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022602 return;
22603 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022604 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022605 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022606
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022607 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022608 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022609 else
22610 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022611 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022612 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022613 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022615}
22616
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022617/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022618 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022619 * Also give an error message.
22620 */
22621 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022622var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022623{
22624 if (flags & DI_FLAGS_RO)
22625 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022626 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022627 return TRUE;
22628 }
22629 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22630 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022631 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022632 return TRUE;
22633 }
22634 return FALSE;
22635}
22636
22637/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022638 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22639 * Also give an error message.
22640 */
22641 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022642var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022643{
22644 if (flags & DI_FLAGS_FIX)
22645 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022646 EMSG2(_("E795: Cannot delete variable %s"),
22647 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022648 return TRUE;
22649 }
22650 return FALSE;
22651}
22652
22653/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022654 * Check if a funcref is assigned to a valid variable name.
22655 * Return TRUE and give an error if not.
22656 */
22657 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022658var_check_func_name(
22659 char_u *name, /* points to start of variable name */
22660 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022661{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022662 /* Allow for w: b: s: and t:. */
22663 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022664 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22665 ? name[2] : name[0]))
22666 {
22667 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22668 name);
22669 return TRUE;
22670 }
22671 /* Don't allow hiding a function. When "v" is not NULL we might be
22672 * assigning another function to the same var, the type is checked
22673 * below. */
22674 if (new_var && function_exists(name))
22675 {
22676 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22677 name);
22678 return TRUE;
22679 }
22680 return FALSE;
22681}
22682
22683/*
22684 * Check if a variable name is valid.
22685 * Return FALSE and give an error if not.
22686 */
22687 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022688valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022689{
22690 char_u *p;
22691
22692 for (p = varname; *p != NUL; ++p)
22693 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22694 && *p != AUTOLOAD_CHAR)
22695 {
22696 EMSG2(_(e_illvar), varname);
22697 return FALSE;
22698 }
22699 return TRUE;
22700}
22701
22702/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022703 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022704 * Also give an error message, using "name" or _("name") when use_gettext is
22705 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022706 */
22707 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022708tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022709{
22710 if (lock & VAR_LOCKED)
22711 {
22712 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022713 name == NULL ? (char_u *)_("Unknown")
22714 : use_gettext ? (char_u *)_(name)
22715 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022716 return TRUE;
22717 }
22718 if (lock & VAR_FIXED)
22719 {
22720 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022721 name == NULL ? (char_u *)_("Unknown")
22722 : use_gettext ? (char_u *)_(name)
22723 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022724 return TRUE;
22725 }
22726 return FALSE;
22727}
22728
22729/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022730 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022731 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022732 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022733 * It is OK for "from" and "to" to point to the same item. This is used to
22734 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022735 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022736 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022737copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022738{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022739 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022740 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022741 switch (from->v_type)
22742 {
22743 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022744 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022745 to->vval.v_number = from->vval.v_number;
22746 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022747 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022748#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022749 to->vval.v_float = from->vval.v_float;
22750 break;
22751#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022752 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022753#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022754 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010022755 if (to->vval.v_job != NULL)
22756 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022757 break;
22758#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022759 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022760#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022761 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022762 if (to->vval.v_channel != NULL)
22763 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022764 break;
22765#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022766 case VAR_STRING:
22767 case VAR_FUNC:
22768 if (from->vval.v_string == NULL)
22769 to->vval.v_string = NULL;
22770 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022771 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022772 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022773 if (from->v_type == VAR_FUNC)
22774 func_ref(to->vval.v_string);
22775 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022776 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022777 case VAR_PARTIAL:
22778 if (from->vval.v_partial == NULL)
22779 to->vval.v_partial = NULL;
22780 else
22781 {
22782 to->vval.v_partial = from->vval.v_partial;
22783 ++to->vval.v_partial->pt_refcount;
22784 }
22785 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022786 case VAR_LIST:
22787 if (from->vval.v_list == NULL)
22788 to->vval.v_list = NULL;
22789 else
22790 {
22791 to->vval.v_list = from->vval.v_list;
22792 ++to->vval.v_list->lv_refcount;
22793 }
22794 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022795 case VAR_DICT:
22796 if (from->vval.v_dict == NULL)
22797 to->vval.v_dict = NULL;
22798 else
22799 {
22800 to->vval.v_dict = from->vval.v_dict;
22801 ++to->vval.v_dict->dv_refcount;
22802 }
22803 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022804 case VAR_UNKNOWN:
22805 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022806 break;
22807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022808}
22809
22810/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022811 * Make a copy of an item.
22812 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022813 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22814 * reference to an already copied list/dict can be used.
22815 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022816 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022817 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022818item_copy(
22819 typval_T *from,
22820 typval_T *to,
22821 int deep,
22822 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022823{
22824 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022825 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022826
Bram Moolenaar33570922005-01-25 22:26:29 +000022827 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022828 {
22829 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022830 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022831 }
22832 ++recurse;
22833
22834 switch (from->v_type)
22835 {
22836 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022837 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022838 case VAR_STRING:
22839 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022840 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010022841 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022842 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022843 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022844 copy_tv(from, to);
22845 break;
22846 case VAR_LIST:
22847 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022848 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022849 if (from->vval.v_list == NULL)
22850 to->vval.v_list = NULL;
22851 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22852 {
22853 /* use the copy made earlier */
22854 to->vval.v_list = from->vval.v_list->lv_copylist;
22855 ++to->vval.v_list->lv_refcount;
22856 }
22857 else
22858 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22859 if (to->vval.v_list == NULL)
22860 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022861 break;
22862 case VAR_DICT:
22863 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022864 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022865 if (from->vval.v_dict == NULL)
22866 to->vval.v_dict = NULL;
22867 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22868 {
22869 /* use the copy made earlier */
22870 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22871 ++to->vval.v_dict->dv_refcount;
22872 }
22873 else
22874 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22875 if (to->vval.v_dict == NULL)
22876 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022877 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022878 case VAR_UNKNOWN:
22879 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022880 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022881 }
22882 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022883 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022884}
22885
22886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022887 * ":echo expr1 ..." print each argument separated with a space, add a
22888 * newline at the end.
22889 * ":echon expr1 ..." print each argument plain.
22890 */
22891 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022892ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893{
22894 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022895 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022896 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897 char_u *p;
22898 int needclr = TRUE;
22899 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022900 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901
22902 if (eap->skip)
22903 ++emsg_skip;
22904 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22905 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022906 /* If eval1() causes an error message the text from the command may
22907 * still need to be cleared. E.g., "echo 22,44". */
22908 need_clr_eos = needclr;
22909
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022911 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912 {
22913 /*
22914 * Report the invalid expression unless the expression evaluation
22915 * has been cancelled due to an aborting error, an interrupt, or an
22916 * exception.
22917 */
22918 if (!aborting())
22919 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022920 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921 break;
22922 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022923 need_clr_eos = FALSE;
22924
Bram Moolenaar071d4272004-06-13 20:20:40 +000022925 if (!eap->skip)
22926 {
22927 if (atstart)
22928 {
22929 atstart = FALSE;
22930 /* Call msg_start() after eval1(), evaluating the expression
22931 * may cause a message to appear. */
22932 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022933 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022934 /* Mark the saved text as finishing the line, so that what
22935 * follows is displayed on a new line when scrolling back
22936 * at the more prompt. */
22937 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022938 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022940 }
22941 else if (eap->cmdidx == CMD_echo)
22942 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022943 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022944 if (p != NULL)
22945 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022946 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022947 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022948 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022949 if (*p != TAB && needclr)
22950 {
22951 /* remove any text still there from the command */
22952 msg_clr_eos();
22953 needclr = FALSE;
22954 }
22955 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022956 }
22957 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022958 {
22959#ifdef FEAT_MBYTE
22960 if (has_mbyte)
22961 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022962 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022963
22964 (void)msg_outtrans_len_attr(p, i, echo_attr);
22965 p += i - 1;
22966 }
22967 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022968#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022969 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022971 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022972 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022973 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022974 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022975 arg = skipwhite(arg);
22976 }
22977 eap->nextcmd = check_nextcmd(arg);
22978
22979 if (eap->skip)
22980 --emsg_skip;
22981 else
22982 {
22983 /* remove text that may still be there from the command */
22984 if (needclr)
22985 msg_clr_eos();
22986 if (eap->cmdidx == CMD_echo)
22987 msg_end();
22988 }
22989}
22990
22991/*
22992 * ":echohl {name}".
22993 */
22994 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022995ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996{
22997 int id;
22998
22999 id = syn_name2id(eap->arg);
23000 if (id == 0)
23001 echo_attr = 0;
23002 else
23003 echo_attr = syn_id2attr(id);
23004}
23005
23006/*
23007 * ":execute expr1 ..." execute the result of an expression.
23008 * ":echomsg expr1 ..." Print a message
23009 * ":echoerr expr1 ..." Print an error
23010 * Each gets spaces around each argument and a newline at the end for
23011 * echo commands
23012 */
23013 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023014ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023015{
23016 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023017 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023018 int ret = OK;
23019 char_u *p;
23020 garray_T ga;
23021 int len;
23022 int save_did_emsg;
23023
23024 ga_init2(&ga, 1, 80);
23025
23026 if (eap->skip)
23027 ++emsg_skip;
23028 while (*arg != NUL && *arg != '|' && *arg != '\n')
23029 {
23030 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023031 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023032 {
23033 /*
23034 * Report the invalid expression unless the expression evaluation
23035 * has been cancelled due to an aborting error, an interrupt, or an
23036 * exception.
23037 */
23038 if (!aborting())
23039 EMSG2(_(e_invexpr2), p);
23040 ret = FAIL;
23041 break;
23042 }
23043
23044 if (!eap->skip)
23045 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023046 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023047 len = (int)STRLEN(p);
23048 if (ga_grow(&ga, len + 2) == FAIL)
23049 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023050 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023051 ret = FAIL;
23052 break;
23053 }
23054 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023056 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023057 ga.ga_len += len;
23058 }
23059
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023060 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023061 arg = skipwhite(arg);
23062 }
23063
23064 if (ret != FAIL && ga.ga_data != NULL)
23065 {
23066 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023067 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023068 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023069 out_flush();
23070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023071 else if (eap->cmdidx == CMD_echoerr)
23072 {
23073 /* We don't want to abort following commands, restore did_emsg. */
23074 save_did_emsg = did_emsg;
23075 EMSG((char_u *)ga.ga_data);
23076 if (!force_abort)
23077 did_emsg = save_did_emsg;
23078 }
23079 else if (eap->cmdidx == CMD_execute)
23080 do_cmdline((char_u *)ga.ga_data,
23081 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23082 }
23083
23084 ga_clear(&ga);
23085
23086 if (eap->skip)
23087 --emsg_skip;
23088
23089 eap->nextcmd = check_nextcmd(arg);
23090}
23091
23092/*
23093 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23094 * "arg" points to the "&" or '+' when called, to "option" when returning.
23095 * Returns NULL when no option name found. Otherwise pointer to the char
23096 * after the option name.
23097 */
23098 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023099find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023100{
23101 char_u *p = *arg;
23102
23103 ++p;
23104 if (*p == 'g' && p[1] == ':')
23105 {
23106 *opt_flags = OPT_GLOBAL;
23107 p += 2;
23108 }
23109 else if (*p == 'l' && p[1] == ':')
23110 {
23111 *opt_flags = OPT_LOCAL;
23112 p += 2;
23113 }
23114 else
23115 *opt_flags = 0;
23116
23117 if (!ASCII_ISALPHA(*p))
23118 return NULL;
23119 *arg = p;
23120
23121 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23122 p += 4; /* termcap option */
23123 else
23124 while (ASCII_ISALPHA(*p))
23125 ++p;
23126 return p;
23127}
23128
23129/*
23130 * ":function"
23131 */
23132 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023133ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023134{
23135 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023136 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023137 int j;
23138 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023139 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023140 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023141 char_u *name = NULL;
23142 char_u *p;
23143 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023144 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145 garray_T newargs;
23146 garray_T newlines;
23147 int varargs = FALSE;
23148 int mustend = FALSE;
23149 int flags = 0;
23150 ufunc_T *fp;
23151 int indent;
23152 int nesting;
23153 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023154 dictitem_T *v;
23155 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023156 static int func_nr = 0; /* number for nameless function */
23157 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023158 hashtab_T *ht;
23159 int todo;
23160 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023161 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023162
23163 /*
23164 * ":function" without argument: list functions.
23165 */
23166 if (ends_excmd(*eap->arg))
23167 {
23168 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023169 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023170 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023171 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023172 {
23173 if (!HASHITEM_EMPTY(hi))
23174 {
23175 --todo;
23176 fp = HI2UF(hi);
23177 if (!isdigit(*fp->uf_name))
23178 list_func_head(fp, FALSE);
23179 }
23180 }
23181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023182 eap->nextcmd = check_nextcmd(eap->arg);
23183 return;
23184 }
23185
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023186 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023187 * ":function /pat": list functions matching pattern.
23188 */
23189 if (*eap->arg == '/')
23190 {
23191 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23192 if (!eap->skip)
23193 {
23194 regmatch_T regmatch;
23195
23196 c = *p;
23197 *p = NUL;
23198 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23199 *p = c;
23200 if (regmatch.regprog != NULL)
23201 {
23202 regmatch.rm_ic = p_ic;
23203
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023204 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023205 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23206 {
23207 if (!HASHITEM_EMPTY(hi))
23208 {
23209 --todo;
23210 fp = HI2UF(hi);
23211 if (!isdigit(*fp->uf_name)
23212 && vim_regexec(&regmatch, fp->uf_name, 0))
23213 list_func_head(fp, FALSE);
23214 }
23215 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023216 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023217 }
23218 }
23219 if (*p == '/')
23220 ++p;
23221 eap->nextcmd = check_nextcmd(p);
23222 return;
23223 }
23224
23225 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023226 * Get the function name. There are these situations:
23227 * func normal function name
23228 * "name" == func, "fudi.fd_dict" == NULL
23229 * dict.func new dictionary entry
23230 * "name" == NULL, "fudi.fd_dict" set,
23231 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23232 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023233 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023234 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23235 * dict.func existing dict entry that's not a Funcref
23236 * "name" == NULL, "fudi.fd_dict" set,
23237 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023238 * s:func script-local function name
23239 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023241 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023242 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023243 paren = (vim_strchr(p, '(') != NULL);
23244 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023245 {
23246 /*
23247 * Return on an invalid expression in braces, unless the expression
23248 * evaluation has been cancelled due to an aborting error, an
23249 * interrupt, or an exception.
23250 */
23251 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023252 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023253 if (!eap->skip && fudi.fd_newkey != NULL)
23254 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023255 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023256 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023258 else
23259 eap->skip = TRUE;
23260 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023261
Bram Moolenaar071d4272004-06-13 20:20:40 +000023262 /* An error in a function call during evaluation of an expression in magic
23263 * braces should not cause the function not to be defined. */
23264 saved_did_emsg = did_emsg;
23265 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023266
23267 /*
23268 * ":function func" with only function name: list function.
23269 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023270 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023271 {
23272 if (!ends_excmd(*skipwhite(p)))
23273 {
23274 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023275 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023276 }
23277 eap->nextcmd = check_nextcmd(p);
23278 if (eap->nextcmd != NULL)
23279 *p = NUL;
23280 if (!eap->skip && !got_int)
23281 {
23282 fp = find_func(name);
23283 if (fp != NULL)
23284 {
23285 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023286 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023287 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023288 if (FUNCLINE(fp, j) == NULL)
23289 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023290 msg_putchar('\n');
23291 msg_outnum((long)(j + 1));
23292 if (j < 9)
23293 msg_putchar(' ');
23294 if (j < 99)
23295 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023296 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023297 out_flush(); /* show a line at a time */
23298 ui_breakcheck();
23299 }
23300 if (!got_int)
23301 {
23302 msg_putchar('\n');
23303 msg_puts((char_u *)" endfunction");
23304 }
23305 }
23306 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023307 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023308 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023309 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023310 }
23311
23312 /*
23313 * ":function name(arg1, arg2)" Define function.
23314 */
23315 p = skipwhite(p);
23316 if (*p != '(')
23317 {
23318 if (!eap->skip)
23319 {
23320 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023321 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023322 }
23323 /* attempt to continue by skipping some text */
23324 if (vim_strchr(p, '(') != NULL)
23325 p = vim_strchr(p, '(');
23326 }
23327 p = skipwhite(p + 1);
23328
23329 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23330 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23331
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023332 if (!eap->skip)
23333 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023334 /* Check the name of the function. Unless it's a dictionary function
23335 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023336 if (name != NULL)
23337 arg = name;
23338 else
23339 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023340 if (arg != NULL && (fudi.fd_di == NULL
23341 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023342 {
23343 if (*arg == K_SPECIAL)
23344 j = 3;
23345 else
23346 j = 0;
23347 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23348 : eval_isnamec(arg[j])))
23349 ++j;
23350 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023351 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023352 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023353 /* Disallow using the g: dict. */
23354 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23355 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023356 }
23357
Bram Moolenaar071d4272004-06-13 20:20:40 +000023358 /*
23359 * Isolate the arguments: "arg1, arg2, ...)"
23360 */
23361 while (*p != ')')
23362 {
23363 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23364 {
23365 varargs = TRUE;
23366 p += 3;
23367 mustend = TRUE;
23368 }
23369 else
23370 {
23371 arg = p;
23372 while (ASCII_ISALNUM(*p) || *p == '_')
23373 ++p;
23374 if (arg == p || isdigit(*arg)
23375 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23376 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23377 {
23378 if (!eap->skip)
23379 EMSG2(_("E125: Illegal argument: %s"), arg);
23380 break;
23381 }
23382 if (ga_grow(&newargs, 1) == FAIL)
23383 goto erret;
23384 c = *p;
23385 *p = NUL;
23386 arg = vim_strsave(arg);
23387 if (arg == NULL)
23388 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023389
23390 /* Check for duplicate argument name. */
23391 for (i = 0; i < newargs.ga_len; ++i)
23392 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23393 {
23394 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023395 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023396 goto erret;
23397 }
23398
Bram Moolenaar071d4272004-06-13 20:20:40 +000023399 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23400 *p = c;
23401 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023402 if (*p == ',')
23403 ++p;
23404 else
23405 mustend = TRUE;
23406 }
23407 p = skipwhite(p);
23408 if (mustend && *p != ')')
23409 {
23410 if (!eap->skip)
23411 EMSG2(_(e_invarg2), eap->arg);
23412 break;
23413 }
23414 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023415 if (*p != ')')
23416 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023417 ++p; /* skip the ')' */
23418
Bram Moolenaare9a41262005-01-15 22:18:47 +000023419 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023420 for (;;)
23421 {
23422 p = skipwhite(p);
23423 if (STRNCMP(p, "range", 5) == 0)
23424 {
23425 flags |= FC_RANGE;
23426 p += 5;
23427 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023428 else if (STRNCMP(p, "dict", 4) == 0)
23429 {
23430 flags |= FC_DICT;
23431 p += 4;
23432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023433 else if (STRNCMP(p, "abort", 5) == 0)
23434 {
23435 flags |= FC_ABORT;
23436 p += 5;
23437 }
23438 else
23439 break;
23440 }
23441
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023442 /* When there is a line break use what follows for the function body.
23443 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23444 if (*p == '\n')
23445 line_arg = p + 1;
23446 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023447 EMSG(_(e_trailing));
23448
23449 /*
23450 * Read the body of the function, until ":endfunction" is found.
23451 */
23452 if (KeyTyped)
23453 {
23454 /* Check if the function already exists, don't let the user type the
23455 * whole function before telling him it doesn't work! For a script we
23456 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023457 if (!eap->skip && !eap->forceit)
23458 {
23459 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23460 EMSG(_(e_funcdict));
23461 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023462 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023464
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023465 if (!eap->skip && did_emsg)
23466 goto erret;
23467
Bram Moolenaar071d4272004-06-13 20:20:40 +000023468 msg_putchar('\n'); /* don't overwrite the function name */
23469 cmdline_row = msg_row;
23470 }
23471
23472 indent = 2;
23473 nesting = 0;
23474 for (;;)
23475 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023476 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023477 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023478 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023479 saved_wait_return = FALSE;
23480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023481 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023482 sourcing_lnum_off = sourcing_lnum;
23483
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023484 if (line_arg != NULL)
23485 {
23486 /* Use eap->arg, split up in parts by line breaks. */
23487 theline = line_arg;
23488 p = vim_strchr(theline, '\n');
23489 if (p == NULL)
23490 line_arg += STRLEN(line_arg);
23491 else
23492 {
23493 *p = NUL;
23494 line_arg = p + 1;
23495 }
23496 }
23497 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023498 theline = getcmdline(':', 0L, indent);
23499 else
23500 theline = eap->getline(':', eap->cookie, indent);
23501 if (KeyTyped)
23502 lines_left = Rows - 1;
23503 if (theline == NULL)
23504 {
23505 EMSG(_("E126: Missing :endfunction"));
23506 goto erret;
23507 }
23508
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023509 /* Detect line continuation: sourcing_lnum increased more than one. */
23510 if (sourcing_lnum > sourcing_lnum_off + 1)
23511 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23512 else
23513 sourcing_lnum_off = 0;
23514
Bram Moolenaar071d4272004-06-13 20:20:40 +000023515 if (skip_until != NULL)
23516 {
23517 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23518 * don't check for ":endfunc". */
23519 if (STRCMP(theline, skip_until) == 0)
23520 {
23521 vim_free(skip_until);
23522 skip_until = NULL;
23523 }
23524 }
23525 else
23526 {
23527 /* skip ':' and blanks*/
23528 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23529 ;
23530
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023531 /* Check for "endfunction". */
23532 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023533 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023534 if (line_arg == NULL)
23535 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023536 break;
23537 }
23538
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023539 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023540 * at "end". */
23541 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23542 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023543 else if (STRNCMP(p, "if", 2) == 0
23544 || STRNCMP(p, "wh", 2) == 0
23545 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023546 || STRNCMP(p, "try", 3) == 0)
23547 indent += 2;
23548
23549 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023550 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023551 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023552 if (*p == '!')
23553 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023554 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010023555 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010023556 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023557 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023558 ++nesting;
23559 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023560 }
23561 }
23562
23563 /* Check for ":append" or ":insert". */
23564 p = skip_range(p, NULL);
23565 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23566 || (p[0] == 'i'
23567 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23568 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23569 skip_until = vim_strsave((char_u *)".");
23570
23571 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23572 arg = skipwhite(skiptowhite(p));
23573 if (arg[0] == '<' && arg[1] =='<'
23574 && ((p[0] == 'p' && p[1] == 'y'
23575 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23576 || (p[0] == 'p' && p[1] == 'e'
23577 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23578 || (p[0] == 't' && p[1] == 'c'
23579 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023580 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23581 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023582 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23583 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023584 || (p[0] == 'm' && p[1] == 'z'
23585 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023586 ))
23587 {
23588 /* ":python <<" continues until a dot, like ":append" */
23589 p = skipwhite(arg + 2);
23590 if (*p == NUL)
23591 skip_until = vim_strsave((char_u *)".");
23592 else
23593 skip_until = vim_strsave(p);
23594 }
23595 }
23596
23597 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023598 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023599 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023600 if (line_arg == NULL)
23601 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023602 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023603 }
23604
23605 /* Copy the line to newly allocated memory. get_one_sourceline()
23606 * allocates 250 bytes per line, this saves 80% on average. The cost
23607 * is an extra alloc/free. */
23608 p = vim_strsave(theline);
23609 if (p != NULL)
23610 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023611 if (line_arg == NULL)
23612 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023613 theline = p;
23614 }
23615
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023616 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23617
23618 /* Add NULL lines for continuation lines, so that the line count is
23619 * equal to the index in the growarray. */
23620 while (sourcing_lnum_off-- > 0)
23621 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023622
23623 /* Check for end of eap->arg. */
23624 if (line_arg != NULL && *line_arg == NUL)
23625 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023626 }
23627
23628 /* Don't define the function when skipping commands or when an error was
23629 * detected. */
23630 if (eap->skip || did_emsg)
23631 goto erret;
23632
23633 /*
23634 * If there are no errors, add the function
23635 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023636 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023637 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023638 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023639 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023640 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023641 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023642 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023643 goto erret;
23644 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023645
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023646 fp = find_func(name);
23647 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023648 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023649 if (!eap->forceit)
23650 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023651 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023652 goto erret;
23653 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023654 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023655 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023656 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023657 name);
23658 goto erret;
23659 }
23660 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023661 ga_clear_strings(&(fp->uf_args));
23662 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023663 vim_free(name);
23664 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023666 }
23667 else
23668 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023669 char numbuf[20];
23670
23671 fp = NULL;
23672 if (fudi.fd_newkey == NULL && !eap->forceit)
23673 {
23674 EMSG(_(e_funcdict));
23675 goto erret;
23676 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023677 if (fudi.fd_di == NULL)
23678 {
23679 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023680 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023681 goto erret;
23682 }
23683 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023684 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023685 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023686
23687 /* Give the function a sequential number. Can only be used with a
23688 * Funcref! */
23689 vim_free(name);
23690 sprintf(numbuf, "%d", ++func_nr);
23691 name = vim_strsave((char_u *)numbuf);
23692 if (name == NULL)
23693 goto erret;
23694 }
23695
23696 if (fp == NULL)
23697 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023698 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023699 {
23700 int slen, plen;
23701 char_u *scriptname;
23702
23703 /* Check that the autoload name matches the script name. */
23704 j = FAIL;
23705 if (sourcing_name != NULL)
23706 {
23707 scriptname = autoload_name(name);
23708 if (scriptname != NULL)
23709 {
23710 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023711 plen = (int)STRLEN(p);
23712 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023713 if (slen > plen && fnamecmp(p,
23714 sourcing_name + slen - plen) == 0)
23715 j = OK;
23716 vim_free(scriptname);
23717 }
23718 }
23719 if (j == FAIL)
23720 {
23721 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23722 goto erret;
23723 }
23724 }
23725
23726 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023727 if (fp == NULL)
23728 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023729
23730 if (fudi.fd_dict != NULL)
23731 {
23732 if (fudi.fd_di == NULL)
23733 {
23734 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023735 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023736 if (fudi.fd_di == NULL)
23737 {
23738 vim_free(fp);
23739 goto erret;
23740 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023741 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23742 {
23743 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023744 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023745 goto erret;
23746 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023747 }
23748 else
23749 /* overwrite existing dict entry */
23750 clear_tv(&fudi.fd_di->di_tv);
23751 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023752 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023753 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023754 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023755
23756 /* behave like "dict" was used */
23757 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023758 }
23759
Bram Moolenaar071d4272004-06-13 20:20:40 +000023760 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023761 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023762 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23763 {
23764 vim_free(fp);
23765 goto erret;
23766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023767 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023768 fp->uf_args = newargs;
23769 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023770#ifdef FEAT_PROFILE
23771 fp->uf_tml_count = NULL;
23772 fp->uf_tml_total = NULL;
23773 fp->uf_tml_self = NULL;
23774 fp->uf_profiling = FALSE;
23775 if (prof_def_func())
23776 func_do_profile(fp);
23777#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023778 fp->uf_varargs = varargs;
23779 fp->uf_flags = flags;
23780 fp->uf_calls = 0;
23781 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023782 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023783
23784erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023785 ga_clear_strings(&newargs);
23786 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023787ret_free:
23788 vim_free(skip_until);
23789 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023790 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023791 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023792 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023793}
23794
23795/*
23796 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023797 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023798 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023799 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023800 * TFN_INT: internal function name OK
23801 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023802 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023803 * Advances "pp" to just after the function name (if no error).
23804 */
23805 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023806trans_function_name(
23807 char_u **pp,
23808 int skip, /* only find the end, don't evaluate */
23809 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010023810 funcdict_T *fdp, /* return: info about dictionary used */
23811 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023812{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023813 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023814 char_u *start;
23815 char_u *end;
23816 int lead;
23817 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023818 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023819 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023820
23821 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023822 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023823 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023824
23825 /* Check for hard coded <SNR>: already translated function ID (from a user
23826 * command). */
23827 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23828 && (*pp)[2] == (int)KE_SNR)
23829 {
23830 *pp += 3;
23831 len = get_id_len(pp) + 3;
23832 return vim_strnsave(start, len);
23833 }
23834
23835 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23836 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023837 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023838 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023839 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023840
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023841 /* Note that TFN_ flags use the same values as GLV_ flags. */
23842 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023843 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023844 if (end == start)
23845 {
23846 if (!skip)
23847 EMSG(_("E129: Function name required"));
23848 goto theend;
23849 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023850 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023851 {
23852 /*
23853 * Report an invalid expression in braces, unless the expression
23854 * evaluation has been cancelled due to an aborting error, an
23855 * interrupt, or an exception.
23856 */
23857 if (!aborting())
23858 {
23859 if (end != NULL)
23860 EMSG2(_(e_invarg2), start);
23861 }
23862 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023863 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023864 goto theend;
23865 }
23866
23867 if (lv.ll_tv != NULL)
23868 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023869 if (fdp != NULL)
23870 {
23871 fdp->fd_dict = lv.ll_dict;
23872 fdp->fd_newkey = lv.ll_newkey;
23873 lv.ll_newkey = NULL;
23874 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023875 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023876 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23877 {
23878 name = vim_strsave(lv.ll_tv->vval.v_string);
23879 *pp = end;
23880 }
23881 else
23882 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023883 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23884 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023885 EMSG(_(e_funcref));
23886 else
23887 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023888 name = NULL;
23889 }
23890 goto theend;
23891 }
23892
23893 if (lv.ll_name == NULL)
23894 {
23895 /* Error found, but continue after the function name. */
23896 *pp = end;
23897 goto theend;
23898 }
23899
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023900 /* Check if the name is a Funcref. If so, use the value. */
23901 if (lv.ll_exp_name != NULL)
23902 {
23903 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010023904 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023905 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023906 if (name == lv.ll_exp_name)
23907 name = NULL;
23908 }
23909 else
23910 {
23911 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010023912 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023913 if (name == *pp)
23914 name = NULL;
23915 }
23916 if (name != NULL)
23917 {
23918 name = vim_strsave(name);
23919 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023920 if (STRNCMP(name, "<SNR>", 5) == 0)
23921 {
23922 /* Change "<SNR>" to the byte sequence. */
23923 name[0] = K_SPECIAL;
23924 name[1] = KS_EXTRA;
23925 name[2] = (int)KE_SNR;
23926 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23927 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023928 goto theend;
23929 }
23930
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023931 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023932 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023933 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023934 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23935 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23936 {
23937 /* When there was "s:" already or the name expanded to get a
23938 * leading "s:" then remove it. */
23939 lv.ll_name += 2;
23940 len -= 2;
23941 lead = 2;
23942 }
23943 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023944 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023945 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023946 /* skip over "s:" and "g:" */
23947 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023948 lv.ll_name += 2;
23949 len = (int)(end - lv.ll_name);
23950 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023951
23952 /*
23953 * Copy the function name to allocated memory.
23954 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23955 * Accept <SNR>123_name() outside a script.
23956 */
23957 if (skip)
23958 lead = 0; /* do nothing */
23959 else if (lead > 0)
23960 {
23961 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023962 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23963 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023964 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023965 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023966 if (current_SID <= 0)
23967 {
23968 EMSG(_(e_usingsid));
23969 goto theend;
23970 }
23971 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23972 lead += (int)STRLEN(sid_buf);
23973 }
23974 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023975 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023976 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023977 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023978 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023979 goto theend;
23980 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023981 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023982 {
23983 char_u *cp = vim_strchr(lv.ll_name, ':');
23984
23985 if (cp != NULL && cp < end)
23986 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023987 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023988 goto theend;
23989 }
23990 }
23991
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023992 name = alloc((unsigned)(len + lead + 1));
23993 if (name != NULL)
23994 {
23995 if (lead > 0)
23996 {
23997 name[0] = K_SPECIAL;
23998 name[1] = KS_EXTRA;
23999 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024000 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024001 STRCPY(name + 3, sid_buf);
24002 }
24003 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024004 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024005 }
24006 *pp = end;
24007
24008theend:
24009 clear_lval(&lv);
24010 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024011}
24012
24013/*
24014 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24015 * Return 2 if "p" starts with "s:".
24016 * Return 0 otherwise.
24017 */
24018 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024019eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024020{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024021 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24022 * the standard library function. */
24023 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24024 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024025 return 5;
24026 if (p[0] == 's' && p[1] == ':')
24027 return 2;
24028 return 0;
24029}
24030
24031/*
24032 * Return TRUE if "p" starts with "<SID>" or "s:".
24033 * Only works if eval_fname_script() returned non-zero for "p"!
24034 */
24035 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024036eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024037{
24038 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24039}
24040
24041/*
24042 * List the head of the function: "name(arg1, arg2)".
24043 */
24044 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024045list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024046{
24047 int j;
24048
24049 msg_start();
24050 if (indent)
24051 MSG_PUTS(" ");
24052 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024053 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024054 {
24055 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024056 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024057 }
24058 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024059 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024060 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024061 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024062 {
24063 if (j)
24064 MSG_PUTS(", ");
24065 msg_puts(FUNCARG(fp, j));
24066 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024067 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024068 {
24069 if (j)
24070 MSG_PUTS(", ");
24071 MSG_PUTS("...");
24072 }
24073 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024074 if (fp->uf_flags & FC_ABORT)
24075 MSG_PUTS(" abort");
24076 if (fp->uf_flags & FC_RANGE)
24077 MSG_PUTS(" range");
24078 if (fp->uf_flags & FC_DICT)
24079 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024080 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024081 if (p_verbose > 0)
24082 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024083}
24084
24085/*
24086 * Find a function by name, return pointer to it in ufuncs.
24087 * Return NULL for unknown function.
24088 */
24089 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024090find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024091{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024092 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024093
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024094 hi = hash_find(&func_hashtab, name);
24095 if (!HASHITEM_EMPTY(hi))
24096 return HI2UF(hi);
24097 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024098}
24099
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024100#if defined(EXITFREE) || defined(PROTO)
24101 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024102free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024103{
24104 hashitem_T *hi;
24105
24106 /* Need to start all over every time, because func_free() may change the
24107 * hash table. */
24108 while (func_hashtab.ht_used > 0)
24109 for (hi = func_hashtab.ht_array; ; ++hi)
24110 if (!HASHITEM_EMPTY(hi))
24111 {
24112 func_free(HI2UF(hi));
24113 break;
24114 }
24115}
24116#endif
24117
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024118 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024119translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024120{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024121 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024122 return find_internal_func(name) >= 0;
24123 return find_func(name) != NULL;
24124}
24125
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024126/*
24127 * Return TRUE if a function "name" exists.
24128 */
24129 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024130function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024131{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024132 char_u *nm = name;
24133 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024134 int n = FALSE;
24135
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024136 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024137 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024138 nm = skipwhite(nm);
24139
24140 /* Only accept "funcname", "funcname ", "funcname (..." and
24141 * "funcname(...", not "funcname!...". */
24142 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024143 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024144 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024145 return n;
24146}
24147
Bram Moolenaara1544c02013-05-30 12:35:52 +020024148 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024149get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024150{
24151 char_u *nm = name;
24152 char_u *p;
24153
Bram Moolenaar65639032016-03-16 21:40:30 +010024154 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024155
24156 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024157 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024158 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024159
Bram Moolenaara1544c02013-05-30 12:35:52 +020024160 vim_free(p);
24161 return NULL;
24162}
24163
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024164/*
24165 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024166 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24167 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024168 */
24169 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024170builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024171{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024172 char_u *p;
24173
24174 if (!ASCII_ISLOWER(name[0]))
24175 return FALSE;
24176 p = vim_strchr(name, AUTOLOAD_CHAR);
24177 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024178}
24179
Bram Moolenaar05159a02005-02-26 23:04:13 +000024180#if defined(FEAT_PROFILE) || defined(PROTO)
24181/*
24182 * Start profiling function "fp".
24183 */
24184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024185func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024186{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024187 int len = fp->uf_lines.ga_len;
24188
24189 if (len == 0)
24190 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024191 fp->uf_tm_count = 0;
24192 profile_zero(&fp->uf_tm_self);
24193 profile_zero(&fp->uf_tm_total);
24194 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024195 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024196 if (fp->uf_tml_total == NULL)
24197 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024198 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024199 if (fp->uf_tml_self == NULL)
24200 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024201 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024202 fp->uf_tml_idx = -1;
24203 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24204 || fp->uf_tml_self == NULL)
24205 return; /* out of memory */
24206
24207 fp->uf_profiling = TRUE;
24208}
24209
24210/*
24211 * Dump the profiling results for all functions in file "fd".
24212 */
24213 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024214func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024215{
24216 hashitem_T *hi;
24217 int todo;
24218 ufunc_T *fp;
24219 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024220 ufunc_T **sorttab;
24221 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024222
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024223 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024224 if (todo == 0)
24225 return; /* nothing to dump */
24226
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024227 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024228
Bram Moolenaar05159a02005-02-26 23:04:13 +000024229 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24230 {
24231 if (!HASHITEM_EMPTY(hi))
24232 {
24233 --todo;
24234 fp = HI2UF(hi);
24235 if (fp->uf_profiling)
24236 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024237 if (sorttab != NULL)
24238 sorttab[st_len++] = fp;
24239
Bram Moolenaar05159a02005-02-26 23:04:13 +000024240 if (fp->uf_name[0] == K_SPECIAL)
24241 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24242 else
24243 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24244 if (fp->uf_tm_count == 1)
24245 fprintf(fd, "Called 1 time\n");
24246 else
24247 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24248 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24249 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24250 fprintf(fd, "\n");
24251 fprintf(fd, "count total (s) self (s)\n");
24252
24253 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24254 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024255 if (FUNCLINE(fp, i) == NULL)
24256 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024257 prof_func_line(fd, fp->uf_tml_count[i],
24258 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024259 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24260 }
24261 fprintf(fd, "\n");
24262 }
24263 }
24264 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024265
24266 if (sorttab != NULL && st_len > 0)
24267 {
24268 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24269 prof_total_cmp);
24270 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24271 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24272 prof_self_cmp);
24273 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24274 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024275
24276 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024277}
Bram Moolenaar73830342005-02-28 22:48:19 +000024278
24279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024280prof_sort_list(
24281 FILE *fd,
24282 ufunc_T **sorttab,
24283 int st_len,
24284 char *title,
24285 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024286{
24287 int i;
24288 ufunc_T *fp;
24289
24290 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24291 fprintf(fd, "count total (s) self (s) function\n");
24292 for (i = 0; i < 20 && i < st_len; ++i)
24293 {
24294 fp = sorttab[i];
24295 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24296 prefer_self);
24297 if (fp->uf_name[0] == K_SPECIAL)
24298 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24299 else
24300 fprintf(fd, " %s()\n", fp->uf_name);
24301 }
24302 fprintf(fd, "\n");
24303}
24304
24305/*
24306 * Print the count and times for one function or function line.
24307 */
24308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024309prof_func_line(
24310 FILE *fd,
24311 int count,
24312 proftime_T *total,
24313 proftime_T *self,
24314 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024315{
24316 if (count > 0)
24317 {
24318 fprintf(fd, "%5d ", count);
24319 if (prefer_self && profile_equal(total, self))
24320 fprintf(fd, " ");
24321 else
24322 fprintf(fd, "%s ", profile_msg(total));
24323 if (!prefer_self && profile_equal(total, self))
24324 fprintf(fd, " ");
24325 else
24326 fprintf(fd, "%s ", profile_msg(self));
24327 }
24328 else
24329 fprintf(fd, " ");
24330}
24331
24332/*
24333 * Compare function for total time sorting.
24334 */
24335 static int
24336#ifdef __BORLANDC__
24337_RTLENTRYF
24338#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024339prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024340{
24341 ufunc_T *p1, *p2;
24342
24343 p1 = *(ufunc_T **)s1;
24344 p2 = *(ufunc_T **)s2;
24345 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24346}
24347
24348/*
24349 * Compare function for self time sorting.
24350 */
24351 static int
24352#ifdef __BORLANDC__
24353_RTLENTRYF
24354#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024355prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024356{
24357 ufunc_T *p1, *p2;
24358
24359 p1 = *(ufunc_T **)s1;
24360 p2 = *(ufunc_T **)s2;
24361 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24362}
24363
Bram Moolenaar05159a02005-02-26 23:04:13 +000024364#endif
24365
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024366/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024367 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024368 * Return TRUE if a package was loaded.
24369 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024370 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024371script_autoload(
24372 char_u *name,
24373 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024374{
24375 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024376 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024377 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024378 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024379
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024380 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024381 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024382 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024383 return FALSE;
24384
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024385 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024386
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024387 /* Find the name in the list of previously loaded package names. Skip
24388 * "autoload/", it's always the same. */
24389 for (i = 0; i < ga_loaded.ga_len; ++i)
24390 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24391 break;
24392 if (!reload && i < ga_loaded.ga_len)
24393 ret = FALSE; /* was loaded already */
24394 else
24395 {
24396 /* Remember the name if it wasn't loaded already. */
24397 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24398 {
24399 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24400 tofree = NULL;
24401 }
24402
24403 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024404 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024405 ret = TRUE;
24406 }
24407
24408 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024409 return ret;
24410}
24411
24412/*
24413 * Return the autoload script name for a function or variable name.
24414 * Returns NULL when out of memory.
24415 */
24416 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024417autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024418{
24419 char_u *p;
24420 char_u *scriptname;
24421
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024422 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024423 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24424 if (scriptname == NULL)
24425 return FALSE;
24426 STRCPY(scriptname, "autoload/");
24427 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024428 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024429 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024430 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024431 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024432 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024433}
24434
Bram Moolenaar071d4272004-06-13 20:20:40 +000024435#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24436
24437/*
24438 * Function given to ExpandGeneric() to obtain the list of user defined
24439 * function names.
24440 */
24441 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024442get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024443{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024444 static long_u done;
24445 static hashitem_T *hi;
24446 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024447
24448 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024449 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024450 done = 0;
24451 hi = func_hashtab.ht_array;
24452 }
24453 if (done < func_hashtab.ht_used)
24454 {
24455 if (done++ > 0)
24456 ++hi;
24457 while (HASHITEM_EMPTY(hi))
24458 ++hi;
24459 fp = HI2UF(hi);
24460
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024461 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024462 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024463
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024464 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24465 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024466
24467 cat_func_name(IObuff, fp);
24468 if (xp->xp_context != EXPAND_USER_FUNC)
24469 {
24470 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024471 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024472 STRCAT(IObuff, ")");
24473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024474 return IObuff;
24475 }
24476 return NULL;
24477}
24478
24479#endif /* FEAT_CMDL_COMPL */
24480
24481/*
24482 * Copy the function name of "fp" to buffer "buf".
24483 * "buf" must be able to hold the function name plus three bytes.
24484 * Takes care of script-local function names.
24485 */
24486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024487cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024488{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024489 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024490 {
24491 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024492 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024493 }
24494 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024495 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024496}
24497
24498/*
24499 * ":delfunction {name}"
24500 */
24501 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024502ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024503{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024504 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024505 char_u *p;
24506 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024507 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024508
24509 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024510 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024511 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024512 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024513 {
24514 if (fudi.fd_dict != NULL && !eap->skip)
24515 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024516 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024518 if (!ends_excmd(*skipwhite(p)))
24519 {
24520 vim_free(name);
24521 EMSG(_(e_trailing));
24522 return;
24523 }
24524 eap->nextcmd = check_nextcmd(p);
24525 if (eap->nextcmd != NULL)
24526 *p = NUL;
24527
24528 if (!eap->skip)
24529 fp = find_func(name);
24530 vim_free(name);
24531
24532 if (!eap->skip)
24533 {
24534 if (fp == NULL)
24535 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024536 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024537 return;
24538 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024539 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024540 {
24541 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24542 return;
24543 }
24544
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024545 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024546 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024547 /* Delete the dict item that refers to the function, it will
24548 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024549 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024550 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024551 else
24552 func_free(fp);
24553 }
24554}
24555
24556/*
24557 * Free a function and remove it from the list of functions.
24558 */
24559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024560func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024561{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024562 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024563
24564 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024565 ga_clear_strings(&(fp->uf_args));
24566 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024567#ifdef FEAT_PROFILE
24568 vim_free(fp->uf_tml_count);
24569 vim_free(fp->uf_tml_total);
24570 vim_free(fp->uf_tml_self);
24571#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024572
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024573 /* remove the function from the function hashtable */
24574 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24575 if (HASHITEM_EMPTY(hi))
24576 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024577 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024578 hash_remove(&func_hashtab, hi);
24579
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024580 vim_free(fp);
24581}
24582
24583/*
24584 * Unreference a Function: decrement the reference count and free it when it
24585 * becomes zero. Only for numbered functions.
24586 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024587 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024588func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024589{
24590 ufunc_T *fp;
24591
24592 if (name != NULL && isdigit(*name))
24593 {
24594 fp = find_func(name);
24595 if (fp == NULL)
24596 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024597 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024598 {
24599 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024600 * when "uf_calls" becomes zero. */
24601 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024602 func_free(fp);
24603 }
24604 }
24605}
24606
24607/*
24608 * Count a reference to a Function.
24609 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024610 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024611func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024612{
24613 ufunc_T *fp;
24614
24615 if (name != NULL && isdigit(*name))
24616 {
24617 fp = find_func(name);
24618 if (fp == NULL)
24619 EMSG2(_(e_intern2), "func_ref()");
24620 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024621 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024622 }
24623}
24624
24625/*
24626 * Call a user function.
24627 */
24628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024629call_user_func(
24630 ufunc_T *fp, /* pointer to function */
24631 int argcount, /* nr of args */
24632 typval_T *argvars, /* arguments */
24633 typval_T *rettv, /* return value */
24634 linenr_T firstline, /* first line of range */
24635 linenr_T lastline, /* last line of range */
24636 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024637{
Bram Moolenaar33570922005-01-25 22:26:29 +000024638 char_u *save_sourcing_name;
24639 linenr_T save_sourcing_lnum;
24640 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024641 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024642 int save_did_emsg;
24643 static int depth = 0;
24644 dictitem_T *v;
24645 int fixvar_idx = 0; /* index in fixvar[] */
24646 int i;
24647 int ai;
24648 char_u numbuf[NUMBUFLEN];
24649 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024650 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024651#ifdef FEAT_PROFILE
24652 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024653 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024655
24656 /* If depth of calling is getting too high, don't execute the function */
24657 if (depth >= p_mfd)
24658 {
24659 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024660 rettv->v_type = VAR_NUMBER;
24661 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024662 return;
24663 }
24664 ++depth;
24665
24666 line_breakcheck(); /* check for CTRL-C hit */
24667
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024668 fc = (funccall_T *)alloc(sizeof(funccall_T));
24669 fc->caller = current_funccal;
24670 current_funccal = fc;
24671 fc->func = fp;
24672 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024673 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024674 fc->linenr = 0;
24675 fc->returned = FALSE;
24676 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024677 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024678 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24679 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024680
Bram Moolenaar33570922005-01-25 22:26:29 +000024681 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024682 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024683 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24684 * each argument variable and saves a lot of time.
24685 */
24686 /*
24687 * Init l: variables.
24688 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024689 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024690 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024691 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024692 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24693 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024694 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024695 name = v->di_key;
24696 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024697 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024698 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024699 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024700 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024701 v->di_tv.vval.v_dict = selfdict;
24702 ++selfdict->dv_refcount;
24703 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024704
Bram Moolenaar33570922005-01-25 22:26:29 +000024705 /*
24706 * Init a: variables.
24707 * Set a:0 to "argcount".
24708 * Set a:000 to a list with room for the "..." arguments.
24709 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024710 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024711 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024712 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024713 /* Use "name" to avoid a warning from some compiler that checks the
24714 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024715 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024716 name = v->di_key;
24717 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024718 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024719 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024720 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024721 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024722 v->di_tv.vval.v_list = &fc->l_varlist;
24723 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24724 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24725 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024726
24727 /*
24728 * Set a:firstline to "firstline" and a:lastline to "lastline".
24729 * Set a:name to named arguments.
24730 * Set a:N to the "..." arguments.
24731 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024732 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024733 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024734 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024735 (varnumber_T)lastline);
24736 for (i = 0; i < argcount; ++i)
24737 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024738 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024739 if (ai < 0)
24740 /* named argument a:name */
24741 name = FUNCARG(fp, i);
24742 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024743 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024744 /* "..." argument a:1, a:2, etc. */
24745 sprintf((char *)numbuf, "%d", ai + 1);
24746 name = numbuf;
24747 }
24748 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24749 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024750 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024751 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24752 }
24753 else
24754 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024755 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24756 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024757 if (v == NULL)
24758 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024759 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024760 }
24761 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024762 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024763
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024764 /* Note: the values are copied directly to avoid alloc/free.
24765 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024766 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024767 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024768
24769 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24770 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024771 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24772 fc->l_listitems[ai].li_tv = argvars[i];
24773 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024774 }
24775 }
24776
Bram Moolenaar071d4272004-06-13 20:20:40 +000024777 /* Don't redraw while executing the function. */
24778 ++RedrawingDisabled;
24779 save_sourcing_name = sourcing_name;
24780 save_sourcing_lnum = sourcing_lnum;
24781 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024782 /* need space for function name + ("function " + 3) or "[number]" */
24783 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24784 + STRLEN(fp->uf_name) + 20;
24785 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024786 if (sourcing_name != NULL)
24787 {
24788 if (save_sourcing_name != NULL
24789 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024790 sprintf((char *)sourcing_name, "%s[%d]..",
24791 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024792 else
24793 STRCPY(sourcing_name, "function ");
24794 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24795
24796 if (p_verbose >= 12)
24797 {
24798 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024799 verbose_enter_scroll();
24800
Bram Moolenaar555b2802005-05-19 21:08:39 +000024801 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024802 if (p_verbose >= 14)
24803 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024804 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024805 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024806 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024807 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024808
24809 msg_puts((char_u *)"(");
24810 for (i = 0; i < argcount; ++i)
24811 {
24812 if (i > 0)
24813 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024814 if (argvars[i].v_type == VAR_NUMBER)
24815 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024816 else
24817 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024818 /* Do not want errors such as E724 here. */
24819 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024820 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024821 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024822 if (s != NULL)
24823 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024824 if (vim_strsize(s) > MSG_BUF_CLEN)
24825 {
24826 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24827 s = buf;
24828 }
24829 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024830 vim_free(tofree);
24831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024832 }
24833 }
24834 msg_puts((char_u *)")");
24835 }
24836 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024837
24838 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024839 --no_wait_return;
24840 }
24841 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024842#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024843 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024844 {
24845 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24846 func_do_profile(fp);
24847 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024848 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024849 {
24850 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024851 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024852 profile_zero(&fp->uf_tm_children);
24853 }
24854 script_prof_save(&wait_start);
24855 }
24856#endif
24857
Bram Moolenaar071d4272004-06-13 20:20:40 +000024858 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024859 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024860 save_did_emsg = did_emsg;
24861 did_emsg = FALSE;
24862
24863 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024864 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024865 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24866
24867 --RedrawingDisabled;
24868
24869 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024870 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024871 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024872 clear_tv(rettv);
24873 rettv->v_type = VAR_NUMBER;
24874 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024875 }
24876
Bram Moolenaar05159a02005-02-26 23:04:13 +000024877#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024878 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024879 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024880 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024881 profile_end(&call_start);
24882 profile_sub_wait(&wait_start, &call_start);
24883 profile_add(&fp->uf_tm_total, &call_start);
24884 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024885 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024886 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024887 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24888 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024889 }
24890 }
24891#endif
24892
Bram Moolenaar071d4272004-06-13 20:20:40 +000024893 /* when being verbose, mention the return value */
24894 if (p_verbose >= 12)
24895 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024896 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024897 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024898
Bram Moolenaar071d4272004-06-13 20:20:40 +000024899 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024900 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024901 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024902 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024903 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024904 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024905 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024906 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024907 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024908 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024909 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024910
Bram Moolenaar555b2802005-05-19 21:08:39 +000024911 /* The value may be very long. Skip the middle part, so that we
24912 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024913 * truncate it at the end. Don't want errors such as E724 here. */
24914 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024915 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024916 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024917 if (s != NULL)
24918 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024919 if (vim_strsize(s) > MSG_BUF_CLEN)
24920 {
24921 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24922 s = buf;
24923 }
24924 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024925 vim_free(tofree);
24926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024927 }
24928 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024929
24930 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024931 --no_wait_return;
24932 }
24933
24934 vim_free(sourcing_name);
24935 sourcing_name = save_sourcing_name;
24936 sourcing_lnum = save_sourcing_lnum;
24937 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024938#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024939 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024940 script_prof_restore(&wait_start);
24941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024942
24943 if (p_verbose >= 12 && sourcing_name != NULL)
24944 {
24945 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024946 verbose_enter_scroll();
24947
Bram Moolenaar555b2802005-05-19 21:08:39 +000024948 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024949 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024950
24951 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024952 --no_wait_return;
24953 }
24954
24955 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024956 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024957 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024958
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024959 /* If the a:000 list and the l: and a: dicts are not referenced we can
24960 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024961 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24962 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24963 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24964 {
24965 free_funccal(fc, FALSE);
24966 }
24967 else
24968 {
24969 hashitem_T *hi;
24970 listitem_T *li;
24971 int todo;
24972
24973 /* "fc" is still in use. This can happen when returning "a:000" or
24974 * assigning "l:" to a global variable.
24975 * Link "fc" in the list for garbage collection later. */
24976 fc->caller = previous_funccal;
24977 previous_funccal = fc;
24978
24979 /* Make a copy of the a: variables, since we didn't do that above. */
24980 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24981 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24982 {
24983 if (!HASHITEM_EMPTY(hi))
24984 {
24985 --todo;
24986 v = HI2DI(hi);
24987 copy_tv(&v->di_tv, &v->di_tv);
24988 }
24989 }
24990
24991 /* Make a copy of the a:000 items, since we didn't do that above. */
24992 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24993 copy_tv(&li->li_tv, &li->li_tv);
24994 }
24995}
24996
24997/*
24998 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024999 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025000 */
25001 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025002can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025003{
25004 return (fc->l_varlist.lv_copyID != copyID
25005 && fc->l_vars.dv_copyID != copyID
25006 && fc->l_avars.dv_copyID != copyID);
25007}
25008
25009/*
25010 * Free "fc" and what it contains.
25011 */
25012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025013free_funccal(
25014 funccall_T *fc,
25015 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025016{
25017 listitem_T *li;
25018
25019 /* The a: variables typevals may not have been allocated, only free the
25020 * allocated variables. */
25021 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25022
25023 /* free all l: variables */
25024 vars_clear(&fc->l_vars.dv_hashtab);
25025
25026 /* Free the a:000 variables if they were allocated. */
25027 if (free_val)
25028 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25029 clear_tv(&li->li_tv);
25030
25031 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025032}
25033
25034/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025035 * Add a number variable "name" to dict "dp" with value "nr".
25036 */
25037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025038add_nr_var(
25039 dict_T *dp,
25040 dictitem_T *v,
25041 char *name,
25042 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025043{
25044 STRCPY(v->di_key, name);
25045 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25046 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25047 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025048 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025049 v->di_tv.vval.v_number = nr;
25050}
25051
25052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025053 * ":return [expr]"
25054 */
25055 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025056ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025057{
25058 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025059 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025060 int returning = FALSE;
25061
25062 if (current_funccal == NULL)
25063 {
25064 EMSG(_("E133: :return not inside a function"));
25065 return;
25066 }
25067
25068 if (eap->skip)
25069 ++emsg_skip;
25070
25071 eap->nextcmd = NULL;
25072 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025073 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025074 {
25075 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025076 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025077 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025078 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025079 }
25080 /* It's safer to return also on error. */
25081 else if (!eap->skip)
25082 {
25083 /*
25084 * Return unless the expression evaluation has been cancelled due to an
25085 * aborting error, an interrupt, or an exception.
25086 */
25087 if (!aborting())
25088 returning = do_return(eap, FALSE, TRUE, NULL);
25089 }
25090
25091 /* When skipping or the return gets pending, advance to the next command
25092 * in this line (!returning). Otherwise, ignore the rest of the line.
25093 * Following lines will be ignored by get_func_line(). */
25094 if (returning)
25095 eap->nextcmd = NULL;
25096 else if (eap->nextcmd == NULL) /* no argument */
25097 eap->nextcmd = check_nextcmd(arg);
25098
25099 if (eap->skip)
25100 --emsg_skip;
25101}
25102
25103/*
25104 * Return from a function. Possibly makes the return pending. Also called
25105 * for a pending return at the ":endtry" or after returning from an extra
25106 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025107 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025108 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025109 * FALSE when the return gets pending.
25110 */
25111 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025112do_return(
25113 exarg_T *eap,
25114 int reanimate,
25115 int is_cmd,
25116 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025117{
25118 int idx;
25119 struct condstack *cstack = eap->cstack;
25120
25121 if (reanimate)
25122 /* Undo the return. */
25123 current_funccal->returned = FALSE;
25124
25125 /*
25126 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25127 * not in its finally clause (which then is to be executed next) is found.
25128 * In this case, make the ":return" pending for execution at the ":endtry".
25129 * Otherwise, return normally.
25130 */
25131 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25132 if (idx >= 0)
25133 {
25134 cstack->cs_pending[idx] = CSTP_RETURN;
25135
25136 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025137 /* A pending return again gets pending. "rettv" points to an
25138 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025139 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025140 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025141 else
25142 {
25143 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025144 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025145 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025146 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025147
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025148 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025149 {
25150 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025151 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025152 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025153 else
25154 EMSG(_(e_outofmem));
25155 }
25156 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025157 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025158
25159 if (reanimate)
25160 {
25161 /* The pending return value could be overwritten by a ":return"
25162 * without argument in a finally clause; reset the default
25163 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025164 current_funccal->rettv->v_type = VAR_NUMBER;
25165 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025166 }
25167 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025168 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025169 }
25170 else
25171 {
25172 current_funccal->returned = TRUE;
25173
25174 /* If the return is carried out now, store the return value. For
25175 * a return immediately after reanimation, the value is already
25176 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025177 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025178 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025179 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025180 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025181 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025182 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025183 }
25184 }
25185
25186 return idx < 0;
25187}
25188
25189/*
25190 * Free the variable with a pending return value.
25191 */
25192 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025193discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025194{
Bram Moolenaar33570922005-01-25 22:26:29 +000025195 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025196}
25197
25198/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025199 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025200 * is an allocated string. Used by report_pending() for verbose messages.
25201 */
25202 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025203get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025204{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025205 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025206 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025207 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025208
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025209 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025210 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025211 if (s == NULL)
25212 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025213
25214 STRCPY(IObuff, ":return ");
25215 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25216 if (STRLEN(s) + 8 >= IOSIZE)
25217 STRCPY(IObuff + IOSIZE - 4, "...");
25218 vim_free(tofree);
25219 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025220}
25221
25222/*
25223 * Get next function line.
25224 * Called by do_cmdline() to get the next line.
25225 * Returns allocated string, or NULL for end of function.
25226 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025227 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025228get_func_line(
25229 int c UNUSED,
25230 void *cookie,
25231 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025232{
Bram Moolenaar33570922005-01-25 22:26:29 +000025233 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025234 ufunc_T *fp = fcp->func;
25235 char_u *retval;
25236 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025237
25238 /* If breakpoints have been added/deleted need to check for it. */
25239 if (fcp->dbg_tick != debug_tick)
25240 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025241 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025242 sourcing_lnum);
25243 fcp->dbg_tick = debug_tick;
25244 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025245#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025246 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025247 func_line_end(cookie);
25248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025249
Bram Moolenaar05159a02005-02-26 23:04:13 +000025250 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025251 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25252 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025253 retval = NULL;
25254 else
25255 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025256 /* Skip NULL lines (continuation lines). */
25257 while (fcp->linenr < gap->ga_len
25258 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25259 ++fcp->linenr;
25260 if (fcp->linenr >= gap->ga_len)
25261 retval = NULL;
25262 else
25263 {
25264 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25265 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025266#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025267 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025268 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025269#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025271 }
25272
25273 /* Did we encounter a breakpoint? */
25274 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25275 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025276 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025277 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025278 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025279 sourcing_lnum);
25280 fcp->dbg_tick = debug_tick;
25281 }
25282
25283 return retval;
25284}
25285
Bram Moolenaar05159a02005-02-26 23:04:13 +000025286#if defined(FEAT_PROFILE) || defined(PROTO)
25287/*
25288 * Called when starting to read a function line.
25289 * "sourcing_lnum" must be correct!
25290 * When skipping lines it may not actually be executed, but we won't find out
25291 * until later and we need to store the time now.
25292 */
25293 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025294func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025295{
25296 funccall_T *fcp = (funccall_T *)cookie;
25297 ufunc_T *fp = fcp->func;
25298
25299 if (fp->uf_profiling && sourcing_lnum >= 1
25300 && sourcing_lnum <= fp->uf_lines.ga_len)
25301 {
25302 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025303 /* Skip continuation lines. */
25304 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25305 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025306 fp->uf_tml_execed = FALSE;
25307 profile_start(&fp->uf_tml_start);
25308 profile_zero(&fp->uf_tml_children);
25309 profile_get_wait(&fp->uf_tml_wait);
25310 }
25311}
25312
25313/*
25314 * Called when actually executing a function line.
25315 */
25316 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025317func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025318{
25319 funccall_T *fcp = (funccall_T *)cookie;
25320 ufunc_T *fp = fcp->func;
25321
25322 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25323 fp->uf_tml_execed = TRUE;
25324}
25325
25326/*
25327 * Called when done with a function line.
25328 */
25329 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025330func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025331{
25332 funccall_T *fcp = (funccall_T *)cookie;
25333 ufunc_T *fp = fcp->func;
25334
25335 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25336 {
25337 if (fp->uf_tml_execed)
25338 {
25339 ++fp->uf_tml_count[fp->uf_tml_idx];
25340 profile_end(&fp->uf_tml_start);
25341 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025342 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025343 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25344 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025345 }
25346 fp->uf_tml_idx = -1;
25347 }
25348}
25349#endif
25350
Bram Moolenaar071d4272004-06-13 20:20:40 +000025351/*
25352 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025353 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025354 */
25355 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025356func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025357{
Bram Moolenaar33570922005-01-25 22:26:29 +000025358 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025359
25360 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25361 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025362 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025363 || fcp->returned);
25364}
25365
25366/*
25367 * return TRUE if cookie indicates a function which "abort"s on errors.
25368 */
25369 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025370func_has_abort(
25371 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025372{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025373 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025374}
25375
25376#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25377typedef enum
25378{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025379 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25380 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25381 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025382} var_flavour_T;
25383
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025384static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025385
25386 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025387var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025388{
25389 char_u *p = varname;
25390
25391 if (ASCII_ISUPPER(*p))
25392 {
25393 while (*(++p))
25394 if (ASCII_ISLOWER(*p))
25395 return VAR_FLAVOUR_SESSION;
25396 return VAR_FLAVOUR_VIMINFO;
25397 }
25398 else
25399 return VAR_FLAVOUR_DEFAULT;
25400}
25401#endif
25402
25403#if defined(FEAT_VIMINFO) || defined(PROTO)
25404/*
25405 * Restore global vars that start with a capital from the viminfo file
25406 */
25407 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025408read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025409{
25410 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025411 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025412 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025413 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025414
25415 if (!writing && (find_viminfo_parameter('!') != NULL))
25416 {
25417 tab = vim_strchr(virp->vir_line + 1, '\t');
25418 if (tab != NULL)
25419 {
25420 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025421 switch (*tab)
25422 {
25423 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025424#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025425 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025426#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025427 case 'D': type = VAR_DICT; break;
25428 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025429 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025431
25432 tab = vim_strchr(tab, '\t');
25433 if (tab != NULL)
25434 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025435 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025436 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025437 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025438 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025439#ifdef FEAT_FLOAT
25440 else if (type == VAR_FLOAT)
25441 (void)string2float(tab + 1, &tv.vval.v_float);
25442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025443 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025444 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025445 if (type == VAR_DICT || type == VAR_LIST)
25446 {
25447 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25448
25449 if (etv == NULL)
25450 /* Failed to parse back the dict or list, use it as a
25451 * string. */
25452 tv.v_type = VAR_STRING;
25453 else
25454 {
25455 vim_free(tv.vval.v_string);
25456 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025457 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025458 }
25459 }
25460
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025461 /* when in a function use global variables */
25462 save_funccal = current_funccal;
25463 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025464 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025465 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025466
25467 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025468 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025469 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25470 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025471 }
25472 }
25473 }
25474
25475 return viminfo_readline(virp);
25476}
25477
25478/*
25479 * Write global vars that start with a capital to the viminfo file
25480 */
25481 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025482write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025483{
Bram Moolenaar33570922005-01-25 22:26:29 +000025484 hashitem_T *hi;
25485 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025486 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025487 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025488 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025489 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025490 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025491
25492 if (find_viminfo_parameter('!') == NULL)
25493 return;
25494
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025495 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025496
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025497 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025498 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025499 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025500 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025501 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025502 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025503 this_var = HI2DI(hi);
25504 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025505 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025506 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025507 {
25508 case VAR_STRING: s = "STR"; break;
25509 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025510 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025511 case VAR_DICT: s = "DIC"; break;
25512 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025513 case VAR_SPECIAL: s = "XPL"; break;
25514
25515 case VAR_UNKNOWN:
25516 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025517 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025518 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025519 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025520 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025521 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025522 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025523 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025524 if (p != NULL)
25525 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025526 vim_free(tofree);
25527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025528 }
25529 }
25530}
25531#endif
25532
25533#if defined(FEAT_SESSION) || defined(PROTO)
25534 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025535store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025536{
Bram Moolenaar33570922005-01-25 22:26:29 +000025537 hashitem_T *hi;
25538 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025539 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025540 char_u *p, *t;
25541
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025542 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025543 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025544 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025545 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025546 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025547 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025548 this_var = HI2DI(hi);
25549 if ((this_var->di_tv.v_type == VAR_NUMBER
25550 || this_var->di_tv.v_type == VAR_STRING)
25551 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025552 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025553 /* Escape special characters with a backslash. Turn a LF and
25554 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025555 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025556 (char_u *)"\\\"\n\r");
25557 if (p == NULL) /* out of memory */
25558 break;
25559 for (t = p; *t != NUL; ++t)
25560 if (*t == '\n')
25561 *t = 'n';
25562 else if (*t == '\r')
25563 *t = 'r';
25564 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025565 this_var->di_key,
25566 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25567 : ' ',
25568 p,
25569 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25570 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025571 || put_eol(fd) == FAIL)
25572 {
25573 vim_free(p);
25574 return FAIL;
25575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025576 vim_free(p);
25577 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025578#ifdef FEAT_FLOAT
25579 else if (this_var->di_tv.v_type == VAR_FLOAT
25580 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25581 {
25582 float_T f = this_var->di_tv.vval.v_float;
25583 int sign = ' ';
25584
25585 if (f < 0)
25586 {
25587 f = -f;
25588 sign = '-';
25589 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025590 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025591 this_var->di_key, sign, f) < 0)
25592 || put_eol(fd) == FAIL)
25593 return FAIL;
25594 }
25595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025596 }
25597 }
25598 return OK;
25599}
25600#endif
25601
Bram Moolenaar661b1822005-07-28 22:36:45 +000025602/*
25603 * Display script name where an item was last set.
25604 * Should only be invoked when 'verbose' is non-zero.
25605 */
25606 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025607last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025608{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025609 char_u *p;
25610
Bram Moolenaar661b1822005-07-28 22:36:45 +000025611 if (scriptID != 0)
25612 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025613 p = home_replace_save(NULL, get_scriptname(scriptID));
25614 if (p != NULL)
25615 {
25616 verbose_enter();
25617 MSG_PUTS(_("\n\tLast set from "));
25618 MSG_PUTS(p);
25619 vim_free(p);
25620 verbose_leave();
25621 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025622 }
25623}
25624
Bram Moolenaard812df62008-11-09 12:46:09 +000025625/*
25626 * List v:oldfiles in a nice way.
25627 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025628 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025629ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025630{
25631 list_T *l = vimvars[VV_OLDFILES].vv_list;
25632 listitem_T *li;
25633 int nr = 0;
25634
25635 if (l == NULL)
25636 msg((char_u *)_("No old files"));
25637 else
25638 {
25639 msg_start();
25640 msg_scroll = TRUE;
25641 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25642 {
25643 msg_outnum((long)++nr);
25644 MSG_PUTS(": ");
25645 msg_outtrans(get_tv_string(&li->li_tv));
25646 msg_putchar('\n');
25647 out_flush(); /* output one line at a time */
25648 ui_breakcheck();
25649 }
25650 /* Assume "got_int" was set to truncate the listing. */
25651 got_int = FALSE;
25652
25653#ifdef FEAT_BROWSE_CMD
25654 if (cmdmod.browse)
25655 {
25656 quit_more = FALSE;
25657 nr = prompt_for_number(FALSE);
25658 msg_starthere();
25659 if (nr > 0)
25660 {
25661 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25662 (long)nr);
25663
25664 if (p != NULL)
25665 {
25666 p = expand_env_save(p);
25667 eap->arg = p;
25668 eap->cmdidx = CMD_edit;
25669 cmdmod.browse = FALSE;
25670 do_exedit(eap, NULL);
25671 vim_free(p);
25672 }
25673 }
25674 }
25675#endif
25676 }
25677}
25678
Bram Moolenaar53744302015-07-17 17:38:22 +020025679/* reset v:option_new, v:option_old and v:option_type */
25680 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025681reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025682{
25683 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25684 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25685 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25686}
25687
25688
Bram Moolenaar071d4272004-06-13 20:20:40 +000025689#endif /* FEAT_EVAL */
25690
Bram Moolenaar071d4272004-06-13 20:20:40 +000025691
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025692#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025693
25694#ifdef WIN3264
25695/*
25696 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25697 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025698static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25699static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25700static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025701
25702/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025703 * Get the short path (8.3) for the filename in "fnamep".
25704 * Only works for a valid file name.
25705 * When the path gets longer "fnamep" is changed and the allocated buffer
25706 * is put in "bufp".
25707 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25708 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025709 */
25710 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025711get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025712{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025713 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025714 char_u *newbuf;
25715
25716 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025717 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025718 if (l > len - 1)
25719 {
25720 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025721 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025722 newbuf = vim_strnsave(*fnamep, l);
25723 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025724 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025725
25726 vim_free(*bufp);
25727 *fnamep = *bufp = newbuf;
25728
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025729 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025730 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025731 }
25732
25733 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025734 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025735}
25736
25737/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025738 * Get the short path (8.3) for the filename in "fname". The converted
25739 * path is returned in "bufp".
25740 *
25741 * Some of the directories specified in "fname" may not exist. This function
25742 * will shorten the existing directories at the beginning of the path and then
25743 * append the remaining non-existing path.
25744 *
25745 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025746 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025747 * bufp - Pointer to an allocated buffer for the filename.
25748 * fnamelen - Length of the filename pointed to by fname
25749 *
25750 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025751 */
25752 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025753shortpath_for_invalid_fname(
25754 char_u **fname,
25755 char_u **bufp,
25756 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025757{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025758 char_u *short_fname, *save_fname, *pbuf_unused;
25759 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025760 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025761 int old_len, len;
25762 int new_len, sfx_len;
25763 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025764
25765 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025766 old_len = *fnamelen;
25767 save_fname = vim_strnsave(*fname, old_len);
25768 pbuf_unused = NULL;
25769 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025770
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025771 endp = save_fname + old_len - 1; /* Find the end of the copy */
25772 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025773
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025774 /*
25775 * Try shortening the supplied path till it succeeds by removing one
25776 * directory at a time from the tail of the path.
25777 */
25778 len = 0;
25779 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025780 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025781 /* go back one path-separator */
25782 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25783 --endp;
25784 if (endp <= save_fname)
25785 break; /* processed the complete path */
25786
25787 /*
25788 * Replace the path separator with a NUL and try to shorten the
25789 * resulting path.
25790 */
25791 ch = *endp;
25792 *endp = 0;
25793 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025794 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025795 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25796 {
25797 retval = FAIL;
25798 goto theend;
25799 }
25800 *endp = ch; /* preserve the string */
25801
25802 if (len > 0)
25803 break; /* successfully shortened the path */
25804
25805 /* failed to shorten the path. Skip the path separator */
25806 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025807 }
25808
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025809 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025810 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025811 /*
25812 * Succeeded in shortening the path. Now concatenate the shortened
25813 * path with the remaining path at the tail.
25814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025815
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025816 /* Compute the length of the new path. */
25817 sfx_len = (int)(save_endp - endp) + 1;
25818 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025819
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025820 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025821 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025822 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025823 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025824 /* There is not enough space in the currently allocated string,
25825 * copy it to a buffer big enough. */
25826 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025827 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025828 {
25829 retval = FAIL;
25830 goto theend;
25831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025832 }
25833 else
25834 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025835 /* Transfer short_fname to the main buffer (it's big enough),
25836 * unless get_short_pathname() did its work in-place. */
25837 *fname = *bufp = save_fname;
25838 if (short_fname != save_fname)
25839 vim_strncpy(save_fname, short_fname, len);
25840 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025841 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025842
25843 /* concat the not-shortened part of the path */
25844 vim_strncpy(*fname + len, endp, sfx_len);
25845 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025846 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025847
25848theend:
25849 vim_free(pbuf_unused);
25850 vim_free(save_fname);
25851
25852 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025853}
25854
25855/*
25856 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025857 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025858 */
25859 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025860shortpath_for_partial(
25861 char_u **fnamep,
25862 char_u **bufp,
25863 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025864{
25865 int sepcount, len, tflen;
25866 char_u *p;
25867 char_u *pbuf, *tfname;
25868 int hasTilde;
25869
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025870 /* Count up the path separators from the RHS.. so we know which part
25871 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025872 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025873 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025874 if (vim_ispathsep(*p))
25875 ++sepcount;
25876
25877 /* Need full path first (use expand_env() to remove a "~/") */
25878 hasTilde = (**fnamep == '~');
25879 if (hasTilde)
25880 pbuf = tfname = expand_env_save(*fnamep);
25881 else
25882 pbuf = tfname = FullName_save(*fnamep, FALSE);
25883
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025884 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025885
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025886 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25887 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025888
25889 if (len == 0)
25890 {
25891 /* Don't have a valid filename, so shorten the rest of the
25892 * path if we can. This CAN give us invalid 8.3 filenames, but
25893 * there's not a lot of point in guessing what it might be.
25894 */
25895 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025896 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25897 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025898 }
25899
25900 /* Count the paths backward to find the beginning of the desired string. */
25901 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025902 {
25903#ifdef FEAT_MBYTE
25904 if (has_mbyte)
25905 p -= mb_head_off(tfname, p);
25906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025907 if (vim_ispathsep(*p))
25908 {
25909 if (sepcount == 0 || (hasTilde && sepcount == 1))
25910 break;
25911 else
25912 sepcount --;
25913 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025915 if (hasTilde)
25916 {
25917 --p;
25918 if (p >= tfname)
25919 *p = '~';
25920 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025921 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025922 }
25923 else
25924 ++p;
25925
25926 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25927 vim_free(*bufp);
25928 *fnamelen = (int)STRLEN(p);
25929 *bufp = pbuf;
25930 *fnamep = p;
25931
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025932 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025933}
25934#endif /* WIN3264 */
25935
25936/*
25937 * Adjust a filename, according to a string of modifiers.
25938 * *fnamep must be NUL terminated when called. When returning, the length is
25939 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025940 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025941 * When there is an error, *fnamep is set to NULL.
25942 */
25943 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025944modify_fname(
25945 char_u *src, /* string with modifiers */
25946 int *usedlen, /* characters after src that are used */
25947 char_u **fnamep, /* file name so far */
25948 char_u **bufp, /* buffer for allocated file name or NULL */
25949 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025950{
25951 int valid = 0;
25952 char_u *tail;
25953 char_u *s, *p, *pbuf;
25954 char_u dirname[MAXPATHL];
25955 int c;
25956 int has_fullname = 0;
25957#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025958 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025959 int has_shortname = 0;
25960#endif
25961
25962repeat:
25963 /* ":p" - full path/file_name */
25964 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25965 {
25966 has_fullname = 1;
25967
25968 valid |= VALID_PATH;
25969 *usedlen += 2;
25970
25971 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25972 if ((*fnamep)[0] == '~'
25973#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25974 && ((*fnamep)[1] == '/'
25975# ifdef BACKSLASH_IN_FILENAME
25976 || (*fnamep)[1] == '\\'
25977# endif
25978 || (*fnamep)[1] == NUL)
25979
25980#endif
25981 )
25982 {
25983 *fnamep = expand_env_save(*fnamep);
25984 vim_free(*bufp); /* free any allocated file name */
25985 *bufp = *fnamep;
25986 if (*fnamep == NULL)
25987 return -1;
25988 }
25989
25990 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025991 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025992 {
25993 if (vim_ispathsep(*p)
25994 && p[1] == '.'
25995 && (p[2] == NUL
25996 || vim_ispathsep(p[2])
25997 || (p[2] == '.'
25998 && (p[3] == NUL || vim_ispathsep(p[3])))))
25999 break;
26000 }
26001
26002 /* FullName_save() is slow, don't use it when not needed. */
26003 if (*p != NUL || !vim_isAbsName(*fnamep))
26004 {
26005 *fnamep = FullName_save(*fnamep, *p != NUL);
26006 vim_free(*bufp); /* free any allocated file name */
26007 *bufp = *fnamep;
26008 if (*fnamep == NULL)
26009 return -1;
26010 }
26011
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026012#ifdef WIN3264
26013# if _WIN32_WINNT >= 0x0500
26014 if (vim_strchr(*fnamep, '~') != NULL)
26015 {
26016 /* Expand 8.3 filename to full path. Needed to make sure the same
26017 * file does not have two different names.
26018 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26019 p = alloc(_MAX_PATH + 1);
26020 if (p != NULL)
26021 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026022 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026023 {
26024 vim_free(*bufp);
26025 *bufp = *fnamep = p;
26026 }
26027 else
26028 vim_free(p);
26029 }
26030 }
26031# endif
26032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026033 /* Append a path separator to a directory. */
26034 if (mch_isdir(*fnamep))
26035 {
26036 /* Make room for one or two extra characters. */
26037 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26038 vim_free(*bufp); /* free any allocated file name */
26039 *bufp = *fnamep;
26040 if (*fnamep == NULL)
26041 return -1;
26042 add_pathsep(*fnamep);
26043 }
26044 }
26045
26046 /* ":." - path relative to the current directory */
26047 /* ":~" - path relative to the home directory */
26048 /* ":8" - shortname path - postponed till after */
26049 while (src[*usedlen] == ':'
26050 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26051 {
26052 *usedlen += 2;
26053 if (c == '8')
26054 {
26055#ifdef WIN3264
26056 has_shortname = 1; /* Postpone this. */
26057#endif
26058 continue;
26059 }
26060 pbuf = NULL;
26061 /* Need full path first (use expand_env() to remove a "~/") */
26062 if (!has_fullname)
26063 {
26064 if (c == '.' && **fnamep == '~')
26065 p = pbuf = expand_env_save(*fnamep);
26066 else
26067 p = pbuf = FullName_save(*fnamep, FALSE);
26068 }
26069 else
26070 p = *fnamep;
26071
26072 has_fullname = 0;
26073
26074 if (p != NULL)
26075 {
26076 if (c == '.')
26077 {
26078 mch_dirname(dirname, MAXPATHL);
26079 s = shorten_fname(p, dirname);
26080 if (s != NULL)
26081 {
26082 *fnamep = s;
26083 if (pbuf != NULL)
26084 {
26085 vim_free(*bufp); /* free any allocated file name */
26086 *bufp = pbuf;
26087 pbuf = NULL;
26088 }
26089 }
26090 }
26091 else
26092 {
26093 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26094 /* Only replace it when it starts with '~' */
26095 if (*dirname == '~')
26096 {
26097 s = vim_strsave(dirname);
26098 if (s != NULL)
26099 {
26100 *fnamep = s;
26101 vim_free(*bufp);
26102 *bufp = s;
26103 }
26104 }
26105 }
26106 vim_free(pbuf);
26107 }
26108 }
26109
26110 tail = gettail(*fnamep);
26111 *fnamelen = (int)STRLEN(*fnamep);
26112
26113 /* ":h" - head, remove "/file_name", can be repeated */
26114 /* Don't remove the first "/" or "c:\" */
26115 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26116 {
26117 valid |= VALID_HEAD;
26118 *usedlen += 2;
26119 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026120 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026121 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026122 *fnamelen = (int)(tail - *fnamep);
26123#ifdef VMS
26124 if (*fnamelen > 0)
26125 *fnamelen += 1; /* the path separator is part of the path */
26126#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026127 if (*fnamelen == 0)
26128 {
26129 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26130 p = vim_strsave((char_u *)".");
26131 if (p == NULL)
26132 return -1;
26133 vim_free(*bufp);
26134 *bufp = *fnamep = tail = p;
26135 *fnamelen = 1;
26136 }
26137 else
26138 {
26139 while (tail > s && !after_pathsep(s, tail))
26140 mb_ptr_back(*fnamep, tail);
26141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026142 }
26143
26144 /* ":8" - shortname */
26145 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26146 {
26147 *usedlen += 2;
26148#ifdef WIN3264
26149 has_shortname = 1;
26150#endif
26151 }
26152
26153#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026154 /*
26155 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026156 */
26157 if (has_shortname)
26158 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026159 /* Copy the string if it is shortened by :h and when it wasn't copied
26160 * yet, because we are going to change it in place. Avoids changing
26161 * the buffer name for "%:8". */
26162 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026163 {
26164 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026165 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026166 return -1;
26167 vim_free(*bufp);
26168 *bufp = *fnamep = p;
26169 }
26170
26171 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026172 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026173 if (!has_fullname && !vim_isAbsName(*fnamep))
26174 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026175 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026176 return -1;
26177 }
26178 else
26179 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026180 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026181
Bram Moolenaardc935552011-08-17 15:23:23 +020026182 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026183 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026184 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026185 return -1;
26186
26187 if (l == 0)
26188 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026189 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026190 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026191 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026192 return -1;
26193 }
26194 *fnamelen = l;
26195 }
26196 }
26197#endif /* WIN3264 */
26198
26199 /* ":t" - tail, just the basename */
26200 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26201 {
26202 *usedlen += 2;
26203 *fnamelen -= (int)(tail - *fnamep);
26204 *fnamep = tail;
26205 }
26206
26207 /* ":e" - extension, can be repeated */
26208 /* ":r" - root, without extension, can be repeated */
26209 while (src[*usedlen] == ':'
26210 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26211 {
26212 /* find a '.' in the tail:
26213 * - for second :e: before the current fname
26214 * - otherwise: The last '.'
26215 */
26216 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26217 s = *fnamep - 2;
26218 else
26219 s = *fnamep + *fnamelen - 1;
26220 for ( ; s > tail; --s)
26221 if (s[0] == '.')
26222 break;
26223 if (src[*usedlen + 1] == 'e') /* :e */
26224 {
26225 if (s > tail)
26226 {
26227 *fnamelen += (int)(*fnamep - (s + 1));
26228 *fnamep = s + 1;
26229#ifdef VMS
26230 /* cut version from the extension */
26231 s = *fnamep + *fnamelen - 1;
26232 for ( ; s > *fnamep; --s)
26233 if (s[0] == ';')
26234 break;
26235 if (s > *fnamep)
26236 *fnamelen = s - *fnamep;
26237#endif
26238 }
26239 else if (*fnamep <= tail)
26240 *fnamelen = 0;
26241 }
26242 else /* :r */
26243 {
26244 if (s > tail) /* remove one extension */
26245 *fnamelen = (int)(s - *fnamep);
26246 }
26247 *usedlen += 2;
26248 }
26249
26250 /* ":s?pat?foo?" - substitute */
26251 /* ":gs?pat?foo?" - global substitute */
26252 if (src[*usedlen] == ':'
26253 && (src[*usedlen + 1] == 's'
26254 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26255 {
26256 char_u *str;
26257 char_u *pat;
26258 char_u *sub;
26259 int sep;
26260 char_u *flags;
26261 int didit = FALSE;
26262
26263 flags = (char_u *)"";
26264 s = src + *usedlen + 2;
26265 if (src[*usedlen + 1] == 'g')
26266 {
26267 flags = (char_u *)"g";
26268 ++s;
26269 }
26270
26271 sep = *s++;
26272 if (sep)
26273 {
26274 /* find end of pattern */
26275 p = vim_strchr(s, sep);
26276 if (p != NULL)
26277 {
26278 pat = vim_strnsave(s, (int)(p - s));
26279 if (pat != NULL)
26280 {
26281 s = p + 1;
26282 /* find end of substitution */
26283 p = vim_strchr(s, sep);
26284 if (p != NULL)
26285 {
26286 sub = vim_strnsave(s, (int)(p - s));
26287 str = vim_strnsave(*fnamep, *fnamelen);
26288 if (sub != NULL && str != NULL)
26289 {
26290 *usedlen = (int)(p + 1 - src);
26291 s = do_string_sub(str, pat, sub, flags);
26292 if (s != NULL)
26293 {
26294 *fnamep = s;
26295 *fnamelen = (int)STRLEN(s);
26296 vim_free(*bufp);
26297 *bufp = s;
26298 didit = TRUE;
26299 }
26300 }
26301 vim_free(sub);
26302 vim_free(str);
26303 }
26304 vim_free(pat);
26305 }
26306 }
26307 /* after using ":s", repeat all the modifiers */
26308 if (didit)
26309 goto repeat;
26310 }
26311 }
26312
Bram Moolenaar26df0922014-02-23 23:39:13 +010026313 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26314 {
26315 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26316 if (p == NULL)
26317 return -1;
26318 vim_free(*bufp);
26319 *bufp = *fnamep = p;
26320 *fnamelen = (int)STRLEN(p);
26321 *usedlen += 2;
26322 }
26323
Bram Moolenaar071d4272004-06-13 20:20:40 +000026324 return valid;
26325}
26326
26327/*
26328 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26329 * "flags" can be "g" to do a global substitute.
26330 * Returns an allocated string, NULL for error.
26331 */
26332 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026333do_string_sub(
26334 char_u *str,
26335 char_u *pat,
26336 char_u *sub,
26337 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026338{
26339 int sublen;
26340 regmatch_T regmatch;
26341 int i;
26342 int do_all;
26343 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026344 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026345 garray_T ga;
26346 char_u *ret;
26347 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026348 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026349
26350 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26351 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026352 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026353
26354 ga_init2(&ga, 1, 200);
26355
26356 do_all = (flags[0] == 'g');
26357
26358 regmatch.rm_ic = p_ic;
26359 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26360 if (regmatch.regprog != NULL)
26361 {
26362 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026363 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026364 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26365 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026366 /* Skip empty match except for first match. */
26367 if (regmatch.startp[0] == regmatch.endp[0])
26368 {
26369 if (zero_width == regmatch.startp[0])
26370 {
26371 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026372 i = MB_PTR2LEN(tail);
26373 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26374 (size_t)i);
26375 ga.ga_len += i;
26376 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026377 continue;
26378 }
26379 zero_width = regmatch.startp[0];
26380 }
26381
Bram Moolenaar071d4272004-06-13 20:20:40 +000026382 /*
26383 * Get some space for a temporary buffer to do the substitution
26384 * into. It will contain:
26385 * - The text up to where the match is.
26386 * - The substituted text.
26387 * - The text after the match.
26388 */
26389 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026390 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026391 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26392 {
26393 ga_clear(&ga);
26394 break;
26395 }
26396
26397 /* copy the text up to where the match is */
26398 i = (int)(regmatch.startp[0] - tail);
26399 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26400 /* add the substituted text */
26401 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26402 + ga.ga_len + i, TRUE, TRUE, FALSE);
26403 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026404 tail = regmatch.endp[0];
26405 if (*tail == NUL)
26406 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026407 if (!do_all)
26408 break;
26409 }
26410
26411 if (ga.ga_data != NULL)
26412 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26413
Bram Moolenaar473de612013-06-08 18:19:48 +020026414 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026415 }
26416
26417 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26418 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026419 if (p_cpo == empty_option)
26420 p_cpo = save_cpo;
26421 else
26422 /* Darn, evaluating {sub} expression changed the value. */
26423 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026424
26425 return ret;
26426}
26427
26428#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */