blob: 6280323aa5027fd19001d846331071511c2e2766 [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 Moolenaar8c8de832008-06-24 22:58:06 +0000113
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100114#define NAMESPACE_CHAR (char_u *)"abglstvw"
115
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200116static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000128 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 */
130static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131#define COPYID_INC 2
132#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000133
Bram Moolenaar8502c702014-06-17 12:51:16 +0200134/* Abort conversion to string after a recursion error. */
135static int did_echo_string_emsg = FALSE;
136
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000138 * Array to hold the hashtab with variables local to each sourced script.
139 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000141typedef struct
142{
143 dictitem_T sv_var;
144 dict_T sv_dict;
145} scriptvar_T;
146
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200147static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
148#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
149#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151static int echo_attr = 0; /* attributes used for ":echo" */
152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000153/* Values for trans_function_name() argument: */
154#define TFN_INT 1 /* internal function name OK */
155#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100156#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
157
158/* Values for get_lval() flags argument: */
159#define GLV_QUIET TFN_QUIET /* no error messages */
160#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100361 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000362 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200363 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200364 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200365 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200366 {VV_NAME("option_new", VAR_STRING), VV_RO},
367 {VV_NAME("option_old", VAR_STRING), VV_RO},
368 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100369 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100370 {VV_NAME("false", VAR_SPECIAL), VV_RO},
371 {VV_NAME("true", VAR_SPECIAL), VV_RO},
372 {VV_NAME("null", VAR_SPECIAL), VV_RO},
373 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000374};
375
376/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000377#define vv_type vv_di.di_tv.v_type
378#define vv_nr vv_di.di_tv.vval.v_number
379#define vv_float vv_di.di_tv.vval.v_float
380#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000381#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200382#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000383#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000384
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200385static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000386#define vimvarht vimvardict.dv_hashtab
387
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100388static void prepare_vimvar(int idx, typval_T *save_tv);
389static void restore_vimvar(int idx, typval_T *save_tv);
390static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
391static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
392static char_u *skip_var_one(char_u *arg);
393static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
394static void list_glob_vars(int *first);
395static void list_buf_vars(int *first);
396static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000397#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100398static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000399#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100400static void list_vim_vars(int *first);
401static void list_script_vars(int *first);
402static void list_func_vars(int *first);
403static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
404static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
405static int check_changedtick(char_u *arg);
406static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
407static void clear_lval(lval_T *lp);
408static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
409static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
410static void list_fix_watch(list_T *l, listitem_T *item);
411static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
412static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
413static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
414static void item_lock(typval_T *tv, int deep, int lock);
415static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000416
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100417static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
418static int eval1(char_u **arg, typval_T *rettv, int evaluate);
419static int eval2(char_u **arg, typval_T *rettv, int evaluate);
420static int eval3(char_u **arg, typval_T *rettv, int evaluate);
421static int eval4(char_u **arg, typval_T *rettv, int evaluate);
422static int eval5(char_u **arg, typval_T *rettv, int evaluate);
423static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
424static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000425
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100426static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
427static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
428static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
429static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
430static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
431static long list_len(list_T *l);
432static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
433static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
434static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
435static long list_find_nr(list_T *l, long idx, int *errorp);
436static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100437static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
438static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
439static list_T *list_copy(list_T *orig, int deep, int copyID);
440static char_u *list2string(typval_T *tv, int copyID);
441static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
442static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
443static int free_unref_items(int copyID);
444static dictitem_T *dictitem_copy(dictitem_T *org);
445static void dictitem_remove(dict_T *dict, dictitem_T *item);
446static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
447static long dict_len(dict_T *d);
448static char_u *dict2string(typval_T *tv, int copyID);
449static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
450static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
451static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
452static char_u *string_quote(char_u *str, int function);
453static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
454static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100455static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
456static 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 +0100457static void emsg_funcname(char *ermsg, char_u *name);
458static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000459
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100461static void f_abs(typval_T *argvars, typval_T *rettv);
462static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000463#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100464static void f_add(typval_T *argvars, typval_T *rettv);
465static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
466static void f_and(typval_T *argvars, typval_T *rettv);
467static void f_append(typval_T *argvars, typval_T *rettv);
468static void f_argc(typval_T *argvars, typval_T *rettv);
469static void f_argidx(typval_T *argvars, typval_T *rettv);
470static void f_arglistid(typval_T *argvars, typval_T *rettv);
471static void f_argv(typval_T *argvars, typval_T *rettv);
472static void f_assert_equal(typval_T *argvars, typval_T *rettv);
473static void f_assert_exception(typval_T *argvars, typval_T *rettv);
474static void f_assert_fails(typval_T *argvars, typval_T *rettv);
475static void f_assert_false(typval_T *argvars, typval_T *rettv);
476static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000477#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100478static void f_asin(typval_T *argvars, typval_T *rettv);
479static void f_atan(typval_T *argvars, typval_T *rettv);
480static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100482static void f_browse(typval_T *argvars, typval_T *rettv);
483static void f_browsedir(typval_T *argvars, typval_T *rettv);
484static void f_bufexists(typval_T *argvars, typval_T *rettv);
485static void f_buflisted(typval_T *argvars, typval_T *rettv);
486static void f_bufloaded(typval_T *argvars, typval_T *rettv);
487static void f_bufname(typval_T *argvars, typval_T *rettv);
488static void f_bufnr(typval_T *argvars, typval_T *rettv);
489static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
490static void f_byte2line(typval_T *argvars, typval_T *rettv);
491static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
492static void f_byteidx(typval_T *argvars, typval_T *rettv);
493static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
494static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000495#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100496static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000497#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100498#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100499static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100500static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
501static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100502static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100503static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100504static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100505static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
506static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100507static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100508static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100509static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
510static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100511static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100512static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100513#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100514static void f_changenr(typval_T *argvars, typval_T *rettv);
515static void f_char2nr(typval_T *argvars, typval_T *rettv);
516static void f_cindent(typval_T *argvars, typval_T *rettv);
517static void f_clearmatches(typval_T *argvars, typval_T *rettv);
518static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000519#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100520static void f_complete(typval_T *argvars, typval_T *rettv);
521static void f_complete_add(typval_T *argvars, typval_T *rettv);
522static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000523#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100524static void f_confirm(typval_T *argvars, typval_T *rettv);
525static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000526#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100527static void f_cos(typval_T *argvars, typval_T *rettv);
528static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000529#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100530static void f_count(typval_T *argvars, typval_T *rettv);
531static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
532static void f_cursor(typval_T *argsvars, typval_T *rettv);
533static void f_deepcopy(typval_T *argvars, typval_T *rettv);
534static void f_delete(typval_T *argvars, typval_T *rettv);
535static void f_did_filetype(typval_T *argvars, typval_T *rettv);
536static void f_diff_filler(typval_T *argvars, typval_T *rettv);
537static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100538static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100539static void f_empty(typval_T *argvars, typval_T *rettv);
540static void f_escape(typval_T *argvars, typval_T *rettv);
541static void f_eval(typval_T *argvars, typval_T *rettv);
542static void f_eventhandler(typval_T *argvars, typval_T *rettv);
543static void f_executable(typval_T *argvars, typval_T *rettv);
544static void f_exepath(typval_T *argvars, typval_T *rettv);
545static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200546#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100547static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200548#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100549static void f_expand(typval_T *argvars, typval_T *rettv);
550static void f_extend(typval_T *argvars, typval_T *rettv);
551static void f_feedkeys(typval_T *argvars, typval_T *rettv);
552static void f_filereadable(typval_T *argvars, typval_T *rettv);
553static void f_filewritable(typval_T *argvars, typval_T *rettv);
554static void f_filter(typval_T *argvars, typval_T *rettv);
555static void f_finddir(typval_T *argvars, typval_T *rettv);
556static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000557#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100558static void f_float2nr(typval_T *argvars, typval_T *rettv);
559static void f_floor(typval_T *argvars, typval_T *rettv);
560static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000561#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100562static void f_fnameescape(typval_T *argvars, typval_T *rettv);
563static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
564static void f_foldclosed(typval_T *argvars, typval_T *rettv);
565static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
566static void f_foldlevel(typval_T *argvars, typval_T *rettv);
567static void f_foldtext(typval_T *argvars, typval_T *rettv);
568static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
569static void f_foreground(typval_T *argvars, typval_T *rettv);
570static void f_function(typval_T *argvars, typval_T *rettv);
571static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
572static void f_get(typval_T *argvars, typval_T *rettv);
573static void f_getbufline(typval_T *argvars, typval_T *rettv);
574static void f_getbufvar(typval_T *argvars, typval_T *rettv);
575static void f_getchar(typval_T *argvars, typval_T *rettv);
576static void f_getcharmod(typval_T *argvars, typval_T *rettv);
577static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
578static void f_getcmdline(typval_T *argvars, typval_T *rettv);
579static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
580static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
581static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
582static void f_getcwd(typval_T *argvars, typval_T *rettv);
583static void f_getfontname(typval_T *argvars, typval_T *rettv);
584static void f_getfperm(typval_T *argvars, typval_T *rettv);
585static void f_getfsize(typval_T *argvars, typval_T *rettv);
586static void f_getftime(typval_T *argvars, typval_T *rettv);
587static void f_getftype(typval_T *argvars, typval_T *rettv);
588static void f_getline(typval_T *argvars, typval_T *rettv);
589static void f_getmatches(typval_T *argvars, typval_T *rettv);
590static void f_getpid(typval_T *argvars, typval_T *rettv);
591static void f_getcurpos(typval_T *argvars, typval_T *rettv);
592static void f_getpos(typval_T *argvars, typval_T *rettv);
593static void f_getqflist(typval_T *argvars, typval_T *rettv);
594static void f_getreg(typval_T *argvars, typval_T *rettv);
595static void f_getregtype(typval_T *argvars, typval_T *rettv);
596static void f_gettabvar(typval_T *argvars, typval_T *rettv);
597static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
598static void f_getwinposx(typval_T *argvars, typval_T *rettv);
599static void f_getwinposy(typval_T *argvars, typval_T *rettv);
600static void f_getwinvar(typval_T *argvars, typval_T *rettv);
601static void f_glob(typval_T *argvars, typval_T *rettv);
602static void f_globpath(typval_T *argvars, typval_T *rettv);
603static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
604static void f_has(typval_T *argvars, typval_T *rettv);
605static void f_has_key(typval_T *argvars, typval_T *rettv);
606static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
607static void f_hasmapto(typval_T *argvars, typval_T *rettv);
608static void f_histadd(typval_T *argvars, typval_T *rettv);
609static void f_histdel(typval_T *argvars, typval_T *rettv);
610static void f_histget(typval_T *argvars, typval_T *rettv);
611static void f_histnr(typval_T *argvars, typval_T *rettv);
612static void f_hlID(typval_T *argvars, typval_T *rettv);
613static void f_hlexists(typval_T *argvars, typval_T *rettv);
614static void f_hostname(typval_T *argvars, typval_T *rettv);
615static void f_iconv(typval_T *argvars, typval_T *rettv);
616static void f_indent(typval_T *argvars, typval_T *rettv);
617static void f_index(typval_T *argvars, typval_T *rettv);
618static void f_input(typval_T *argvars, typval_T *rettv);
619static void f_inputdialog(typval_T *argvars, typval_T *rettv);
620static void f_inputlist(typval_T *argvars, typval_T *rettv);
621static void f_inputrestore(typval_T *argvars, typval_T *rettv);
622static void f_inputsave(typval_T *argvars, typval_T *rettv);
623static void f_inputsecret(typval_T *argvars, typval_T *rettv);
624static void f_insert(typval_T *argvars, typval_T *rettv);
625static void f_invert(typval_T *argvars, typval_T *rettv);
626static void f_isdirectory(typval_T *argvars, typval_T *rettv);
627static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100628#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
629static void f_isnan(typval_T *argvars, typval_T *rettv);
630#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100631static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100632#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100633static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100634static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100635static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100636static void f_job_start(typval_T *argvars, typval_T *rettv);
637static void f_job_stop(typval_T *argvars, typval_T *rettv);
638static void f_job_status(typval_T *argvars, typval_T *rettv);
639#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100640static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100641static void f_js_decode(typval_T *argvars, typval_T *rettv);
642static void f_js_encode(typval_T *argvars, typval_T *rettv);
643static void f_json_decode(typval_T *argvars, typval_T *rettv);
644static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100645static void f_keys(typval_T *argvars, typval_T *rettv);
646static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
647static void f_len(typval_T *argvars, typval_T *rettv);
648static void f_libcall(typval_T *argvars, typval_T *rettv);
649static void f_libcallnr(typval_T *argvars, typval_T *rettv);
650static void f_line(typval_T *argvars, typval_T *rettv);
651static void f_line2byte(typval_T *argvars, typval_T *rettv);
652static void f_lispindent(typval_T *argvars, typval_T *rettv);
653static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000654#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100655static void f_log(typval_T *argvars, typval_T *rettv);
656static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000657#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200658#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100659static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200660#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100661static void f_map(typval_T *argvars, typval_T *rettv);
662static void f_maparg(typval_T *argvars, typval_T *rettv);
663static void f_mapcheck(typval_T *argvars, typval_T *rettv);
664static void f_match(typval_T *argvars, typval_T *rettv);
665static void f_matchadd(typval_T *argvars, typval_T *rettv);
666static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
667static void f_matcharg(typval_T *argvars, typval_T *rettv);
668static void f_matchdelete(typval_T *argvars, typval_T *rettv);
669static void f_matchend(typval_T *argvars, typval_T *rettv);
670static void f_matchlist(typval_T *argvars, typval_T *rettv);
671static void f_matchstr(typval_T *argvars, typval_T *rettv);
672static void f_max(typval_T *argvars, typval_T *rettv);
673static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000674#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100675static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000676#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100677static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100678#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100679static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100680#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100681static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
682static void f_nr2char(typval_T *argvars, typval_T *rettv);
683static void f_or(typval_T *argvars, typval_T *rettv);
684static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100685#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100686static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100687#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000688#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100689static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000690#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100691static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
692static void f_printf(typval_T *argvars, typval_T *rettv);
693static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200694#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200696#endif
697#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100698static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200699#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100700static void f_range(typval_T *argvars, typval_T *rettv);
701static void f_readfile(typval_T *argvars, typval_T *rettv);
702static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100703#ifdef FEAT_FLOAT
704static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
705#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100706static void f_reltimestr(typval_T *argvars, typval_T *rettv);
707static void f_remote_expr(typval_T *argvars, typval_T *rettv);
708static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
709static void f_remote_peek(typval_T *argvars, typval_T *rettv);
710static void f_remote_read(typval_T *argvars, typval_T *rettv);
711static void f_remote_send(typval_T *argvars, typval_T *rettv);
712static void f_remove(typval_T *argvars, typval_T *rettv);
713static void f_rename(typval_T *argvars, typval_T *rettv);
714static void f_repeat(typval_T *argvars, typval_T *rettv);
715static void f_resolve(typval_T *argvars, typval_T *rettv);
716static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000717#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100718static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000719#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100720static void f_screenattr(typval_T *argvars, typval_T *rettv);
721static void f_screenchar(typval_T *argvars, typval_T *rettv);
722static void f_screencol(typval_T *argvars, typval_T *rettv);
723static void f_screenrow(typval_T *argvars, typval_T *rettv);
724static void f_search(typval_T *argvars, typval_T *rettv);
725static void f_searchdecl(typval_T *argvars, typval_T *rettv);
726static void f_searchpair(typval_T *argvars, typval_T *rettv);
727static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
728static void f_searchpos(typval_T *argvars, typval_T *rettv);
729static void f_server2client(typval_T *argvars, typval_T *rettv);
730static void f_serverlist(typval_T *argvars, typval_T *rettv);
731static void f_setbufvar(typval_T *argvars, typval_T *rettv);
732static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
733static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100734static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100735static void f_setline(typval_T *argvars, typval_T *rettv);
736static void f_setloclist(typval_T *argvars, typval_T *rettv);
737static void f_setmatches(typval_T *argvars, typval_T *rettv);
738static void f_setpos(typval_T *argvars, typval_T *rettv);
739static void f_setqflist(typval_T *argvars, typval_T *rettv);
740static void f_setreg(typval_T *argvars, typval_T *rettv);
741static void f_settabvar(typval_T *argvars, typval_T *rettv);
742static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
743static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100744#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100745static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100746#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100747static void f_shellescape(typval_T *argvars, typval_T *rettv);
748static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
749static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000750#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100751static void f_sin(typval_T *argvars, typval_T *rettv);
752static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000753#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100754static void f_sort(typval_T *argvars, typval_T *rettv);
755static void f_soundfold(typval_T *argvars, typval_T *rettv);
756static void f_spellbadword(typval_T *argvars, typval_T *rettv);
757static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
758static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000759#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100760static void f_sqrt(typval_T *argvars, typval_T *rettv);
761static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000762#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_str2nr(typval_T *argvars, typval_T *rettv);
764static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000765#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100766static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000767#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100768static void f_stridx(typval_T *argvars, typval_T *rettv);
769static void f_string(typval_T *argvars, typval_T *rettv);
770static void f_strlen(typval_T *argvars, typval_T *rettv);
771static void f_strpart(typval_T *argvars, typval_T *rettv);
772static void f_strridx(typval_T *argvars, typval_T *rettv);
773static void f_strtrans(typval_T *argvars, typval_T *rettv);
774static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
775static void f_strwidth(typval_T *argvars, typval_T *rettv);
776static void f_submatch(typval_T *argvars, typval_T *rettv);
777static void f_substitute(typval_T *argvars, typval_T *rettv);
778static void f_synID(typval_T *argvars, typval_T *rettv);
779static void f_synIDattr(typval_T *argvars, typval_T *rettv);
780static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
781static void f_synstack(typval_T *argvars, typval_T *rettv);
782static void f_synconcealed(typval_T *argvars, typval_T *rettv);
783static void f_system(typval_T *argvars, typval_T *rettv);
784static void f_systemlist(typval_T *argvars, typval_T *rettv);
785static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
786static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
787static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
788static void f_taglist(typval_T *argvars, typval_T *rettv);
789static void f_tagfiles(typval_T *argvars, typval_T *rettv);
790static void f_tempname(typval_T *argvars, typval_T *rettv);
791static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200792#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100793static void f_tan(typval_T *argvars, typval_T *rettv);
794static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200795#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100796#ifdef FEAT_TIMERS
797static void f_timer_start(typval_T *argvars, typval_T *rettv);
798static void f_timer_stop(typval_T *argvars, typval_T *rettv);
799#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100800static void f_tolower(typval_T *argvars, typval_T *rettv);
801static void f_toupper(typval_T *argvars, typval_T *rettv);
802static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000803#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100804static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000805#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100806static void f_type(typval_T *argvars, typval_T *rettv);
807static void f_undofile(typval_T *argvars, typval_T *rettv);
808static void f_undotree(typval_T *argvars, typval_T *rettv);
809static void f_uniq(typval_T *argvars, typval_T *rettv);
810static void f_values(typval_T *argvars, typval_T *rettv);
811static void f_virtcol(typval_T *argvars, typval_T *rettv);
812static void f_visualmode(typval_T *argvars, typval_T *rettv);
813static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100814static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100815static void f_win_getid(typval_T *argvars, typval_T *rettv);
816static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
817static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
818static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100819static void f_winbufnr(typval_T *argvars, typval_T *rettv);
820static void f_wincol(typval_T *argvars, typval_T *rettv);
821static void f_winheight(typval_T *argvars, typval_T *rettv);
822static void f_winline(typval_T *argvars, typval_T *rettv);
823static void f_winnr(typval_T *argvars, typval_T *rettv);
824static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
825static void f_winrestview(typval_T *argvars, typval_T *rettv);
826static void f_winsaveview(typval_T *argvars, typval_T *rettv);
827static void f_winwidth(typval_T *argvars, typval_T *rettv);
828static void f_writefile(typval_T *argvars, typval_T *rettv);
829static void f_wordcount(typval_T *argvars, typval_T *rettv);
830static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000831
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100832static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
833static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
834static int get_env_len(char_u **arg);
835static int get_id_len(char_u **arg);
836static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
837static 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 +0000838#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
839#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
840 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100841static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
842static int eval_isnamec(int c);
843static int eval_isnamec1(int c);
844static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
845static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100846static typval_T *alloc_string_tv(char_u *string);
847static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100848#ifdef FEAT_FLOAT
849static float_T get_tv_float(typval_T *varp);
850#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100851static linenr_T get_tv_lnum(typval_T *argvars);
852static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100853static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
854static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
855static hashtab_T *find_var_ht(char_u *name, char_u **varname);
856static funccall_T *get_funccal(void);
857static void vars_clear_ext(hashtab_T *ht, int free_val);
858static void delete_var(hashtab_T *ht, hashitem_T *hi);
859static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
860static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
861static void set_var(char_u *name, typval_T *varp, int copy);
862static int var_check_ro(int flags, char_u *name, int use_gettext);
863static int var_check_fixed(int flags, char_u *name, int use_gettext);
864static int var_check_func_name(char_u *name, int new_var);
865static int valid_varname(char_u *varname);
866static int tv_check_lock(int lock, char_u *name, int use_gettext);
867static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
868static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100869static 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 +0100870static int eval_fname_script(char_u *p);
871static int eval_fname_sid(char_u *p);
872static void list_func_head(ufunc_T *fp, int indent);
873static ufunc_T *find_func(char_u *name);
874static int function_exists(char_u *name);
875static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000876#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100877static void func_do_profile(ufunc_T *fp);
878static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
879static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000880static int
881# ifdef __BORLANDC__
882 _RTLENTRYF
883# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100884 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000885static int
886# ifdef __BORLANDC__
887 _RTLENTRYF
888# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100889 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000890#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100891static int script_autoload(char_u *name, int reload);
892static char_u *autoload_name(char_u *name);
893static void cat_func_name(char_u *buf, ufunc_T *fp);
894static void func_free(ufunc_T *fp);
895static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
896static int can_free_funccal(funccall_T *fc, int copyID) ;
897static void free_funccal(funccall_T *fc, int free_val);
898static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
899static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
900static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
901static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
902static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
903static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
904static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
905static int write_list(FILE *fd, list_T *list, int binary);
906static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000907
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200908
909#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100910static int compare_func_name(const void *s1, const void *s2);
911static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200912#endif
913
Bram Moolenaar33570922005-01-25 22:26:29 +0000914/*
915 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000916 */
917 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100918eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000919{
Bram Moolenaar33570922005-01-25 22:26:29 +0000920 int i;
921 struct vimvar *p;
922
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200923 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
924 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200925 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000926 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000927 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000928
929 for (i = 0; i < VV_LEN; ++i)
930 {
931 p = &vimvars[i];
932 STRCPY(p->vv_di.di_key, p->vv_name);
933 if (p->vv_flags & VV_RO)
934 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
935 else if (p->vv_flags & VV_RO_SBX)
936 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
937 else
938 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000939
940 /* add to v: scope dict, unless the value is not always available */
941 if (p->vv_type != VAR_UNKNOWN)
942 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000943 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000944 /* add to compat scope dict */
945 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000946 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100947 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
948
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000949 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100950 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200951 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100952 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100953
954 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
955 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
956 set_vim_var_nr(VV_NONE, VVAL_NONE);
957 set_vim_var_nr(VV_NULL, VVAL_NULL);
958
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200959 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200960
961#ifdef EBCDIC
962 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100963 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200964 */
965 sortFunctions();
966#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000967}
968
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000969#if defined(EXITFREE) || defined(PROTO)
970 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100971eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000972{
973 int i;
974 struct vimvar *p;
975
976 for (i = 0; i < VV_LEN; ++i)
977 {
978 p = &vimvars[i];
979 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000980 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000981 vim_free(p->vv_str);
982 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000983 }
984 else if (p->vv_di.di_tv.v_type == VAR_LIST)
985 {
986 list_unref(p->vv_list);
987 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000988 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000989 }
990 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000991 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000992 hash_clear(&compat_hashtab);
993
Bram Moolenaard9fba312005-06-26 22:34:35 +0000994 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100995# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200996 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100997# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000998
999 /* global variables */
1000 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001001
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001002 /* autoloaded script names */
1003 ga_clear_strings(&ga_loaded);
1004
Bram Moolenaarcca74132013-09-25 21:00:28 +02001005 /* Script-local variables. First clear all the variables and in a second
1006 * loop free the scriptvar_T, because a variable in one script might hold
1007 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001008 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001009 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001010 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001011 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001012 ga_clear(&ga_scripts);
1013
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001014 /* unreferenced lists and dicts */
1015 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001016
1017 /* functions */
1018 free_all_functions();
1019 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001020}
1021#endif
1022
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 * Return the name of the executed function.
1025 */
1026 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001027func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001029 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030}
1031
1032/*
1033 * Return the address holding the next breakpoint line for a funccall cookie.
1034 */
1035 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001036func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037{
Bram Moolenaar33570922005-01-25 22:26:29 +00001038 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039}
1040
1041/*
1042 * Return the address holding the debug tick for a funccall cookie.
1043 */
1044 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001045func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046{
Bram Moolenaar33570922005-01-25 22:26:29 +00001047 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048}
1049
1050/*
1051 * Return the nesting level for a funccall cookie.
1052 */
1053 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001054func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055{
Bram Moolenaar33570922005-01-25 22:26:29 +00001056 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057}
1058
1059/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001060funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001062/* pointer to list of previously used funccal, still around because some
1063 * item in it is still being used. */
1064funccall_T *previous_funccal = NULL;
1065
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066/*
1067 * Return TRUE when a function was ended by a ":return" command.
1068 */
1069 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001070current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071{
1072 return current_funccal->returned;
1073}
1074
1075
1076/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 * Set an internal variable to a string value. Creates the variable if it does
1078 * not already exist.
1079 */
1080 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001081set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001083 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001084 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085
1086 val = vim_strsave(value);
1087 if (val != NULL)
1088 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001089 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001090 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001092 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001093 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 }
1095 }
1096}
1097
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001099static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001100static char_u *redir_endp = NULL;
1101static char_u *redir_varname = NULL;
1102
1103/*
1104 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001105 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 * Returns OK if successfully completed the setup. FAIL otherwise.
1107 */
1108 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001109var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110{
1111 int save_emsg;
1112 int err;
1113 typval_T tv;
1114
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001115 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001116 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117 {
1118 EMSG(_(e_invarg));
1119 return FAIL;
1120 }
1121
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001122 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 redir_varname = vim_strsave(name);
1124 if (redir_varname == NULL)
1125 return FAIL;
1126
1127 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1128 if (redir_lval == NULL)
1129 {
1130 var_redir_stop();
1131 return FAIL;
1132 }
1133
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001134 /* The output is stored in growarray "redir_ga" until redirection ends. */
1135 ga_init2(&redir_ga, (int)sizeof(char), 500);
1136
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001138 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001139 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1141 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001142 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143 if (redir_endp != NULL && *redir_endp != NUL)
1144 /* Trailing characters are present after the variable name */
1145 EMSG(_(e_trailing));
1146 else
1147 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001148 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149 var_redir_stop();
1150 return FAIL;
1151 }
1152
1153 /* check if we can write to the variable: set it to or append an empty
1154 * string */
1155 save_emsg = did_emsg;
1156 did_emsg = FALSE;
1157 tv.v_type = VAR_STRING;
1158 tv.vval.v_string = (char_u *)"";
1159 if (append)
1160 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1161 else
1162 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001163 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001164 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001165 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001166 if (err)
1167 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001168 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169 var_redir_stop();
1170 return FAIL;
1171 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001172
1173 return OK;
1174}
1175
1176/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001177 * Append "value[value_len]" to the variable set by var_redir_start().
1178 * The actual appending is postponed until redirection ends, because the value
1179 * appended may in fact be the string we write to, changing it may cause freed
1180 * memory to be used:
1181 * :redir => foo
1182 * :let foo
1183 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001184 */
1185 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001186var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001187{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001188 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001189
1190 if (redir_lval == NULL)
1191 return;
1192
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001193 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001194 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001195 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001196 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001197
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001198 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001199 {
1200 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001201 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001202 }
1203 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001204 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001205}
1206
1207/*
1208 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001209 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001210 */
1211 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001212var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001213{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001214 typval_T tv;
1215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001216 if (redir_lval != NULL)
1217 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001218 /* If there was no error: assign the text to the variable. */
1219 if (redir_endp != NULL)
1220 {
1221 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1222 tv.v_type = VAR_STRING;
1223 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001224 /* Call get_lval() again, if it's inside a Dict or List it may
1225 * have changed. */
1226 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001227 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001228 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1229 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1230 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001231 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001232
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001233 /* free the collected output */
1234 vim_free(redir_ga.ga_data);
1235 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001236
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001237 vim_free(redir_lval);
1238 redir_lval = NULL;
1239 }
1240 vim_free(redir_varname);
1241 redir_varname = NULL;
1242}
1243
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244# if defined(FEAT_MBYTE) || defined(PROTO)
1245 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001246eval_charconvert(
1247 char_u *enc_from,
1248 char_u *enc_to,
1249 char_u *fname_from,
1250 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251{
1252 int err = FALSE;
1253
1254 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1255 set_vim_var_string(VV_CC_TO, enc_to, -1);
1256 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1257 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1258 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1259 err = TRUE;
1260 set_vim_var_string(VV_CC_FROM, NULL, -1);
1261 set_vim_var_string(VV_CC_TO, NULL, -1);
1262 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1263 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1264
1265 if (err)
1266 return FAIL;
1267 return OK;
1268}
1269# endif
1270
1271# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1272 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001273eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274{
1275 int err = FALSE;
1276
1277 set_vim_var_string(VV_FNAME_IN, fname, -1);
1278 set_vim_var_string(VV_CMDARG, args, -1);
1279 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1280 err = TRUE;
1281 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1282 set_vim_var_string(VV_CMDARG, NULL, -1);
1283
1284 if (err)
1285 {
1286 mch_remove(fname);
1287 return FAIL;
1288 }
1289 return OK;
1290}
1291# endif
1292
1293# if defined(FEAT_DIFF) || defined(PROTO)
1294 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001295eval_diff(
1296 char_u *origfile,
1297 char_u *newfile,
1298 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299{
1300 int err = FALSE;
1301
1302 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1303 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1304 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1305 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1306 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1307 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1308 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1309}
1310
1311 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001312eval_patch(
1313 char_u *origfile,
1314 char_u *difffile,
1315 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316{
1317 int err;
1318
1319 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1320 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1321 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1322 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1323 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1324 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1325 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1326}
1327# endif
1328
1329/*
1330 * Top level evaluation function, returning a boolean.
1331 * Sets "error" to TRUE if there was an error.
1332 * Return TRUE or FALSE.
1333 */
1334 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001335eval_to_bool(
1336 char_u *arg,
1337 int *error,
1338 char_u **nextcmd,
1339 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340{
Bram Moolenaar33570922005-01-25 22:26:29 +00001341 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 int retval = FALSE;
1343
1344 if (skip)
1345 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001346 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 else
1349 {
1350 *error = FALSE;
1351 if (!skip)
1352 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001353 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001354 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356 }
1357 if (skip)
1358 --emsg_skip;
1359
1360 return retval;
1361}
1362
1363/*
1364 * Top level evaluation function, returning a string. If "skip" is TRUE,
1365 * only parsing to "nextcmd" is done, without reporting errors. Return
1366 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1367 */
1368 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001369eval_to_string_skip(
1370 char_u *arg,
1371 char_u **nextcmd,
1372 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373{
Bram Moolenaar33570922005-01-25 22:26:29 +00001374 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 char_u *retval;
1376
1377 if (skip)
1378 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001379 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 retval = NULL;
1381 else
1382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001383 retval = vim_strsave(get_tv_string(&tv));
1384 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 }
1386 if (skip)
1387 --emsg_skip;
1388
1389 return retval;
1390}
1391
1392/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001393 * Skip over an expression at "*pp".
1394 * Return FAIL for an error, OK otherwise.
1395 */
1396 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001397skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001398{
Bram Moolenaar33570922005-01-25 22:26:29 +00001399 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001400
1401 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001402 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001403}
1404
1405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001407 * When "convert" is TRUE convert a List into a sequence of lines and convert
1408 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 * Return pointer to allocated memory, or NULL for failure.
1410 */
1411 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001412eval_to_string(
1413 char_u *arg,
1414 char_u **nextcmd,
1415 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416{
Bram Moolenaar33570922005-01-25 22:26:29 +00001417 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001419 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001420#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001421 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001424 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 retval = NULL;
1426 else
1427 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001428 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001429 {
1430 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001431 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001432 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001433 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001434 if (tv.vval.v_list->lv_len > 0)
1435 ga_append(&ga, NL);
1436 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001437 ga_append(&ga, NUL);
1438 retval = (char_u *)ga.ga_data;
1439 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001440#ifdef FEAT_FLOAT
1441 else if (convert && tv.v_type == VAR_FLOAT)
1442 {
1443 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1444 retval = vim_strsave(numbuf);
1445 }
1446#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001447 else
1448 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001449 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 }
1451
1452 return retval;
1453}
1454
1455/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001456 * Call eval_to_string() without using current local variables and using
1457 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 */
1459 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001460eval_to_string_safe(
1461 char_u *arg,
1462 char_u **nextcmd,
1463 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
1465 char_u *retval;
1466 void *save_funccalp;
1467
1468 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001469 if (use_sandbox)
1470 ++sandbox;
1471 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001472 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001473 if (use_sandbox)
1474 --sandbox;
1475 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 restore_funccal(save_funccalp);
1477 return retval;
1478}
1479
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480/*
1481 * Top level evaluation function, returning a number.
1482 * Evaluates "expr" silently.
1483 * Returns -1 for an error.
1484 */
1485 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001486eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487{
Bram Moolenaar33570922005-01-25 22:26:29 +00001488 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001490 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491
1492 ++emsg_off;
1493
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001494 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 retval = -1;
1496 else
1497 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001498 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001499 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 }
1501 --emsg_off;
1502
1503 return retval;
1504}
1505
Bram Moolenaara40058a2005-07-11 22:42:07 +00001506/*
1507 * Prepare v: variable "idx" to be used.
1508 * Save the current typeval in "save_tv".
1509 * When not used yet add the variable to the v: hashtable.
1510 */
1511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001512prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001513{
1514 *save_tv = vimvars[idx].vv_tv;
1515 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1516 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1517}
1518
1519/*
1520 * Restore v: variable "idx" to typeval "save_tv".
1521 * When no longer defined, remove the variable from the v: hashtable.
1522 */
1523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001524restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001525{
1526 hashitem_T *hi;
1527
Bram Moolenaara40058a2005-07-11 22:42:07 +00001528 vimvars[idx].vv_tv = *save_tv;
1529 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1530 {
1531 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1532 if (HASHITEM_EMPTY(hi))
1533 EMSG2(_(e_intern2), "restore_vimvar()");
1534 else
1535 hash_remove(&vimvarht, hi);
1536 }
1537}
1538
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001539#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001540/*
1541 * Evaluate an expression to a list with suggestions.
1542 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001543 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001544 */
1545 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001546eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001547{
1548 typval_T save_val;
1549 typval_T rettv;
1550 list_T *list = NULL;
1551 char_u *p = skipwhite(expr);
1552
1553 /* Set "v:val" to the bad word. */
1554 prepare_vimvar(VV_VAL, &save_val);
1555 vimvars[VV_VAL].vv_type = VAR_STRING;
1556 vimvars[VV_VAL].vv_str = badword;
1557 if (p_verbose == 0)
1558 ++emsg_off;
1559
1560 if (eval1(&p, &rettv, TRUE) == OK)
1561 {
1562 if (rettv.v_type != VAR_LIST)
1563 clear_tv(&rettv);
1564 else
1565 list = rettv.vval.v_list;
1566 }
1567
1568 if (p_verbose == 0)
1569 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001570 restore_vimvar(VV_VAL, &save_val);
1571
1572 return list;
1573}
1574
1575/*
1576 * "list" is supposed to contain two items: a word and a number. Return the
1577 * word in "pp" and the number as the return value.
1578 * Return -1 if anything isn't right.
1579 * Used to get the good word and score from the eval_spell_expr() result.
1580 */
1581 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001582get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001583{
1584 listitem_T *li;
1585
1586 li = list->lv_first;
1587 if (li == NULL)
1588 return -1;
1589 *pp = get_tv_string(&li->li_tv);
1590
1591 li = li->li_next;
1592 if (li == NULL)
1593 return -1;
1594 return get_tv_number(&li->li_tv);
1595}
1596#endif
1597
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001598/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001599 * Top level evaluation function.
1600 * Returns an allocated typval_T with the result.
1601 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001602 */
1603 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001604eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001605{
1606 typval_T *tv;
1607
1608 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001609 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001610 {
1611 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001612 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001613 }
1614
1615 return tv;
1616}
1617
1618
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001620 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001621 * Uses argv[argc] for the function arguments. Only Number and String
1622 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001625 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001626call_vim_function(
1627 char_u *func,
1628 int argc,
1629 char_u **argv,
1630 int safe, /* use the sandbox */
1631 int str_arg_only, /* all arguments are strings */
1632 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633{
Bram Moolenaar33570922005-01-25 22:26:29 +00001634 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 long n;
1636 int len;
1637 int i;
1638 int doesrange;
1639 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001640 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001642 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001644 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645
1646 for (i = 0; i < argc; i++)
1647 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001648 /* Pass a NULL or empty argument as an empty string */
1649 if (argv[i] == NULL || *argv[i] == NUL)
1650 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001651 argvars[i].v_type = VAR_STRING;
1652 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001653 continue;
1654 }
1655
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001656 if (str_arg_only)
1657 len = 0;
1658 else
1659 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001660 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 if (len != 0 && len == (int)STRLEN(argv[i]))
1662 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001663 argvars[i].v_type = VAR_NUMBER;
1664 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 }
1666 else
1667 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001668 argvars[i].v_type = VAR_STRING;
1669 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 }
1671 }
1672
1673 if (safe)
1674 {
1675 save_funccalp = save_funccal();
1676 ++sandbox;
1677 }
1678
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1680 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001682 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 if (safe)
1684 {
1685 --sandbox;
1686 restore_funccal(save_funccalp);
1687 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001688 vim_free(argvars);
1689
1690 if (ret == FAIL)
1691 clear_tv(rettv);
1692
1693 return ret;
1694}
1695
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001696/*
1697 * Call vimL function "func" and return the result as a number.
1698 * Returns -1 when calling the function fails.
1699 * Uses argv[argc] for the function arguments.
1700 */
1701 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001702call_func_retnr(
1703 char_u *func,
1704 int argc,
1705 char_u **argv,
1706 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001707{
1708 typval_T rettv;
1709 long retval;
1710
1711 /* All arguments are passed as strings, no conversion to number. */
1712 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1713 return -1;
1714
1715 retval = get_tv_number_chk(&rettv, NULL);
1716 clear_tv(&rettv);
1717 return retval;
1718}
1719
1720#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1721 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1722
Bram Moolenaar4f688582007-07-24 12:34:30 +00001723# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001724/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001725 * Call vimL function "func" and return the result as a string.
1726 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001727 * Uses argv[argc] for the function arguments.
1728 */
1729 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001730call_func_retstr(
1731 char_u *func,
1732 int argc,
1733 char_u **argv,
1734 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001735{
1736 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001737 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001738
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001739 /* All arguments are passed as strings, no conversion to number. */
1740 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001741 return NULL;
1742
1743 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001744 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 return retval;
1746}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001747# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001748
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001749/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001750 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001751 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001752 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001753 */
1754 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001755call_func_retlist(
1756 char_u *func,
1757 int argc,
1758 char_u **argv,
1759 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001760{
1761 typval_T rettv;
1762
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001763 /* All arguments are passed as strings, no conversion to number. */
1764 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001765 return NULL;
1766
1767 if (rettv.v_type != VAR_LIST)
1768 {
1769 clear_tv(&rettv);
1770 return NULL;
1771 }
1772
1773 return rettv.vval.v_list;
1774}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775#endif
1776
1777/*
1778 * Save the current function call pointer, and set it to NULL.
1779 * Used when executing autocommands and for ":source".
1780 */
1781 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001782save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001784 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 current_funccal = NULL;
1787 return (void *)fc;
1788}
1789
1790 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001791restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001793 funccall_T *fc = (funccall_T *)vfc;
1794
1795 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796}
1797
Bram Moolenaar05159a02005-02-26 23:04:13 +00001798#if defined(FEAT_PROFILE) || defined(PROTO)
1799/*
1800 * Prepare profiling for entering a child or something else that is not
1801 * counted for the script/function itself.
1802 * Should always be called in pair with prof_child_exit().
1803 */
1804 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001805prof_child_enter(
1806 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001807{
1808 funccall_T *fc = current_funccal;
1809
1810 if (fc != NULL && fc->func->uf_profiling)
1811 profile_start(&fc->prof_child);
1812 script_prof_save(tm);
1813}
1814
1815/*
1816 * Take care of time spent in a child.
1817 * Should always be called after prof_child_enter().
1818 */
1819 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001820prof_child_exit(
1821 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001822{
1823 funccall_T *fc = current_funccal;
1824
1825 if (fc != NULL && fc->func->uf_profiling)
1826 {
1827 profile_end(&fc->prof_child);
1828 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1829 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1830 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1831 }
1832 script_prof_restore(tm);
1833}
1834#endif
1835
1836
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837#ifdef FEAT_FOLDING
1838/*
1839 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1840 * it in "*cp". Doesn't give error messages.
1841 */
1842 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001843eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844{
Bram Moolenaar33570922005-01-25 22:26:29 +00001845 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 int retval;
1847 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001848 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1849 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850
1851 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001852 if (use_sandbox)
1853 ++sandbox;
1854 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 retval = 0;
1858 else
1859 {
1860 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 if (tv.v_type == VAR_NUMBER)
1862 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001863 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 retval = 0;
1865 else
1866 {
1867 /* If the result is a string, check if there is a non-digit before
1868 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 if (!VIM_ISDIGIT(*s) && *s != '-')
1871 *cp = *s++;
1872 retval = atol((char *)s);
1873 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 }
1876 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001877 if (use_sandbox)
1878 --sandbox;
1879 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880
1881 return retval;
1882}
1883#endif
1884
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001886 * ":let" list all variable values
1887 * ":let var1 var2" list variable values
1888 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001889 * ":let var += expr" assignment command.
1890 * ":let var -= expr" assignment command.
1891 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 */
1894 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001895ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896{
1897 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001899 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 int var_count = 0;
1902 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001903 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001904 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001905 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906
Bram Moolenaardb552d602006-03-23 22:59:57 +00001907 argend = skip_var_list(arg, &var_count, &semicolon);
1908 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001910 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1911 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001912 expr = skipwhite(argend);
1913 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1914 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001916 /*
1917 * ":let" without "=": list variables
1918 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 if (*arg == '[')
1920 EMSG(_(e_invarg));
1921 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001923 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001925 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001926 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001927 list_glob_vars(&first);
1928 list_buf_vars(&first);
1929 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001930#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001931 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001932#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001933 list_script_vars(&first);
1934 list_func_vars(&first);
1935 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 eap->nextcmd = check_nextcmd(arg);
1938 }
1939 else
1940 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001941 op[0] = '=';
1942 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001943 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001944 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001945 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1946 op[0] = *expr; /* +=, -= or .= */
1947 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001948 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001949 else
1950 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001951
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 if (eap->skip)
1953 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 if (eap->skip)
1956 {
1957 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 --emsg_skip;
1960 }
1961 else if (i != FAIL)
1962 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001963 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001964 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001965 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 }
1967 }
1968}
1969
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001970/*
1971 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1972 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001973 * When "nextchars" is not NULL it points to a string with characters that
1974 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1975 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001976 * Returns OK or FAIL;
1977 */
1978 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001979ex_let_vars(
1980 char_u *arg_start,
1981 typval_T *tv,
1982 int copy, /* copy values from "tv", don't move */
1983 int semicolon, /* from skip_var_list() */
1984 int var_count, /* from skip_var_list() */
1985 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986{
1987 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001988 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001990 listitem_T *item;
1991 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001992
1993 if (*arg != '[')
1994 {
1995 /*
1996 * ":let var = expr" or ":for var in list"
1997 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001998 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 return FAIL;
2000 return OK;
2001 }
2002
2003 /*
2004 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2005 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002006 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002007 {
2008 EMSG(_(e_listreq));
2009 return FAIL;
2010 }
2011
2012 i = list_len(l);
2013 if (semicolon == 0 && var_count < i)
2014 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002015 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002016 return FAIL;
2017 }
2018 if (var_count - semicolon > i)
2019 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002020 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021 return FAIL;
2022 }
2023
2024 item = l->lv_first;
2025 while (*arg != ']')
2026 {
2027 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002028 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002029 item = item->li_next;
2030 if (arg == NULL)
2031 return FAIL;
2032
2033 arg = skipwhite(arg);
2034 if (*arg == ';')
2035 {
2036 /* Put the rest of the list (may be empty) in the var after ';'.
2037 * Create a new list for this. */
2038 l = list_alloc();
2039 if (l == NULL)
2040 return FAIL;
2041 while (item != NULL)
2042 {
2043 list_append_tv(l, &item->li_tv);
2044 item = item->li_next;
2045 }
2046
2047 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002048 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002049 ltv.vval.v_list = l;
2050 l->lv_refcount = 1;
2051
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002052 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2053 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002054 clear_tv(&ltv);
2055 if (arg == NULL)
2056 return FAIL;
2057 break;
2058 }
2059 else if (*arg != ',' && *arg != ']')
2060 {
2061 EMSG2(_(e_intern2), "ex_let_vars()");
2062 return FAIL;
2063 }
2064 }
2065
2066 return OK;
2067}
2068
2069/*
2070 * Skip over assignable variable "var" or list of variables "[var, var]".
2071 * Used for ":let varvar = expr" and ":for varvar in expr".
2072 * For "[var, var]" increment "*var_count" for each variable.
2073 * for "[var, var; var]" set "semicolon".
2074 * Return NULL for an error.
2075 */
2076 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002077skip_var_list(
2078 char_u *arg,
2079 int *var_count,
2080 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002081{
2082 char_u *p, *s;
2083
2084 if (*arg == '[')
2085 {
2086 /* "[var, var]": find the matching ']'. */
2087 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002088 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002089 {
2090 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2091 s = skip_var_one(p);
2092 if (s == p)
2093 {
2094 EMSG2(_(e_invarg2), p);
2095 return NULL;
2096 }
2097 ++*var_count;
2098
2099 p = skipwhite(s);
2100 if (*p == ']')
2101 break;
2102 else if (*p == ';')
2103 {
2104 if (*semicolon == 1)
2105 {
2106 EMSG(_("Double ; in list of variables"));
2107 return NULL;
2108 }
2109 *semicolon = 1;
2110 }
2111 else if (*p != ',')
2112 {
2113 EMSG2(_(e_invarg2), p);
2114 return NULL;
2115 }
2116 }
2117 return p + 1;
2118 }
2119 else
2120 return skip_var_one(arg);
2121}
2122
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002123/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002124 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002125 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002126 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002127 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002128skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002129{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002130 if (*arg == '@' && arg[1] != NUL)
2131 return arg + 2;
2132 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2133 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002134}
2135
Bram Moolenaara7043832005-01-21 11:56:39 +00002136/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002137 * List variables for hashtab "ht" with prefix "prefix".
2138 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002139 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002141list_hashtable_vars(
2142 hashtab_T *ht,
2143 char_u *prefix,
2144 int empty,
2145 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002146{
Bram Moolenaar33570922005-01-25 22:26:29 +00002147 hashitem_T *hi;
2148 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002149 int todo;
2150
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002151 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002152 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2153 {
2154 if (!HASHITEM_EMPTY(hi))
2155 {
2156 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002157 di = HI2DI(hi);
2158 if (empty || di->di_tv.v_type != VAR_STRING
2159 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002161 }
2162 }
2163}
2164
2165/*
2166 * List global variables.
2167 */
2168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002169list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002170{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002171 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002172}
2173
2174/*
2175 * List buffer variables.
2176 */
2177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002178list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002179{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002180 char_u numbuf[NUMBUFLEN];
2181
Bram Moolenaar429fa852013-04-15 12:27:36 +02002182 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002184
2185 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2187 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002188}
2189
2190/*
2191 * List window variables.
2192 */
2193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002194list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002195{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002196 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002197 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002198}
2199
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002200#ifdef FEAT_WINDOWS
2201/*
2202 * List tab page variables.
2203 */
2204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002205list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002206{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002207 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002208 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002209}
2210#endif
2211
Bram Moolenaara7043832005-01-21 11:56:39 +00002212/*
2213 * List Vim variables.
2214 */
2215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002216list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002218 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219}
2220
2221/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002222 * List script-local variables, if there is a script.
2223 */
2224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002225list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002226{
2227 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002228 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2229 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002230}
2231
2232/*
2233 * List function variables, if there is a function.
2234 */
2235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002236list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002237{
2238 if (current_funccal != NULL)
2239 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002240 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002241}
2242
2243/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244 * List variables in "arg".
2245 */
2246 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002247list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248{
2249 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002252 char_u *name_start;
2253 char_u *arg_subsc;
2254 char_u *tofree;
2255 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256
2257 while (!ends_excmd(*arg) && !got_int)
2258 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002261 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2263 {
2264 emsg_severe = TRUE;
2265 EMSG(_(e_trailing));
2266 break;
2267 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002269 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002271 /* get_name_len() takes care of expanding curly braces */
2272 name_start = name = arg;
2273 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2274 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002276 /* This is mainly to keep test 49 working: when expanding
2277 * curly braces fails overrule the exception error message. */
2278 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280 emsg_severe = TRUE;
2281 EMSG2(_(e_invarg2), arg);
2282 break;
2283 }
2284 error = TRUE;
2285 }
2286 else
2287 {
2288 if (tofree != NULL)
2289 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002290 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 else
2293 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294 /* handle d.key, l[idx], f(expr) */
2295 arg_subsc = arg;
2296 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002297 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002299 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002300 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002301 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002303 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002304 case 'g': list_glob_vars(first); break;
2305 case 'b': list_buf_vars(first); break;
2306 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002307#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002308 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002309#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002310 case 'v': list_vim_vars(first); break;
2311 case 's': list_script_vars(first); break;
2312 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002313 default:
2314 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002315 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002316 }
2317 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002318 {
2319 char_u numbuf[NUMBUFLEN];
2320 char_u *tf;
2321 int c;
2322 char_u *s;
2323
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002324 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002325 c = *arg;
2326 *arg = NUL;
2327 list_one_var_a((char_u *)"",
2328 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002329 tv.v_type,
2330 s == NULL ? (char_u *)"" : s,
2331 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002332 *arg = c;
2333 vim_free(tf);
2334 }
2335 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002336 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 }
2338 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002339
2340 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002342
2343 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344 }
2345
2346 return arg;
2347}
2348
2349/*
2350 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2351 * Returns a pointer to the char just after the var name.
2352 * Returns NULL if there is an error.
2353 */
2354 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002355ex_let_one(
2356 char_u *arg, /* points to variable name */
2357 typval_T *tv, /* value to assign to variable */
2358 int copy, /* copy value from "tv" */
2359 char_u *endchars, /* valid chars after variable name or NULL */
2360 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361{
2362 int c1;
2363 char_u *name;
2364 char_u *p;
2365 char_u *arg_end = NULL;
2366 int len;
2367 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002368 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002369
2370 /*
2371 * ":let $VAR = expr": Set environment variable.
2372 */
2373 if (*arg == '$')
2374 {
2375 /* Find the end of the name. */
2376 ++arg;
2377 name = arg;
2378 len = get_env_len(&arg);
2379 if (len == 0)
2380 EMSG2(_(e_invarg2), name - 1);
2381 else
2382 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002383 if (op != NULL && (*op == '+' || *op == '-'))
2384 EMSG2(_(e_letwrong), op);
2385 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002386 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002388 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 {
2390 c1 = name[len];
2391 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002392 p = get_tv_string_chk(tv);
2393 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002394 {
2395 int mustfree = FALSE;
2396 char_u *s = vim_getenv(name, &mustfree);
2397
2398 if (s != NULL)
2399 {
2400 p = tofree = concat_str(s, p);
2401 if (mustfree)
2402 vim_free(s);
2403 }
2404 }
2405 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002406 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002407 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002408 if (STRICMP(name, "HOME") == 0)
2409 init_homedir();
2410 else if (didset_vim && STRICMP(name, "VIM") == 0)
2411 didset_vim = FALSE;
2412 else if (didset_vimruntime
2413 && STRICMP(name, "VIMRUNTIME") == 0)
2414 didset_vimruntime = FALSE;
2415 arg_end = arg;
2416 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002417 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002418 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 }
2420 }
2421 }
2422
2423 /*
2424 * ":let &option = expr": Set option value.
2425 * ":let &l:option = expr": Set local option value.
2426 * ":let &g:option = expr": Set global option value.
2427 */
2428 else if (*arg == '&')
2429 {
2430 /* Find the end of the name. */
2431 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002432 if (p == NULL || (endchars != NULL
2433 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 EMSG(_(e_letunexp));
2435 else
2436 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437 long n;
2438 int opt_type;
2439 long numval;
2440 char_u *stringval = NULL;
2441 char_u *s;
2442
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002443 c1 = *p;
2444 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002445
2446 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002447 s = get_tv_string_chk(tv); /* != NULL if number or string */
2448 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002449 {
2450 opt_type = get_option_value(arg, &numval,
2451 &stringval, opt_flags);
2452 if ((opt_type == 1 && *op == '.')
2453 || (opt_type == 0 && *op != '.'))
2454 EMSG2(_(e_letwrong), op);
2455 else
2456 {
2457 if (opt_type == 1) /* number */
2458 {
2459 if (*op == '+')
2460 n = numval + n;
2461 else
2462 n = numval - n;
2463 }
2464 else if (opt_type == 0 && stringval != NULL) /* string */
2465 {
2466 s = concat_str(stringval, s);
2467 vim_free(stringval);
2468 stringval = s;
2469 }
2470 }
2471 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002472 if (s != NULL)
2473 {
2474 set_option_value(arg, n, s, opt_flags);
2475 arg_end = p;
2476 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002479 }
2480 }
2481
2482 /*
2483 * ":let @r = expr": Set register contents.
2484 */
2485 else if (*arg == '@')
2486 {
2487 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 if (op != NULL && (*op == '+' || *op == '-'))
2489 EMSG2(_(e_letwrong), op);
2490 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002491 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 EMSG(_(e_letunexp));
2493 else
2494 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002495 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002496 char_u *s;
2497
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002498 p = get_tv_string_chk(tv);
2499 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002500 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002501 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 if (s != NULL)
2503 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002504 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002505 vim_free(s);
2506 }
2507 }
2508 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002509 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002510 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002511 arg_end = arg + 1;
2512 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002513 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 }
2515 }
2516
2517 /*
2518 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002521 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002523 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002525 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002527 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2529 EMSG(_(e_letunexp));
2530 else
2531 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002532 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 arg_end = p;
2534 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002535 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537 }
2538
2539 else
2540 EMSG2(_(e_invarg2), arg);
2541
2542 return arg_end;
2543}
2544
2545/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002546 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2547 */
2548 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002549check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002550{
2551 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2552 {
2553 EMSG2(_(e_readonlyvar), arg);
2554 return TRUE;
2555 }
2556 return FALSE;
2557}
2558
2559/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 * Get an lval: variable, Dict item or List item that can be assigned a value
2561 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2562 * "name.key", "name.key[expr]" etc.
2563 * Indexing only works if "name" is an existing List or Dictionary.
2564 * "name" points to the start of the name.
2565 * If "rettv" is not NULL it points to the value to be assigned.
2566 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2567 * wrong; must end in space or cmd separator.
2568 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002569 * flags:
2570 * GLV_QUIET: do not give error messages
2571 * GLV_NO_AUTOLOAD: do not use script autoloading
2572 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002574 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002576 */
2577 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002578get_lval(
2579 char_u *name,
2580 typval_T *rettv,
2581 lval_T *lp,
2582 int unlet,
2583 int skip,
2584 int flags, /* GLV_ values */
2585 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002586{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002587 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 char_u *expr_start, *expr_end;
2589 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002590 dictitem_T *v;
2591 typval_T var1;
2592 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002594 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002595 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002596 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002597 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002598 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002601 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602
2603 if (skip)
2604 {
2605 /* When skipping just find the end of the name. */
2606 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002607 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 }
2609
2610 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002611 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 if (expr_start != NULL)
2613 {
2614 /* Don't expand the name when we already know there is an error. */
2615 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2616 && *p != '[' && *p != '.')
2617 {
2618 EMSG(_(e_trailing));
2619 return NULL;
2620 }
2621
2622 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2623 if (lp->ll_exp_name == NULL)
2624 {
2625 /* Report an invalid expression in braces, unless the
2626 * expression evaluation has been cancelled due to an
2627 * aborting error, an interrupt, or an exception. */
2628 if (!aborting() && !quiet)
2629 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002630 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 EMSG2(_(e_invarg2), name);
2632 return NULL;
2633 }
2634 }
2635 lp->ll_name = lp->ll_exp_name;
2636 }
2637 else
2638 lp->ll_name = name;
2639
2640 /* Without [idx] or .key we are done. */
2641 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2642 return p;
2643
2644 cc = *p;
2645 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002646 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 if (v == NULL && !quiet)
2648 EMSG2(_(e_undefvar), lp->ll_name);
2649 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002650 if (v == NULL)
2651 return NULL;
2652
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 /*
2654 * Loop until no more [idx] or .key is following.
2655 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002656 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2660 && !(lp->ll_tv->v_type == VAR_DICT
2661 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002662 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 if (!quiet)
2664 EMSG(_("E689: Can only index a List or Dictionary"));
2665 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002666 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002668 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 if (!quiet)
2670 EMSG(_("E708: [:] must come last"));
2671 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002672 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002673
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 len = -1;
2675 if (*p == '.')
2676 {
2677 key = p + 1;
2678 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2679 ;
2680 if (len == 0)
2681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (!quiet)
2683 EMSG(_(e_emptykey));
2684 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 }
2686 p = key + len;
2687 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 else
2689 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 if (*p == ':')
2693 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002694 else
2695 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 empty1 = FALSE;
2697 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002699 if (get_tv_string_chk(&var1) == NULL)
2700 {
2701 /* not a number or string */
2702 clear_tv(&var1);
2703 return NULL;
2704 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 }
2706
2707 /* Optionally get the second index [ :expr]. */
2708 if (*p == ':')
2709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002713 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002714 if (!empty1)
2715 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002717 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 if (rettv != NULL && (rettv->v_type != VAR_LIST
2719 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 if (!quiet)
2722 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 if (!empty1)
2724 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 }
2727 p = skipwhite(p + 1);
2728 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 else
2731 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2734 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 if (!empty1)
2736 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002739 if (get_tv_string_chk(&var2) == NULL)
2740 {
2741 /* not a number or string */
2742 if (!empty1)
2743 clear_tv(&var1);
2744 clear_tv(&var2);
2745 return NULL;
2746 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002749 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002752
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002754 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (!quiet)
2756 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 if (!empty1)
2758 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 }
2763
2764 /* Skip to past ']'. */
2765 ++p;
2766 }
2767
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 {
2770 if (len == -1)
2771 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002773 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 if (*key == NUL)
2775 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 if (!quiet)
2777 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002779 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 }
2781 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002782 lp->ll_list = NULL;
2783 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002784 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002785
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002786 /* When assigning to a scope dictionary check that a function and
2787 * variable name is valid (only variable name unless it is l: or
2788 * g: dictionary). Disallow overwriting a builtin function. */
2789 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002790 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002791 int prevval;
2792 int wrong;
2793
2794 if (len != -1)
2795 {
2796 prevval = key[len];
2797 key[len] = NUL;
2798 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002799 else
2800 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002801 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2802 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002803 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002804 || !valid_varname(key);
2805 if (len != -1)
2806 key[len] = prevval;
2807 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002808 return NULL;
2809 }
2810
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002813 /* Can't add "v:" variable. */
2814 if (lp->ll_dict == &vimvardict)
2815 {
2816 EMSG2(_(e_illvar), name);
2817 return NULL;
2818 }
2819
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002820 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002824 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 if (len == -1)
2826 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 }
2829 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 if (len == -1)
2834 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 p = NULL;
2837 break;
2838 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002839 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002840 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002841 return NULL;
2842
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 if (len == -1)
2844 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 }
2847 else
2848 {
2849 /*
2850 * Get the number and item for the only or first index of the List.
2851 */
2852 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 else
2855 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002856 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 clear_tv(&var1);
2858 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002859 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 lp->ll_list = lp->ll_tv->vval.v_list;
2861 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2862 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002864 if (lp->ll_n1 < 0)
2865 {
2866 lp->ll_n1 = 0;
2867 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2868 }
2869 }
2870 if (lp->ll_li == NULL)
2871 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002874 if (!quiet)
2875 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002877 }
2878
2879 /*
2880 * May need to find the item or absolute index for the second
2881 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 * When no index given: "lp->ll_empty2" is TRUE.
2883 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002886 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002887 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002888 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002890 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002892 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002893 {
2894 if (!quiet)
2895 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002897 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002899 }
2900
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2902 if (lp->ll_n1 < 0)
2903 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2904 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002905 {
2906 if (!quiet)
2907 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002909 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002910 }
2911
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002913 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002914 }
2915
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 return p;
2917}
2918
2919/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002920 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 */
2922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002923clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924{
2925 vim_free(lp->ll_exp_name);
2926 vim_free(lp->ll_newkey);
2927}
2928
2929/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002930 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002932 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 */
2934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002935set_var_lval(
2936 lval_T *lp,
2937 char_u *endp,
2938 typval_T *rettv,
2939 int copy,
2940 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941{
2942 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002943 listitem_T *ri;
2944 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945
2946 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002947 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002949 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950 cc = *endp;
2951 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002952 if (op != NULL && *op != '=')
2953 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002954 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002955
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002956 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002957 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002958 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002959 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002960 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002961 if ((di == NULL
2962 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2963 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2964 FALSE)))
2965 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002966 set_var(lp->ll_name, &tv, FALSE);
2967 clear_tv(&tv);
2968 }
2969 }
2970 else
2971 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002973 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002974 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002975 else if (tv_check_lock(lp->ll_newkey == NULL
2976 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002977 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002978 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002979 else if (lp->ll_range)
2980 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002981 listitem_T *ll_li = lp->ll_li;
2982 int ll_n1 = lp->ll_n1;
2983
2984 /*
2985 * Check whether any of the list items is locked
2986 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002987 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002988 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002989 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002990 return;
2991 ri = ri->li_next;
2992 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2993 break;
2994 ll_li = ll_li->li_next;
2995 ++ll_n1;
2996 }
2997
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002998 /*
2999 * Assign the List values to the list items.
3000 */
3001 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003002 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003003 if (op != NULL && *op != '=')
3004 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3005 else
3006 {
3007 clear_tv(&lp->ll_li->li_tv);
3008 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3009 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003010 ri = ri->li_next;
3011 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3012 break;
3013 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003014 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003015 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003016 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003017 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003018 ri = NULL;
3019 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003020 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003021 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003022 lp->ll_li = lp->ll_li->li_next;
3023 ++lp->ll_n1;
3024 }
3025 if (ri != NULL)
3026 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003027 else if (lp->ll_empty2
3028 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003029 : lp->ll_n1 != lp->ll_n2)
3030 EMSG(_("E711: List value has not enough items"));
3031 }
3032 else
3033 {
3034 /*
3035 * Assign to a List or Dictionary item.
3036 */
3037 if (lp->ll_newkey != NULL)
3038 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003039 if (op != NULL && *op != '=')
3040 {
3041 EMSG2(_(e_letwrong), op);
3042 return;
3043 }
3044
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003045 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003046 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003047 if (di == NULL)
3048 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003049 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3050 {
3051 vim_free(di);
3052 return;
3053 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003054 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003055 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003056 else if (op != NULL && *op != '=')
3057 {
3058 tv_op(lp->ll_tv, rettv, op);
3059 return;
3060 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003061 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003062 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003063
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003064 /*
3065 * Assign the value to the variable or list item.
3066 */
3067 if (copy)
3068 copy_tv(rettv, lp->ll_tv);
3069 else
3070 {
3071 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003072 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003073 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003074 }
3075 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003076}
3077
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003078/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003079 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3080 * Returns OK or FAIL.
3081 */
3082 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003083tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003084{
3085 long n;
3086 char_u numbuf[NUMBUFLEN];
3087 char_u *s;
3088
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003089 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3090 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3091 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003092 {
3093 switch (tv1->v_type)
3094 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003095 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003096 case VAR_DICT:
3097 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003098 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003099 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003100 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003101 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003102 break;
3103
3104 case VAR_LIST:
3105 if (*op != '+' || tv2->v_type != VAR_LIST)
3106 break;
3107 /* List += List */
3108 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3109 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3110 return OK;
3111
3112 case VAR_NUMBER:
3113 case VAR_STRING:
3114 if (tv2->v_type == VAR_LIST)
3115 break;
3116 if (*op == '+' || *op == '-')
3117 {
3118 /* nr += nr or nr -= nr*/
3119 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003120#ifdef FEAT_FLOAT
3121 if (tv2->v_type == VAR_FLOAT)
3122 {
3123 float_T f = n;
3124
3125 if (*op == '+')
3126 f += tv2->vval.v_float;
3127 else
3128 f -= tv2->vval.v_float;
3129 clear_tv(tv1);
3130 tv1->v_type = VAR_FLOAT;
3131 tv1->vval.v_float = f;
3132 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003133 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003134#endif
3135 {
3136 if (*op == '+')
3137 n += get_tv_number(tv2);
3138 else
3139 n -= get_tv_number(tv2);
3140 clear_tv(tv1);
3141 tv1->v_type = VAR_NUMBER;
3142 tv1->vval.v_number = n;
3143 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003144 }
3145 else
3146 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003147 if (tv2->v_type == VAR_FLOAT)
3148 break;
3149
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003150 /* str .= str */
3151 s = get_tv_string(tv1);
3152 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3153 clear_tv(tv1);
3154 tv1->v_type = VAR_STRING;
3155 tv1->vval.v_string = s;
3156 }
3157 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003158
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003159 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003160#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003161 {
3162 float_T f;
3163
3164 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3165 && tv2->v_type != VAR_NUMBER
3166 && tv2->v_type != VAR_STRING))
3167 break;
3168 if (tv2->v_type == VAR_FLOAT)
3169 f = tv2->vval.v_float;
3170 else
3171 f = get_tv_number(tv2);
3172 if (*op == '+')
3173 tv1->vval.v_float += f;
3174 else
3175 tv1->vval.v_float -= f;
3176 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003177#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003178 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003179 }
3180 }
3181
3182 EMSG2(_(e_letwrong), op);
3183 return FAIL;
3184}
3185
3186/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003187 * Add a watcher to a list.
3188 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003189 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003190list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191{
3192 lw->lw_next = l->lv_watch;
3193 l->lv_watch = lw;
3194}
3195
3196/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003197 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198 * No warning when it isn't found...
3199 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003200 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003201list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003202{
Bram Moolenaar33570922005-01-25 22:26:29 +00003203 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204
3205 lwp = &l->lv_watch;
3206 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3207 {
3208 if (lw == lwrem)
3209 {
3210 *lwp = lw->lw_next;
3211 break;
3212 }
3213 lwp = &lw->lw_next;
3214 }
3215}
3216
3217/*
3218 * Just before removing an item from a list: advance watchers to the next
3219 * item.
3220 */
3221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003222list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003223{
Bram Moolenaar33570922005-01-25 22:26:29 +00003224 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003225
3226 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3227 if (lw->lw_item == item)
3228 lw->lw_item = item->li_next;
3229}
3230
3231/*
3232 * Evaluate the expression used in a ":for var in expr" command.
3233 * "arg" points to "var".
3234 * Set "*errp" to TRUE for an error, FALSE otherwise;
3235 * Return a pointer that holds the info. Null when there is an error.
3236 */
3237 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003238eval_for_line(
3239 char_u *arg,
3240 int *errp,
3241 char_u **nextcmdp,
3242 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003243{
Bram Moolenaar33570922005-01-25 22:26:29 +00003244 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003246 typval_T tv;
3247 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003248
3249 *errp = TRUE; /* default: there is an error */
3250
Bram Moolenaar33570922005-01-25 22:26:29 +00003251 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003252 if (fi == NULL)
3253 return NULL;
3254
3255 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3256 if (expr == NULL)
3257 return fi;
3258
3259 expr = skipwhite(expr);
3260 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3261 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003262 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003263 return fi;
3264 }
3265
3266 if (skip)
3267 ++emsg_skip;
3268 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3269 {
3270 *errp = FALSE;
3271 if (!skip)
3272 {
3273 l = tv.vval.v_list;
3274 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003275 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003277 clear_tv(&tv);
3278 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 else
3280 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003281 /* No need to increment the refcount, it's already set for the
3282 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283 fi->fi_list = l;
3284 list_add_watch(l, &fi->fi_lw);
3285 fi->fi_lw.lw_item = l->lv_first;
3286 }
3287 }
3288 }
3289 if (skip)
3290 --emsg_skip;
3291
3292 return fi;
3293}
3294
3295/*
3296 * Use the first item in a ":for" list. Advance to the next.
3297 * Assign the values to the variable (list). "arg" points to the first one.
3298 * Return TRUE when a valid item was found, FALSE when at end of list or
3299 * something wrong.
3300 */
3301 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003302next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003303{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003304 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003305 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003306 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003307
3308 item = fi->fi_lw.lw_item;
3309 if (item == NULL)
3310 result = FALSE;
3311 else
3312 {
3313 fi->fi_lw.lw_item = item->li_next;
3314 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3315 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3316 }
3317 return result;
3318}
3319
3320/*
3321 * Free the structure used to store info used by ":for".
3322 */
3323 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003324free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003325{
Bram Moolenaar33570922005-01-25 22:26:29 +00003326 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003327
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003328 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003329 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003330 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003331 list_unref(fi->fi_list);
3332 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003333 vim_free(fi);
3334}
3335
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3337
3338 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003339set_context_for_expression(
3340 expand_T *xp,
3341 char_u *arg,
3342 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343{
3344 int got_eq = FALSE;
3345 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003346 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348 if (cmdidx == CMD_let)
3349 {
3350 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003351 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003352 {
3353 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003354 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003355 {
3356 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003357 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003358 if (vim_iswhite(*p))
3359 break;
3360 }
3361 return;
3362 }
3363 }
3364 else
3365 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3366 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 while ((xp->xp_pattern = vim_strpbrk(arg,
3368 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3369 {
3370 c = *xp->xp_pattern;
3371 if (c == '&')
3372 {
3373 c = xp->xp_pattern[1];
3374 if (c == '&')
3375 {
3376 ++xp->xp_pattern;
3377 xp->xp_context = cmdidx != CMD_let || got_eq
3378 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3379 }
3380 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003381 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003383 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3384 xp->xp_pattern += 2;
3385
3386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 }
3388 else if (c == '$')
3389 {
3390 /* environment variable */
3391 xp->xp_context = EXPAND_ENV_VARS;
3392 }
3393 else if (c == '=')
3394 {
3395 got_eq = TRUE;
3396 xp->xp_context = EXPAND_EXPRESSION;
3397 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003398 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 && xp->xp_context == EXPAND_FUNCTIONS
3400 && vim_strchr(xp->xp_pattern, '(') == NULL)
3401 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003402 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 break;
3404 }
3405 else if (cmdidx != CMD_let || got_eq)
3406 {
3407 if (c == '"') /* string */
3408 {
3409 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3410 if (c == '\\' && xp->xp_pattern[1] != NUL)
3411 ++xp->xp_pattern;
3412 xp->xp_context = EXPAND_NOTHING;
3413 }
3414 else if (c == '\'') /* literal string */
3415 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003416 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3418 /* skip */ ;
3419 xp->xp_context = EXPAND_NOTHING;
3420 }
3421 else if (c == '|')
3422 {
3423 if (xp->xp_pattern[1] == '|')
3424 {
3425 ++xp->xp_pattern;
3426 xp->xp_context = EXPAND_EXPRESSION;
3427 }
3428 else
3429 xp->xp_context = EXPAND_COMMANDS;
3430 }
3431 else
3432 xp->xp_context = EXPAND_EXPRESSION;
3433 }
3434 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003435 /* Doesn't look like something valid, expand as an expression
3436 * anyway. */
3437 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 arg = xp->xp_pattern;
3439 if (*arg != NUL)
3440 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3441 /* skip */ ;
3442 }
3443 xp->xp_pattern = arg;
3444}
3445
3446#endif /* FEAT_CMDL_COMPL */
3447
3448/*
3449 * ":1,25call func(arg1, arg2)" function call.
3450 */
3451 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003452ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453{
3454 char_u *arg = eap->arg;
3455 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003457 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003459 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 linenr_T lnum;
3461 int doesrange;
3462 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003463 funcdict_T fudi;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003464 partial_T *partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003466 if (eap->skip)
3467 {
3468 /* trans_function_name() doesn't work well when skipping, use eval0()
3469 * instead to skip to any following command, e.g. for:
3470 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003471 ++emsg_skip;
3472 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3473 clear_tv(&rettv);
3474 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003475 return;
3476 }
3477
Bram Moolenaar65639032016-03-16 21:40:30 +01003478 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003479 if (fudi.fd_newkey != NULL)
3480 {
3481 /* Still need to give an error message for missing key. */
3482 EMSG2(_(e_dictkey), fudi.fd_newkey);
3483 vim_free(fudi.fd_newkey);
3484 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003485 if (tofree == NULL)
3486 return;
3487
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003488 /* Increase refcount on dictionary, it could get deleted when evaluating
3489 * the arguments. */
3490 if (fudi.fd_dict != NULL)
3491 ++fudi.fd_dict->dv_refcount;
3492
Bram Moolenaar65639032016-03-16 21:40:30 +01003493 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3494 * contents. For VAR_PARTIAL get its partial, unless we already have one
3495 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003496 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003497 name = deref_func_name(tofree, &len,
3498 partial != NULL ? NULL : &partial, FALSE);
3499
3500 /* When calling fdict.func(), where "func" is a partial, use "fdict"
3501 * instead of the dict in the partial, for backwards compatibility.
3502 * TODO: Do use the arguments in the partial? */
3503 if (fudi.fd_dict != NULL)
3504 partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505
Bram Moolenaar532c7802005-01-27 14:44:31 +00003506 /* Skip white space to allow ":call func ()". Not good, but required for
3507 * backward compatibility. */
3508 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003509 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510
3511 if (*startarg != '(')
3512 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003513 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 goto end;
3515 }
3516
3517 /*
3518 * When skipping, evaluate the function once, to find the end of the
3519 * arguments.
3520 * When the function takes a range, this is discovered after the first
3521 * call, and the loop is broken.
3522 */
3523 if (eap->skip)
3524 {
3525 ++emsg_skip;
3526 lnum = eap->line2; /* do it once, also with an invalid range */
3527 }
3528 else
3529 lnum = eap->line1;
3530 for ( ; lnum <= eap->line2; ++lnum)
3531 {
3532 if (!eap->skip && eap->addr_count > 0)
3533 {
3534 curwin->w_cursor.lnum = lnum;
3535 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003536#ifdef FEAT_VIRTUALEDIT
3537 curwin->w_cursor.coladd = 0;
3538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 }
3540 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003541 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003542 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003543 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 {
3545 failed = TRUE;
3546 break;
3547 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003548
3549 /* Handle a function returning a Funcref, Dictionary or List. */
3550 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3551 {
3552 failed = TRUE;
3553 break;
3554 }
3555
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003556 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 if (doesrange || eap->skip)
3558 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003559
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003561 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003562 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003563 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 if (aborting())
3565 break;
3566 }
3567 if (eap->skip)
3568 --emsg_skip;
3569
3570 if (!failed)
3571 {
3572 /* Check for trailing illegal characters and a following command. */
3573 if (!ends_excmd(*arg))
3574 {
3575 emsg_severe = TRUE;
3576 EMSG(_(e_trailing));
3577 }
3578 else
3579 eap->nextcmd = check_nextcmd(arg);
3580 }
3581
3582end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003583 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003584 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585}
3586
3587/*
3588 * ":unlet[!] var1 ... " command.
3589 */
3590 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003591ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003593 ex_unletlock(eap, eap->arg, 0);
3594}
3595
3596/*
3597 * ":lockvar" and ":unlockvar" commands
3598 */
3599 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003600ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003601{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003603 int deep = 2;
3604
3605 if (eap->forceit)
3606 deep = -1;
3607 else if (vim_isdigit(*arg))
3608 {
3609 deep = getdigits(&arg);
3610 arg = skipwhite(arg);
3611 }
3612
3613 ex_unletlock(eap, arg, deep);
3614}
3615
3616/*
3617 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3618 */
3619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003620ex_unletlock(
3621 exarg_T *eap,
3622 char_u *argstart,
3623 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003624{
3625 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003628 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629
3630 do
3631 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003632 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003633 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003634 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003635 if (lv.ll_name == NULL)
3636 error = TRUE; /* error but continue parsing */
3637 if (name_end == NULL || (!vim_iswhite(*name_end)
3638 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003640 if (name_end != NULL)
3641 {
3642 emsg_severe = TRUE;
3643 EMSG(_(e_trailing));
3644 }
3645 if (!(eap->skip || error))
3646 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 break;
3648 }
3649
3650 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003651 {
3652 if (eap->cmdidx == CMD_unlet)
3653 {
3654 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3655 error = TRUE;
3656 }
3657 else
3658 {
3659 if (do_lock_var(&lv, name_end, deep,
3660 eap->cmdidx == CMD_lockvar) == FAIL)
3661 error = TRUE;
3662 }
3663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003665 if (!eap->skip)
3666 clear_lval(&lv);
3667
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 arg = skipwhite(name_end);
3669 } while (!ends_excmd(*arg));
3670
3671 eap->nextcmd = check_nextcmd(arg);
3672}
3673
Bram Moolenaar8c711452005-01-14 21:53:12 +00003674 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003675do_unlet_var(
3676 lval_T *lp,
3677 char_u *name_end,
3678 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003679{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003680 int ret = OK;
3681 int cc;
3682
3683 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003684 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003685 cc = *name_end;
3686 *name_end = NUL;
3687
3688 /* Normal name or expanded name. */
3689 if (check_changedtick(lp->ll_name))
3690 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003691 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003692 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003693 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003694 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003695 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003696 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003697 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003698 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003699 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003700 else if (lp->ll_range)
3701 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003702 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003703 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003704 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003705
3706 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3707 {
3708 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003709 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003710 return FAIL;
3711 ll_li = li;
3712 ++ll_n1;
3713 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003714
3715 /* Delete a range of List items. */
3716 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3717 {
3718 li = lp->ll_li->li_next;
3719 listitem_remove(lp->ll_list, lp->ll_li);
3720 lp->ll_li = li;
3721 ++lp->ll_n1;
3722 }
3723 }
3724 else
3725 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003726 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003727 /* unlet a List item. */
3728 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003729 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003730 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003731 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003732 }
3733
3734 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003735}
3736
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737/*
3738 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003739 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 */
3741 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003742do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743{
Bram Moolenaar33570922005-01-25 22:26:29 +00003744 hashtab_T *ht;
3745 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003746 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003747 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003748 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749
Bram Moolenaar33570922005-01-25 22:26:29 +00003750 ht = find_var_ht(name, &varname);
3751 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003753 if (ht == &globvarht)
3754 d = &globvardict;
3755 else if (current_funccal != NULL
3756 && ht == &current_funccal->l_vars.dv_hashtab)
3757 d = &current_funccal->l_vars;
3758 else if (ht == &compat_hashtab)
3759 d = &vimvardict;
3760 else
3761 {
3762 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3763 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3764 }
3765 if (d == NULL)
3766 {
3767 EMSG2(_(e_intern2), "do_unlet()");
3768 return FAIL;
3769 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003770 hi = hash_find(ht, varname);
3771 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003772 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003773 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003774 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003775 || var_check_ro(di->di_flags, name, FALSE)
3776 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003777 return FAIL;
3778
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003779 delete_var(ht, hi);
3780 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003783 if (forceit)
3784 return OK;
3785 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 return FAIL;
3787}
3788
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003789/*
3790 * Lock or unlock variable indicated by "lp".
3791 * "deep" is the levels to go (-1 for unlimited);
3792 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3793 */
3794 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003795do_lock_var(
3796 lval_T *lp,
3797 char_u *name_end,
3798 int deep,
3799 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003800{
3801 int ret = OK;
3802 int cc;
3803 dictitem_T *di;
3804
3805 if (deep == 0) /* nothing to do */
3806 return OK;
3807
3808 if (lp->ll_tv == NULL)
3809 {
3810 cc = *name_end;
3811 *name_end = NUL;
3812
3813 /* Normal name or expanded name. */
3814 if (check_changedtick(lp->ll_name))
3815 ret = FAIL;
3816 else
3817 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003818 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003819 if (di == NULL)
3820 ret = FAIL;
3821 else
3822 {
3823 if (lock)
3824 di->di_flags |= DI_FLAGS_LOCK;
3825 else
3826 di->di_flags &= ~DI_FLAGS_LOCK;
3827 item_lock(&di->di_tv, deep, lock);
3828 }
3829 }
3830 *name_end = cc;
3831 }
3832 else if (lp->ll_range)
3833 {
3834 listitem_T *li = lp->ll_li;
3835
3836 /* (un)lock a range of List items. */
3837 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3838 {
3839 item_lock(&li->li_tv, deep, lock);
3840 li = li->li_next;
3841 ++lp->ll_n1;
3842 }
3843 }
3844 else if (lp->ll_list != NULL)
3845 /* (un)lock a List item. */
3846 item_lock(&lp->ll_li->li_tv, deep, lock);
3847 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003848 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003849 item_lock(&lp->ll_di->di_tv, deep, lock);
3850
3851 return ret;
3852}
3853
3854/*
3855 * Lock or unlock an item. "deep" is nr of levels to go.
3856 */
3857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003858item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003859{
3860 static int recurse = 0;
3861 list_T *l;
3862 listitem_T *li;
3863 dict_T *d;
3864 hashitem_T *hi;
3865 int todo;
3866
3867 if (recurse >= DICT_MAXNEST)
3868 {
3869 EMSG(_("E743: variable nested too deep for (un)lock"));
3870 return;
3871 }
3872 if (deep == 0)
3873 return;
3874 ++recurse;
3875
3876 /* lock/unlock the item itself */
3877 if (lock)
3878 tv->v_lock |= VAR_LOCKED;
3879 else
3880 tv->v_lock &= ~VAR_LOCKED;
3881
3882 switch (tv->v_type)
3883 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003884 case VAR_UNKNOWN:
3885 case VAR_NUMBER:
3886 case VAR_STRING:
3887 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003888 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003889 case VAR_FLOAT:
3890 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003891 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003892 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003893 break;
3894
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003895 case VAR_LIST:
3896 if ((l = tv->vval.v_list) != NULL)
3897 {
3898 if (lock)
3899 l->lv_lock |= VAR_LOCKED;
3900 else
3901 l->lv_lock &= ~VAR_LOCKED;
3902 if (deep < 0 || deep > 1)
3903 /* recursive: lock/unlock the items the List contains */
3904 for (li = l->lv_first; li != NULL; li = li->li_next)
3905 item_lock(&li->li_tv, deep - 1, lock);
3906 }
3907 break;
3908 case VAR_DICT:
3909 if ((d = tv->vval.v_dict) != NULL)
3910 {
3911 if (lock)
3912 d->dv_lock |= VAR_LOCKED;
3913 else
3914 d->dv_lock &= ~VAR_LOCKED;
3915 if (deep < 0 || deep > 1)
3916 {
3917 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003918 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003919 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3920 {
3921 if (!HASHITEM_EMPTY(hi))
3922 {
3923 --todo;
3924 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3925 }
3926 }
3927 }
3928 }
3929 }
3930 --recurse;
3931}
3932
Bram Moolenaara40058a2005-07-11 22:42:07 +00003933/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003934 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3935 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003936 */
3937 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003938tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003939{
3940 return (tv->v_lock & VAR_LOCKED)
3941 || (tv->v_type == VAR_LIST
3942 && tv->vval.v_list != NULL
3943 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3944 || (tv->v_type == VAR_DICT
3945 && tv->vval.v_dict != NULL
3946 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3947}
3948
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3950/*
3951 * Delete all "menutrans_" variables.
3952 */
3953 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003954del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955{
Bram Moolenaar33570922005-01-25 22:26:29 +00003956 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003957 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958
Bram Moolenaar33570922005-01-25 22:26:29 +00003959 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003960 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003961 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003962 {
3963 if (!HASHITEM_EMPTY(hi))
3964 {
3965 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003966 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3967 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003968 }
3969 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003970 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971}
3972#endif
3973
3974#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3975
3976/*
3977 * Local string buffer for the next two functions to store a variable name
3978 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3979 * get_user_var_name().
3980 */
3981
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003982static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983
3984static char_u *varnamebuf = NULL;
3985static int varnamebuflen = 0;
3986
3987/*
3988 * Function to concatenate a prefix and a variable name.
3989 */
3990 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003991cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992{
3993 int len;
3994
3995 len = (int)STRLEN(name) + 3;
3996 if (len > varnamebuflen)
3997 {
3998 vim_free(varnamebuf);
3999 len += 10; /* some additional space */
4000 varnamebuf = alloc(len);
4001 if (varnamebuf == NULL)
4002 {
4003 varnamebuflen = 0;
4004 return NULL;
4005 }
4006 varnamebuflen = len;
4007 }
4008 *varnamebuf = prefix;
4009 varnamebuf[1] = ':';
4010 STRCPY(varnamebuf + 2, name);
4011 return varnamebuf;
4012}
4013
4014/*
4015 * Function given to ExpandGeneric() to obtain the list of user defined
4016 * (global/buffer/window/built-in) variable names.
4017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004019get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004021 static long_u gdone;
4022 static long_u bdone;
4023 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004024#ifdef FEAT_WINDOWS
4025 static long_u tdone;
4026#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004027 static int vidx;
4028 static hashitem_T *hi;
4029 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030
4031 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004032 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004033 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004034#ifdef FEAT_WINDOWS
4035 tdone = 0;
4036#endif
4037 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004038
4039 /* Global variables */
4040 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004042 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004043 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004044 else
4045 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004046 while (HASHITEM_EMPTY(hi))
4047 ++hi;
4048 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4049 return cat_prefix_varname('g', hi->hi_key);
4050 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004052
4053 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004054 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004055 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004057 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004058 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004059 else
4060 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004061 while (HASHITEM_EMPTY(hi))
4062 ++hi;
4063 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004065 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004067 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 return (char_u *)"b:changedtick";
4069 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004070
4071 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004072 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004073 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004075 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004076 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004077 else
4078 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004079 while (HASHITEM_EMPTY(hi))
4080 ++hi;
4081 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004083
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004084#ifdef FEAT_WINDOWS
4085 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004086 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004087 if (tdone < ht->ht_used)
4088 {
4089 if (tdone++ == 0)
4090 hi = ht->ht_array;
4091 else
4092 ++hi;
4093 while (HASHITEM_EMPTY(hi))
4094 ++hi;
4095 return cat_prefix_varname('t', hi->hi_key);
4096 }
4097#endif
4098
Bram Moolenaar33570922005-01-25 22:26:29 +00004099 /* v: variables */
4100 if (vidx < VV_LEN)
4101 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102
4103 vim_free(varnamebuf);
4104 varnamebuf = NULL;
4105 varnamebuflen = 0;
4106 return NULL;
4107}
4108
4109#endif /* FEAT_CMDL_COMPL */
4110
4111/*
4112 * types for expressions.
4113 */
4114typedef enum
4115{
4116 TYPE_UNKNOWN = 0
4117 , TYPE_EQUAL /* == */
4118 , TYPE_NEQUAL /* != */
4119 , TYPE_GREATER /* > */
4120 , TYPE_GEQUAL /* >= */
4121 , TYPE_SMALLER /* < */
4122 , TYPE_SEQUAL /* <= */
4123 , TYPE_MATCH /* =~ */
4124 , TYPE_NOMATCH /* !~ */
4125} exptype_T;
4126
4127/*
4128 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004129 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4131 */
4132
4133/*
4134 * Handle zero level expression.
4135 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004136 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004137 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 * Return OK or FAIL.
4139 */
4140 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004141eval0(
4142 char_u *arg,
4143 typval_T *rettv,
4144 char_u **nextcmd,
4145 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146{
4147 int ret;
4148 char_u *p;
4149
4150 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004151 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 if (ret == FAIL || !ends_excmd(*p))
4153 {
4154 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004155 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 /*
4157 * Report the invalid expression unless the expression evaluation has
4158 * been cancelled due to an aborting error, an interrupt, or an
4159 * exception.
4160 */
4161 if (!aborting())
4162 EMSG2(_(e_invexpr2), arg);
4163 ret = FAIL;
4164 }
4165 if (nextcmd != NULL)
4166 *nextcmd = check_nextcmd(p);
4167
4168 return ret;
4169}
4170
4171/*
4172 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004173 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 *
4175 * "arg" must point to the first non-white of the expression.
4176 * "arg" is advanced to the next non-white after the recognized expression.
4177 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004178 * Note: "rettv.v_lock" is not set.
4179 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 * Return OK or FAIL.
4181 */
4182 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004183eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184{
4185 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004186 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187
4188 /*
4189 * Get the first variable.
4190 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004191 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 return FAIL;
4193
4194 if ((*arg)[0] == '?')
4195 {
4196 result = FALSE;
4197 if (evaluate)
4198 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004199 int error = FALSE;
4200
4201 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004203 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004204 if (error)
4205 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 }
4207
4208 /*
4209 * Get the second variable.
4210 */
4211 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004212 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 return FAIL;
4214
4215 /*
4216 * Check for the ":".
4217 */
4218 if ((*arg)[0] != ':')
4219 {
4220 EMSG(_("E109: Missing ':' after '?'"));
4221 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004222 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 return FAIL;
4224 }
4225
4226 /*
4227 * Get the third variable.
4228 */
4229 *arg = skipwhite(*arg + 1);
4230 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4231 {
4232 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004233 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 return FAIL;
4235 }
4236 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004237 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 }
4239
4240 return OK;
4241}
4242
4243/*
4244 * Handle first level expression:
4245 * expr2 || expr2 || expr2 logical OR
4246 *
4247 * "arg" must point to the first non-white of the expression.
4248 * "arg" is advanced to the next non-white after the recognized expression.
4249 *
4250 * Return OK or FAIL.
4251 */
4252 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004253eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254{
Bram Moolenaar33570922005-01-25 22:26:29 +00004255 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 long result;
4257 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004258 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259
4260 /*
4261 * Get the first variable.
4262 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 return FAIL;
4265
4266 /*
4267 * Repeat until there is no following "||".
4268 */
4269 first = TRUE;
4270 result = FALSE;
4271 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4272 {
4273 if (evaluate && first)
4274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004275 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004278 if (error)
4279 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 first = FALSE;
4281 }
4282
4283 /*
4284 * Get the second variable.
4285 */
4286 *arg = skipwhite(*arg + 2);
4287 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4288 return FAIL;
4289
4290 /*
4291 * Compute the result.
4292 */
4293 if (evaluate && !result)
4294 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004295 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004298 if (error)
4299 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 }
4301 if (evaluate)
4302 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004303 rettv->v_type = VAR_NUMBER;
4304 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 }
4306 }
4307
4308 return OK;
4309}
4310
4311/*
4312 * Handle second level expression:
4313 * expr3 && expr3 && expr3 logical AND
4314 *
4315 * "arg" must point to the first non-white of the expression.
4316 * "arg" is advanced to the next non-white after the recognized expression.
4317 *
4318 * Return OK or FAIL.
4319 */
4320 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004321eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322{
Bram Moolenaar33570922005-01-25 22:26:29 +00004323 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 long result;
4325 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004326 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327
4328 /*
4329 * Get the first variable.
4330 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004331 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 return FAIL;
4333
4334 /*
4335 * Repeat until there is no following "&&".
4336 */
4337 first = TRUE;
4338 result = TRUE;
4339 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4340 {
4341 if (evaluate && first)
4342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004343 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004345 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004346 if (error)
4347 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 first = FALSE;
4349 }
4350
4351 /*
4352 * Get the second variable.
4353 */
4354 *arg = skipwhite(*arg + 2);
4355 if (eval4(arg, &var2, evaluate && result) == FAIL)
4356 return FAIL;
4357
4358 /*
4359 * Compute the result.
4360 */
4361 if (evaluate && result)
4362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004363 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004365 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004366 if (error)
4367 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
4369 if (evaluate)
4370 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004371 rettv->v_type = VAR_NUMBER;
4372 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 }
4374 }
4375
4376 return OK;
4377}
4378
4379/*
4380 * Handle third level expression:
4381 * var1 == var2
4382 * var1 =~ var2
4383 * var1 != var2
4384 * var1 !~ var2
4385 * var1 > var2
4386 * var1 >= var2
4387 * var1 < var2
4388 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004389 * var1 is var2
4390 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 *
4392 * "arg" must point to the first non-white of the expression.
4393 * "arg" is advanced to the next non-white after the recognized expression.
4394 *
4395 * Return OK or FAIL.
4396 */
4397 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004398eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399{
Bram Moolenaar33570922005-01-25 22:26:29 +00004400 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 char_u *p;
4402 int i;
4403 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004404 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 int len = 2;
4406 long n1, n2;
4407 char_u *s1, *s2;
4408 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4409 regmatch_T regmatch;
4410 int ic;
4411 char_u *save_cpo;
4412
4413 /*
4414 * Get the first variable.
4415 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004416 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 return FAIL;
4418
4419 p = *arg;
4420 switch (p[0])
4421 {
4422 case '=': if (p[1] == '=')
4423 type = TYPE_EQUAL;
4424 else if (p[1] == '~')
4425 type = TYPE_MATCH;
4426 break;
4427 case '!': if (p[1] == '=')
4428 type = TYPE_NEQUAL;
4429 else if (p[1] == '~')
4430 type = TYPE_NOMATCH;
4431 break;
4432 case '>': if (p[1] != '=')
4433 {
4434 type = TYPE_GREATER;
4435 len = 1;
4436 }
4437 else
4438 type = TYPE_GEQUAL;
4439 break;
4440 case '<': if (p[1] != '=')
4441 {
4442 type = TYPE_SMALLER;
4443 len = 1;
4444 }
4445 else
4446 type = TYPE_SEQUAL;
4447 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004448 case 'i': if (p[1] == 's')
4449 {
4450 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4451 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004452 i = p[len];
4453 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004454 {
4455 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4456 type_is = TRUE;
4457 }
4458 }
4459 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 }
4461
4462 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004463 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 */
4465 if (type != TYPE_UNKNOWN)
4466 {
4467 /* extra question mark appended: ignore case */
4468 if (p[len] == '?')
4469 {
4470 ic = TRUE;
4471 ++len;
4472 }
4473 /* extra '#' appended: match case */
4474 else if (p[len] == '#')
4475 {
4476 ic = FALSE;
4477 ++len;
4478 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004479 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 else
4481 ic = p_ic;
4482
4483 /*
4484 * Get the second variable.
4485 */
4486 *arg = skipwhite(p + len);
4487 if (eval5(arg, &var2, evaluate) == FAIL)
4488 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004489 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 return FAIL;
4491 }
4492
4493 if (evaluate)
4494 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004495 if (type_is && rettv->v_type != var2.v_type)
4496 {
4497 /* For "is" a different type always means FALSE, for "notis"
4498 * it means TRUE. */
4499 n1 = (type == TYPE_NEQUAL);
4500 }
4501 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4502 {
4503 if (type_is)
4504 {
4505 n1 = (rettv->v_type == var2.v_type
4506 && rettv->vval.v_list == var2.vval.v_list);
4507 if (type == TYPE_NEQUAL)
4508 n1 = !n1;
4509 }
4510 else if (rettv->v_type != var2.v_type
4511 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4512 {
4513 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004514 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004515 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004516 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004517 clear_tv(rettv);
4518 clear_tv(&var2);
4519 return FAIL;
4520 }
4521 else
4522 {
4523 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004524 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4525 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004526 if (type == TYPE_NEQUAL)
4527 n1 = !n1;
4528 }
4529 }
4530
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004531 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4532 {
4533 if (type_is)
4534 {
4535 n1 = (rettv->v_type == var2.v_type
4536 && rettv->vval.v_dict == var2.vval.v_dict);
4537 if (type == TYPE_NEQUAL)
4538 n1 = !n1;
4539 }
4540 else if (rettv->v_type != var2.v_type
4541 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4542 {
4543 if (rettv->v_type != var2.v_type)
4544 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4545 else
4546 EMSG(_("E736: Invalid operation for Dictionary"));
4547 clear_tv(rettv);
4548 clear_tv(&var2);
4549 return FAIL;
4550 }
4551 else
4552 {
4553 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004554 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4555 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004556 if (type == TYPE_NEQUAL)
4557 n1 = !n1;
4558 }
4559 }
4560
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004561 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4562 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004563 {
4564 if (rettv->v_type != var2.v_type
4565 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4566 {
4567 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004568 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004569 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004570 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004571 clear_tv(rettv);
4572 clear_tv(&var2);
4573 return FAIL;
4574 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004575 else if (rettv->v_type == VAR_PARTIAL)
4576 {
4577 /* Partials are only equal when identical. */
4578 n1 = rettv->vval.v_partial != NULL
4579 && rettv->vval.v_partial == var2.vval.v_partial;
4580 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004581 else
4582 {
4583 /* Compare two Funcrefs for being equal or unequal. */
4584 if (rettv->vval.v_string == NULL
4585 || var2.vval.v_string == NULL)
4586 n1 = FALSE;
4587 else
4588 n1 = STRCMP(rettv->vval.v_string,
4589 var2.vval.v_string) == 0;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004590 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004591 if (type == TYPE_NEQUAL)
4592 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004593 }
4594
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004595#ifdef FEAT_FLOAT
4596 /*
4597 * If one of the two variables is a float, compare as a float.
4598 * When using "=~" or "!~", always compare as string.
4599 */
4600 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4601 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4602 {
4603 float_T f1, f2;
4604
4605 if (rettv->v_type == VAR_FLOAT)
4606 f1 = rettv->vval.v_float;
4607 else
4608 f1 = get_tv_number(rettv);
4609 if (var2.v_type == VAR_FLOAT)
4610 f2 = var2.vval.v_float;
4611 else
4612 f2 = get_tv_number(&var2);
4613 n1 = FALSE;
4614 switch (type)
4615 {
4616 case TYPE_EQUAL: n1 = (f1 == f2); break;
4617 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4618 case TYPE_GREATER: n1 = (f1 > f2); break;
4619 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4620 case TYPE_SMALLER: n1 = (f1 < f2); break;
4621 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4622 case TYPE_UNKNOWN:
4623 case TYPE_MATCH:
4624 case TYPE_NOMATCH: break; /* avoid gcc warning */
4625 }
4626 }
4627#endif
4628
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 /*
4630 * If one of the two variables is a number, compare as a number.
4631 * When using "=~" or "!~", always compare as string.
4632 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004633 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4635 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004636 n1 = get_tv_number(rettv);
4637 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 switch (type)
4639 {
4640 case TYPE_EQUAL: n1 = (n1 == n2); break;
4641 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4642 case TYPE_GREATER: n1 = (n1 > n2); break;
4643 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4644 case TYPE_SMALLER: n1 = (n1 < n2); break;
4645 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4646 case TYPE_UNKNOWN:
4647 case TYPE_MATCH:
4648 case TYPE_NOMATCH: break; /* avoid gcc warning */
4649 }
4650 }
4651 else
4652 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004653 s1 = get_tv_string_buf(rettv, buf1);
4654 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4656 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4657 else
4658 i = 0;
4659 n1 = FALSE;
4660 switch (type)
4661 {
4662 case TYPE_EQUAL: n1 = (i == 0); break;
4663 case TYPE_NEQUAL: n1 = (i != 0); break;
4664 case TYPE_GREATER: n1 = (i > 0); break;
4665 case TYPE_GEQUAL: n1 = (i >= 0); break;
4666 case TYPE_SMALLER: n1 = (i < 0); break;
4667 case TYPE_SEQUAL: n1 = (i <= 0); break;
4668
4669 case TYPE_MATCH:
4670 case TYPE_NOMATCH:
4671 /* avoid 'l' flag in 'cpoptions' */
4672 save_cpo = p_cpo;
4673 p_cpo = (char_u *)"";
4674 regmatch.regprog = vim_regcomp(s2,
4675 RE_MAGIC + RE_STRING);
4676 regmatch.rm_ic = ic;
4677 if (regmatch.regprog != NULL)
4678 {
4679 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004680 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 if (type == TYPE_NOMATCH)
4682 n1 = !n1;
4683 }
4684 p_cpo = save_cpo;
4685 break;
4686
4687 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4688 }
4689 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004690 clear_tv(rettv);
4691 clear_tv(&var2);
4692 rettv->v_type = VAR_NUMBER;
4693 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 }
4695 }
4696
4697 return OK;
4698}
4699
4700/*
4701 * Handle fourth level expression:
4702 * + number addition
4703 * - number subtraction
4704 * . string concatenation
4705 *
4706 * "arg" must point to the first non-white of the expression.
4707 * "arg" is advanced to the next non-white after the recognized expression.
4708 *
4709 * Return OK or FAIL.
4710 */
4711 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004712eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713{
Bram Moolenaar33570922005-01-25 22:26:29 +00004714 typval_T var2;
4715 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 int op;
4717 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004718#ifdef FEAT_FLOAT
4719 float_T f1 = 0, f2 = 0;
4720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 char_u *s1, *s2;
4722 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4723 char_u *p;
4724
4725 /*
4726 * Get the first variable.
4727 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004728 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 return FAIL;
4730
4731 /*
4732 * Repeat computing, until no '+', '-' or '.' is following.
4733 */
4734 for (;;)
4735 {
4736 op = **arg;
4737 if (op != '+' && op != '-' && op != '.')
4738 break;
4739
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004740 if ((op != '+' || rettv->v_type != VAR_LIST)
4741#ifdef FEAT_FLOAT
4742 && (op == '.' || rettv->v_type != VAR_FLOAT)
4743#endif
4744 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004745 {
4746 /* For "list + ...", an illegal use of the first operand as
4747 * a number cannot be determined before evaluating the 2nd
4748 * operand: if this is also a list, all is ok.
4749 * For "something . ...", "something - ..." or "non-list + ...",
4750 * we know that the first operand needs to be a string or number
4751 * without evaluating the 2nd operand. So check before to avoid
4752 * side effects after an error. */
4753 if (evaluate && get_tv_string_chk(rettv) == NULL)
4754 {
4755 clear_tv(rettv);
4756 return FAIL;
4757 }
4758 }
4759
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 /*
4761 * Get the second variable.
4762 */
4763 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004764 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004766 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 return FAIL;
4768 }
4769
4770 if (evaluate)
4771 {
4772 /*
4773 * Compute the result.
4774 */
4775 if (op == '.')
4776 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004777 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4778 s2 = get_tv_string_buf_chk(&var2, buf2);
4779 if (s2 == NULL) /* type error ? */
4780 {
4781 clear_tv(rettv);
4782 clear_tv(&var2);
4783 return FAIL;
4784 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004785 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004786 clear_tv(rettv);
4787 rettv->v_type = VAR_STRING;
4788 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004790 else if (op == '+' && rettv->v_type == VAR_LIST
4791 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004792 {
4793 /* concatenate Lists */
4794 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4795 &var3) == FAIL)
4796 {
4797 clear_tv(rettv);
4798 clear_tv(&var2);
4799 return FAIL;
4800 }
4801 clear_tv(rettv);
4802 *rettv = var3;
4803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 else
4805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004806 int error = FALSE;
4807
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004808#ifdef FEAT_FLOAT
4809 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004810 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004811 f1 = rettv->vval.v_float;
4812 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004813 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004814 else
4815#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004816 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004817 n1 = get_tv_number_chk(rettv, &error);
4818 if (error)
4819 {
4820 /* This can only happen for "list + non-list". For
4821 * "non-list + ..." or "something - ...", we returned
4822 * before evaluating the 2nd operand. */
4823 clear_tv(rettv);
4824 return FAIL;
4825 }
4826#ifdef FEAT_FLOAT
4827 if (var2.v_type == VAR_FLOAT)
4828 f1 = n1;
4829#endif
4830 }
4831#ifdef FEAT_FLOAT
4832 if (var2.v_type == VAR_FLOAT)
4833 {
4834 f2 = var2.vval.v_float;
4835 n2 = 0;
4836 }
4837 else
4838#endif
4839 {
4840 n2 = get_tv_number_chk(&var2, &error);
4841 if (error)
4842 {
4843 clear_tv(rettv);
4844 clear_tv(&var2);
4845 return FAIL;
4846 }
4847#ifdef FEAT_FLOAT
4848 if (rettv->v_type == VAR_FLOAT)
4849 f2 = n2;
4850#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004851 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004852 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004853
4854#ifdef FEAT_FLOAT
4855 /* If there is a float on either side the result is a float. */
4856 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4857 {
4858 if (op == '+')
4859 f1 = f1 + f2;
4860 else
4861 f1 = f1 - f2;
4862 rettv->v_type = VAR_FLOAT;
4863 rettv->vval.v_float = f1;
4864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004866#endif
4867 {
4868 if (op == '+')
4869 n1 = n1 + n2;
4870 else
4871 n1 = n1 - n2;
4872 rettv->v_type = VAR_NUMBER;
4873 rettv->vval.v_number = n1;
4874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004876 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 }
4878 }
4879 return OK;
4880}
4881
4882/*
4883 * Handle fifth level expression:
4884 * * number multiplication
4885 * / number division
4886 * % number modulo
4887 *
4888 * "arg" must point to the first non-white of the expression.
4889 * "arg" is advanced to the next non-white after the recognized expression.
4890 *
4891 * Return OK or FAIL.
4892 */
4893 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004894eval6(
4895 char_u **arg,
4896 typval_T *rettv,
4897 int evaluate,
4898 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899{
Bram Moolenaar33570922005-01-25 22:26:29 +00004900 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 int op;
4902 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004903#ifdef FEAT_FLOAT
4904 int use_float = FALSE;
4905 float_T f1 = 0, f2;
4906#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004907 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908
4909 /*
4910 * Get the first variable.
4911 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004912 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 return FAIL;
4914
4915 /*
4916 * Repeat computing, until no '*', '/' or '%' is following.
4917 */
4918 for (;;)
4919 {
4920 op = **arg;
4921 if (op != '*' && op != '/' && op != '%')
4922 break;
4923
4924 if (evaluate)
4925 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004926#ifdef FEAT_FLOAT
4927 if (rettv->v_type == VAR_FLOAT)
4928 {
4929 f1 = rettv->vval.v_float;
4930 use_float = TRUE;
4931 n1 = 0;
4932 }
4933 else
4934#endif
4935 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004936 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004937 if (error)
4938 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 }
4940 else
4941 n1 = 0;
4942
4943 /*
4944 * Get the second variable.
4945 */
4946 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004947 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 return FAIL;
4949
4950 if (evaluate)
4951 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004952#ifdef FEAT_FLOAT
4953 if (var2.v_type == VAR_FLOAT)
4954 {
4955 if (!use_float)
4956 {
4957 f1 = n1;
4958 use_float = TRUE;
4959 }
4960 f2 = var2.vval.v_float;
4961 n2 = 0;
4962 }
4963 else
4964#endif
4965 {
4966 n2 = get_tv_number_chk(&var2, &error);
4967 clear_tv(&var2);
4968 if (error)
4969 return FAIL;
4970#ifdef FEAT_FLOAT
4971 if (use_float)
4972 f2 = n2;
4973#endif
4974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975
4976 /*
4977 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004978 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980#ifdef FEAT_FLOAT
4981 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004983 if (op == '*')
4984 f1 = f1 * f2;
4985 else if (op == '/')
4986 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004987# ifdef VMS
4988 /* VMS crashes on divide by zero, work around it */
4989 if (f2 == 0.0)
4990 {
4991 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004992 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004993 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004994 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004995 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004996 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004997 }
4998 else
4999 f1 = f1 / f2;
5000# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005001 /* We rely on the floating point library to handle divide
5002 * by zero to result in "inf" and not a crash. */
5003 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005004# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005007 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005008 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005009 return FAIL;
5010 }
5011 rettv->v_type = VAR_FLOAT;
5012 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 }
5014 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005017 if (op == '*')
5018 n1 = n1 * n2;
5019 else if (op == '/')
5020 {
5021 if (n2 == 0) /* give an error message? */
5022 {
5023 if (n1 == 0)
5024 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5025 else if (n1 < 0)
5026 n1 = -0x7fffffffL;
5027 else
5028 n1 = 0x7fffffffL;
5029 }
5030 else
5031 n1 = n1 / n2;
5032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005034 {
5035 if (n2 == 0) /* give an error message? */
5036 n1 = 0;
5037 else
5038 n1 = n1 % n2;
5039 }
5040 rettv->v_type = VAR_NUMBER;
5041 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 }
5044 }
5045
5046 return OK;
5047}
5048
5049/*
5050 * Handle sixth level expression:
5051 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005052 * "string" string constant
5053 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 * &option-name option value
5055 * @r register contents
5056 * identifier variable value
5057 * function() function call
5058 * $VAR environment variable
5059 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005060 * [expr, expr] List
5061 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 *
5063 * Also handle:
5064 * ! in front logical NOT
5065 * - in front unary minus
5066 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005067 * trailing [] subscript in String or List
5068 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 *
5070 * "arg" must point to the first non-white of the expression.
5071 * "arg" is advanced to the next non-white after the recognized expression.
5072 *
5073 * Return OK or FAIL.
5074 */
5075 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005076eval7(
5077 char_u **arg,
5078 typval_T *rettv,
5079 int evaluate,
5080 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 long n;
5083 int len;
5084 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 char_u *start_leader, *end_leader;
5086 int ret = OK;
5087 char_u *alias;
5088
5089 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005090 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005091 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005093 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094
5095 /*
5096 * Skip '!' and '-' characters. They are handled later.
5097 */
5098 start_leader = *arg;
5099 while (**arg == '!' || **arg == '-' || **arg == '+')
5100 *arg = skipwhite(*arg + 1);
5101 end_leader = *arg;
5102
5103 switch (**arg)
5104 {
5105 /*
5106 * Number constant.
5107 */
5108 case '0':
5109 case '1':
5110 case '2':
5111 case '3':
5112 case '4':
5113 case '5':
5114 case '6':
5115 case '7':
5116 case '8':
5117 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005118 {
5119#ifdef FEAT_FLOAT
5120 char_u *p = skipdigits(*arg + 1);
5121 int get_float = FALSE;
5122
5123 /* We accept a float when the format matches
5124 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005125 * strict to avoid backwards compatibility problems.
5126 * Don't look for a float after the "." operator, so that
5127 * ":let vers = 1.2.3" doesn't fail. */
5128 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005130 get_float = TRUE;
5131 p = skipdigits(p + 2);
5132 if (*p == 'e' || *p == 'E')
5133 {
5134 ++p;
5135 if (*p == '-' || *p == '+')
5136 ++p;
5137 if (!vim_isdigit(*p))
5138 get_float = FALSE;
5139 else
5140 p = skipdigits(p + 1);
5141 }
5142 if (ASCII_ISALPHA(*p) || *p == '.')
5143 get_float = FALSE;
5144 }
5145 if (get_float)
5146 {
5147 float_T f;
5148
5149 *arg += string2float(*arg, &f);
5150 if (evaluate)
5151 {
5152 rettv->v_type = VAR_FLOAT;
5153 rettv->vval.v_float = f;
5154 }
5155 }
5156 else
5157#endif
5158 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005159 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005160 *arg += len;
5161 if (evaluate)
5162 {
5163 rettv->v_type = VAR_NUMBER;
5164 rettv->vval.v_number = n;
5165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 }
5167 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169
5170 /*
5171 * String constant: "string".
5172 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005173 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 break;
5175
5176 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005179 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005180 break;
5181
5182 /*
5183 * List: [expr, expr]
5184 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005185 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 break;
5187
5188 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005189 * Dictionary: {key: val, key: val}
5190 */
5191 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5192 break;
5193
5194 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005195 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005197 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 break;
5199
5200 /*
5201 * Environment variable: $VAR.
5202 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005203 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 break;
5205
5206 /*
5207 * Register contents: @r.
5208 */
5209 case '@': ++*arg;
5210 if (evaluate)
5211 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005212 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005213 rettv->vval.v_string = get_reg_contents(**arg,
5214 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 }
5216 if (**arg != NUL)
5217 ++*arg;
5218 break;
5219
5220 /*
5221 * nested expression: (expression).
5222 */
5223 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005224 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 if (**arg == ')')
5226 ++*arg;
5227 else if (ret == OK)
5228 {
5229 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005230 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 ret = FAIL;
5232 }
5233 break;
5234
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 break;
5237 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238
5239 if (ret == NOTDONE)
5240 {
5241 /*
5242 * Must be a variable or function name.
5243 * Can also be a curly-braces kind of name: {expr}.
5244 */
5245 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005246 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005247 if (alias != NULL)
5248 s = alias;
5249
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005250 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005251 ret = FAIL;
5252 else
5253 {
5254 if (**arg == '(') /* recursive! */
5255 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005256 partial_T *partial;
5257
Bram Moolenaar8c711452005-01-14 21:53:12 +00005258 /* If "s" is the name of a variable of type VAR_FUNC
5259 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005260 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261
5262 /* Invoke the function. */
5263 ret = get_func_tv(s, len, rettv, arg,
5264 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005265 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005266
5267 /* If evaluate is FALSE rettv->v_type was not set in
5268 * get_func_tv, but it's needed in handle_subscript() to parse
5269 * what follows. So set it here. */
5270 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5271 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005272 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005273 rettv->v_type = VAR_FUNC;
5274 }
5275
Bram Moolenaar8c711452005-01-14 21:53:12 +00005276 /* Stop the expression evaluation when immediately
5277 * aborting on error, or when an interrupt occurred or
5278 * an exception was thrown but not caught. */
5279 if (aborting())
5280 {
5281 if (ret == OK)
5282 clear_tv(rettv);
5283 ret = FAIL;
5284 }
5285 }
5286 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005287 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005288 else
5289 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005291 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005292 }
5293
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 *arg = skipwhite(*arg);
5295
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005296 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5297 * expr(expr). */
5298 if (ret == OK)
5299 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300
5301 /*
5302 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5303 */
5304 if (ret == OK && evaluate && end_leader > start_leader)
5305 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005306 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005307 int val = 0;
5308#ifdef FEAT_FLOAT
5309 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005310
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005311 if (rettv->v_type == VAR_FLOAT)
5312 f = rettv->vval.v_float;
5313 else
5314#endif
5315 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005316 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005318 clear_tv(rettv);
5319 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005321 else
5322 {
5323 while (end_leader > start_leader)
5324 {
5325 --end_leader;
5326 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005327 {
5328#ifdef FEAT_FLOAT
5329 if (rettv->v_type == VAR_FLOAT)
5330 f = !f;
5331 else
5332#endif
5333 val = !val;
5334 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005335 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005336 {
5337#ifdef FEAT_FLOAT
5338 if (rettv->v_type == VAR_FLOAT)
5339 f = -f;
5340 else
5341#endif
5342 val = -val;
5343 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005344 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005345#ifdef FEAT_FLOAT
5346 if (rettv->v_type == VAR_FLOAT)
5347 {
5348 clear_tv(rettv);
5349 rettv->vval.v_float = f;
5350 }
5351 else
5352#endif
5353 {
5354 clear_tv(rettv);
5355 rettv->v_type = VAR_NUMBER;
5356 rettv->vval.v_number = val;
5357 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 }
5360
5361 return ret;
5362}
5363
5364/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005365 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5366 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5368 */
5369 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005370eval_index(
5371 char_u **arg,
5372 typval_T *rettv,
5373 int evaluate,
5374 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375{
5376 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005377 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005379 long len = -1;
5380 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005381 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005382 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383
Bram Moolenaara03f2332016-02-06 18:09:59 +01005384 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005386 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005387 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005388 if (verbose)
5389 EMSG(_("E695: Cannot index a Funcref"));
5390 return FAIL;
5391 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005392#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005393 if (verbose)
5394 EMSG(_(e_float_as_string));
5395 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005396#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005397 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005398 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005399 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005400 if (verbose)
5401 EMSG(_("E909: Cannot index a special variable"));
5402 return FAIL;
5403 case VAR_UNKNOWN:
5404 if (evaluate)
5405 return FAIL;
5406 /* FALLTHROUGH */
5407
5408 case VAR_STRING:
5409 case VAR_NUMBER:
5410 case VAR_LIST:
5411 case VAR_DICT:
5412 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005413 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005415 init_tv(&var1);
5416 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005417 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005418 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005419 /*
5420 * dict.name
5421 */
5422 key = *arg + 1;
5423 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5424 ;
5425 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005427 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428 }
5429 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005431 /*
5432 * something[idx]
5433 *
5434 * Get the (first) variable from inside the [].
5435 */
5436 *arg = skipwhite(*arg + 1);
5437 if (**arg == ':')
5438 empty1 = TRUE;
5439 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5440 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005441 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5442 {
5443 /* not a number or string */
5444 clear_tv(&var1);
5445 return FAIL;
5446 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005447
5448 /*
5449 * Get the second variable from inside the [:].
5450 */
5451 if (**arg == ':')
5452 {
5453 range = TRUE;
5454 *arg = skipwhite(*arg + 1);
5455 if (**arg == ']')
5456 empty2 = TRUE;
5457 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5458 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005459 if (!empty1)
5460 clear_tv(&var1);
5461 return FAIL;
5462 }
5463 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5464 {
5465 /* not a number or string */
5466 if (!empty1)
5467 clear_tv(&var1);
5468 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005469 return FAIL;
5470 }
5471 }
5472
5473 /* Check for the ']'. */
5474 if (**arg != ']')
5475 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005476 if (verbose)
5477 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005478 clear_tv(&var1);
5479 if (range)
5480 clear_tv(&var2);
5481 return FAIL;
5482 }
5483 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005484 }
5485
5486 if (evaluate)
5487 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005488 n1 = 0;
5489 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005491 n1 = get_tv_number(&var1);
5492 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493 }
5494 if (range)
5495 {
5496 if (empty2)
5497 n2 = -1;
5498 else
5499 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005500 n2 = get_tv_number(&var2);
5501 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005502 }
5503 }
5504
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005505 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005506 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005507 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005508 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005509 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005510 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005511 case VAR_SPECIAL:
5512 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005513 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005514 break; /* not evaluating, skipping over subscript */
5515
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005516 case VAR_NUMBER:
5517 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005518 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519 len = (long)STRLEN(s);
5520 if (range)
5521 {
5522 /* The resulting variable is a substring. If the indexes
5523 * are out of range the result is empty. */
5524 if (n1 < 0)
5525 {
5526 n1 = len + n1;
5527 if (n1 < 0)
5528 n1 = 0;
5529 }
5530 if (n2 < 0)
5531 n2 = len + n2;
5532 else if (n2 >= len)
5533 n2 = len;
5534 if (n1 >= len || n2 < 0 || n1 > n2)
5535 s = NULL;
5536 else
5537 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5538 }
5539 else
5540 {
5541 /* The resulting variable is a string of a single
5542 * character. If the index is too big or negative the
5543 * result is empty. */
5544 if (n1 >= len || n1 < 0)
5545 s = NULL;
5546 else
5547 s = vim_strnsave(s + n1, 1);
5548 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005549 clear_tv(rettv);
5550 rettv->v_type = VAR_STRING;
5551 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552 break;
5553
5554 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005555 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005556 if (n1 < 0)
5557 n1 = len + n1;
5558 if (!empty1 && (n1 < 0 || n1 >= len))
5559 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005560 /* For a range we allow invalid values and return an empty
5561 * list. A list index out of range is an error. */
5562 if (!range)
5563 {
5564 if (verbose)
5565 EMSGN(_(e_listidx), n1);
5566 return FAIL;
5567 }
5568 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005569 }
5570 if (range)
5571 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005572 list_T *l;
5573 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005574
5575 if (n2 < 0)
5576 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005577 else if (n2 >= len)
5578 n2 = len - 1;
5579 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005580 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005581 l = list_alloc();
5582 if (l == NULL)
5583 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005584 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005585 n1 <= n2; ++n1)
5586 {
5587 if (list_append_tv(l, &item->li_tv) == FAIL)
5588 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005589 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005590 return FAIL;
5591 }
5592 item = item->li_next;
5593 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005594 clear_tv(rettv);
5595 rettv->v_type = VAR_LIST;
5596 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005597 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005598 }
5599 else
5600 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005601 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005602 clear_tv(rettv);
5603 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005604 }
5605 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005606
5607 case VAR_DICT:
5608 if (range)
5609 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005610 if (verbose)
5611 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005612 if (len == -1)
5613 clear_tv(&var1);
5614 return FAIL;
5615 }
5616 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005617 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005618
5619 if (len == -1)
5620 {
5621 key = get_tv_string(&var1);
5622 if (*key == NUL)
5623 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005624 if (verbose)
5625 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005626 clear_tv(&var1);
5627 return FAIL;
5628 }
5629 }
5630
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005631 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005632
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005633 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005634 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005635 if (len == -1)
5636 clear_tv(&var1);
5637 if (item == NULL)
5638 return FAIL;
5639
5640 copy_tv(&item->di_tv, &var1);
5641 clear_tv(rettv);
5642 *rettv = var1;
5643 }
5644 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005645 }
5646 }
5647
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005648 return OK;
5649}
5650
5651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 * Get an option value.
5653 * "arg" points to the '&' or '+' before the option name.
5654 * "arg" is advanced to character after the option name.
5655 * Return OK or FAIL.
5656 */
5657 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005658get_option_tv(
5659 char_u **arg,
5660 typval_T *rettv, /* when NULL, only check if option exists */
5661 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662{
5663 char_u *option_end;
5664 long numval;
5665 char_u *stringval;
5666 int opt_type;
5667 int c;
5668 int working = (**arg == '+'); /* has("+option") */
5669 int ret = OK;
5670 int opt_flags;
5671
5672 /*
5673 * Isolate the option name and find its value.
5674 */
5675 option_end = find_option_end(arg, &opt_flags);
5676 if (option_end == NULL)
5677 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005678 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 EMSG2(_("E112: Option name missing: %s"), *arg);
5680 return FAIL;
5681 }
5682
5683 if (!evaluate)
5684 {
5685 *arg = option_end;
5686 return OK;
5687 }
5688
5689 c = *option_end;
5690 *option_end = NUL;
5691 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005692 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693
5694 if (opt_type == -3) /* invalid name */
5695 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005696 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 EMSG2(_("E113: Unknown option: %s"), *arg);
5698 ret = FAIL;
5699 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005700 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 {
5702 if (opt_type == -2) /* hidden string option */
5703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005704 rettv->v_type = VAR_STRING;
5705 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 }
5707 else if (opt_type == -1) /* hidden number option */
5708 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005709 rettv->v_type = VAR_NUMBER;
5710 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 }
5712 else if (opt_type == 1) /* number option */
5713 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005714 rettv->v_type = VAR_NUMBER;
5715 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 }
5717 else /* string option */
5718 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005719 rettv->v_type = VAR_STRING;
5720 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 }
5722 }
5723 else if (working && (opt_type == -2 || opt_type == -1))
5724 ret = FAIL;
5725
5726 *option_end = c; /* put back for error messages */
5727 *arg = option_end;
5728
5729 return ret;
5730}
5731
5732/*
5733 * Allocate a variable for a string constant.
5734 * Return OK or FAIL.
5735 */
5736 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005737get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738{
5739 char_u *p;
5740 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 int extra = 0;
5742
5743 /*
5744 * Find the end of the string, skipping backslashed characters.
5745 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005746 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 {
5748 if (*p == '\\' && p[1] != NUL)
5749 {
5750 ++p;
5751 /* A "\<x>" form occupies at least 4 characters, and produces up
5752 * to 6 characters: reserve space for 2 extra */
5753 if (*p == '<')
5754 extra += 2;
5755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 }
5757
5758 if (*p != '"')
5759 {
5760 EMSG2(_("E114: Missing quote: %s"), *arg);
5761 return FAIL;
5762 }
5763
5764 /* If only parsing, set *arg and return here */
5765 if (!evaluate)
5766 {
5767 *arg = p + 1;
5768 return OK;
5769 }
5770
5771 /*
5772 * Copy the string into allocated memory, handling backslashed
5773 * characters.
5774 */
5775 name = alloc((unsigned)(p - *arg + extra));
5776 if (name == NULL)
5777 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005778 rettv->v_type = VAR_STRING;
5779 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 {
5783 if (*p == '\\')
5784 {
5785 switch (*++p)
5786 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 case 'b': *name++ = BS; ++p; break;
5788 case 'e': *name++ = ESC; ++p; break;
5789 case 'f': *name++ = FF; ++p; break;
5790 case 'n': *name++ = NL; ++p; break;
5791 case 'r': *name++ = CAR; ++p; break;
5792 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793
5794 case 'X': /* hex: "\x1", "\x12" */
5795 case 'x':
5796 case 'u': /* Unicode: "\u0023" */
5797 case 'U':
5798 if (vim_isxdigit(p[1]))
5799 {
5800 int n, nr;
5801 int c = toupper(*p);
5802
5803 if (c == 'X')
5804 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005805 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005807 else
5808 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 nr = 0;
5810 while (--n >= 0 && vim_isxdigit(p[1]))
5811 {
5812 ++p;
5813 nr = (nr << 4) + hex2nr(*p);
5814 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005815 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816#ifdef FEAT_MBYTE
5817 /* For "\u" store the number according to
5818 * 'encoding'. */
5819 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 else
5822#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005823 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 break;
5826
5827 /* octal: "\1", "\12", "\123" */
5828 case '0':
5829 case '1':
5830 case '2':
5831 case '3':
5832 case '4':
5833 case '5':
5834 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005835 case '7': *name = *p++ - '0';
5836 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 *name = (*name << 3) + *p++ - '0';
5839 if (*p >= '0' && *p <= '7')
5840 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 break;
5844
5845 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 if (extra != 0)
5848 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 break;
5851 }
5852 /* FALLTHROUGH */
5853
Bram Moolenaar8c711452005-01-14 21:53:12 +00005854 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 break;
5856 }
5857 }
5858 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 *arg = p + 1;
5864
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 return OK;
5866}
5867
5868/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 * Return OK or FAIL.
5871 */
5872 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005873get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874{
5875 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005876 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005877 int reduce = 0;
5878
5879 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881 */
5882 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5883 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005884 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005885 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887 break;
5888 ++reduce;
5889 ++p;
5890 }
5891 }
5892
Bram Moolenaar8c711452005-01-14 21:53:12 +00005893 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005895 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005896 return FAIL;
5897 }
5898
Bram Moolenaar8c711452005-01-14 21:53:12 +00005899 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005900 if (!evaluate)
5901 {
5902 *arg = p + 1;
5903 return OK;
5904 }
5905
5906 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005907 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005908 */
5909 str = alloc((unsigned)((p - *arg) - reduce));
5910 if (str == NULL)
5911 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005912 rettv->v_type = VAR_STRING;
5913 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005914
Bram Moolenaar8c711452005-01-14 21:53:12 +00005915 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005916 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005917 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005918 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005919 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005920 break;
5921 ++p;
5922 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005923 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005924 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005925 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005926 *arg = p + 1;
5927
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005928 return OK;
5929}
5930
5931/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005932 * Allocate a variable for a List and fill it from "*arg".
5933 * Return OK or FAIL.
5934 */
5935 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005936get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937{
Bram Moolenaar33570922005-01-25 22:26:29 +00005938 list_T *l = NULL;
5939 typval_T tv;
5940 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941
5942 if (evaluate)
5943 {
5944 l = list_alloc();
5945 if (l == NULL)
5946 return FAIL;
5947 }
5948
5949 *arg = skipwhite(*arg + 1);
5950 while (**arg != ']' && **arg != NUL)
5951 {
5952 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5953 goto failret;
5954 if (evaluate)
5955 {
5956 item = listitem_alloc();
5957 if (item != NULL)
5958 {
5959 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005960 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005961 list_append(l, item);
5962 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005963 else
5964 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 }
5966
5967 if (**arg == ']')
5968 break;
5969 if (**arg != ',')
5970 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005971 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972 goto failret;
5973 }
5974 *arg = skipwhite(*arg + 1);
5975 }
5976
5977 if (**arg != ']')
5978 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005979 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980failret:
5981 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005982 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 return FAIL;
5984 }
5985
5986 *arg = skipwhite(*arg + 1);
5987 if (evaluate)
5988 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005989 rettv->v_type = VAR_LIST;
5990 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991 ++l->lv_refcount;
5992 }
5993
5994 return OK;
5995}
5996
5997/*
5998 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005999 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006001 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006002list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006003{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006004 list_T *l;
6005
6006 l = (list_T *)alloc_clear(sizeof(list_T));
6007 if (l != NULL)
6008 {
6009 /* Prepend the list to the list of lists for garbage collection. */
6010 if (first_list != NULL)
6011 first_list->lv_used_prev = l;
6012 l->lv_used_prev = NULL;
6013 l->lv_used_next = first_list;
6014 first_list = l;
6015 }
6016 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017}
6018
6019/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006020 * Allocate an empty list for a return value.
6021 * Returns OK or FAIL.
6022 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006023 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006024rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006025{
6026 list_T *l = list_alloc();
6027
6028 if (l == NULL)
6029 return FAIL;
6030
6031 rettv->vval.v_list = l;
6032 rettv->v_type = VAR_LIST;
6033 ++l->lv_refcount;
6034 return OK;
6035}
6036
6037/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038 * Unreference a list: decrement the reference count and free it when it
6039 * becomes zero.
6040 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006041 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006042list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006043{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006044 if (l != NULL && --l->lv_refcount <= 0)
6045 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046}
6047
6048/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006049 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050 * Ignores the reference count.
6051 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006052 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006053list_free(
6054 list_T *l,
6055 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056{
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006058
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006059 /* Remove the list from the list of lists for garbage collection. */
6060 if (l->lv_used_prev == NULL)
6061 first_list = l->lv_used_next;
6062 else
6063 l->lv_used_prev->lv_used_next = l->lv_used_next;
6064 if (l->lv_used_next != NULL)
6065 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6066
Bram Moolenaard9fba312005-06-26 22:34:35 +00006067 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006068 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006069 /* Remove the item before deleting it. */
6070 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006071 if (recurse || (item->li_tv.v_type != VAR_LIST
6072 && item->li_tv.v_type != VAR_DICT))
6073 clear_tv(&item->li_tv);
6074 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006075 }
6076 vim_free(l);
6077}
6078
6079/*
6080 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006081 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006082 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006083 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006084listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085{
Bram Moolenaar33570922005-01-25 22:26:29 +00006086 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006087}
6088
6089/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006090 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006091 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006092 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006093listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006094{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006095 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006096 vim_free(item);
6097}
6098
6099/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006100 * Remove a list item from a List and free it. Also clears the value.
6101 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006102 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006103listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006104{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006105 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006106 listitem_free(item);
6107}
6108
6109/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006110 * Get the number of items in a list.
6111 */
6112 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006113list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006114{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006115 if (l == NULL)
6116 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006117 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006118}
6119
6120/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006121 * Return TRUE when two lists have exactly the same values.
6122 */
6123 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006124list_equal(
6125 list_T *l1,
6126 list_T *l2,
6127 int ic, /* ignore case for strings */
6128 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006129{
Bram Moolenaar33570922005-01-25 22:26:29 +00006130 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006131
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006132 if (l1 == NULL || l2 == NULL)
6133 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006134 if (l1 == l2)
6135 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006136 if (list_len(l1) != list_len(l2))
6137 return FALSE;
6138
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006139 for (item1 = l1->lv_first, item2 = l2->lv_first;
6140 item1 != NULL && item2 != NULL;
6141 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006142 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006143 return FALSE;
6144 return item1 == NULL && item2 == NULL;
6145}
6146
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006147/*
6148 * Return the dictitem that an entry in a hashtable points to.
6149 */
6150 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006151dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006152{
6153 return HI2DI(hi);
6154}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006155
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006156/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006157 * Return TRUE when two dictionaries have exactly the same key/values.
6158 */
6159 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006160dict_equal(
6161 dict_T *d1,
6162 dict_T *d2,
6163 int ic, /* ignore case for strings */
6164 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006165{
Bram Moolenaar33570922005-01-25 22:26:29 +00006166 hashitem_T *hi;
6167 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006168 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006169
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006170 if (d1 == NULL || d2 == NULL)
6171 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006172 if (d1 == d2)
6173 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006174 if (dict_len(d1) != dict_len(d2))
6175 return FALSE;
6176
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006177 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006178 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006179 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006180 if (!HASHITEM_EMPTY(hi))
6181 {
6182 item2 = dict_find(d2, hi->hi_key, -1);
6183 if (item2 == NULL)
6184 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006185 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006186 return FALSE;
6187 --todo;
6188 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006189 }
6190 return TRUE;
6191}
6192
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006193static int tv_equal_recurse_limit;
6194
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006195/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006196 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006197 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006198 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006199 */
6200 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006201tv_equal(
6202 typval_T *tv1,
6203 typval_T *tv2,
6204 int ic, /* ignore case */
6205 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006206{
6207 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006208 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006209 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006210 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006211
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006212 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006213 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006214
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006215 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006216 * recursiveness to a limit. We guess they are equal then.
6217 * A fixed limit has the problem of still taking an awful long time.
6218 * Reduce the limit every time running into it. That should work fine for
6219 * deeply linked structures that are not recursively linked and catch
6220 * recursiveness quickly. */
6221 if (!recursive)
6222 tv_equal_recurse_limit = 1000;
6223 if (recursive_cnt >= tv_equal_recurse_limit)
6224 {
6225 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006226 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006227 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006228
6229 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006230 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006231 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006232 ++recursive_cnt;
6233 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6234 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006235 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006236
6237 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006238 ++recursive_cnt;
6239 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6240 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006241 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006242
6243 case VAR_FUNC:
6244 return (tv1->vval.v_string != NULL
6245 && tv2->vval.v_string != NULL
6246 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6247
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006248 case VAR_PARTIAL:
6249 return tv1->vval.v_partial != NULL
6250 && tv1->vval.v_partial == tv2->vval.v_partial;
6251
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006252 case VAR_NUMBER:
6253 return tv1->vval.v_number == tv2->vval.v_number;
6254
6255 case VAR_STRING:
6256 s1 = get_tv_string_buf(tv1, buf1);
6257 s2 = get_tv_string_buf(tv2, buf2);
6258 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006259
6260 case VAR_SPECIAL:
6261 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006262
6263 case VAR_FLOAT:
6264#ifdef FEAT_FLOAT
6265 return tv1->vval.v_float == tv2->vval.v_float;
6266#endif
6267 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006268#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006269 return tv1->vval.v_job == tv2->vval.v_job;
6270#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006271 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006272#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006273 return tv1->vval.v_channel == tv2->vval.v_channel;
6274#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006275 case VAR_UNKNOWN:
6276 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006277 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006278
Bram Moolenaara03f2332016-02-06 18:09:59 +01006279 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6280 * does not equal anything, not even itself. */
6281 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006282}
6283
6284/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006285 * Locate item with index "n" in list "l" and return it.
6286 * A negative index is counted from the end; -1 is the last item.
6287 * Returns NULL when "n" is out of range.
6288 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006289 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006290list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006291{
Bram Moolenaar33570922005-01-25 22:26:29 +00006292 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006293 long idx;
6294
6295 if (l == NULL)
6296 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006297
6298 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006299 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006300 n = l->lv_len + n;
6301
6302 /* Check for index out of range. */
6303 if (n < 0 || n >= l->lv_len)
6304 return NULL;
6305
6306 /* When there is a cached index may start search from there. */
6307 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006308 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006309 if (n < l->lv_idx / 2)
6310 {
6311 /* closest to the start of the list */
6312 item = l->lv_first;
6313 idx = 0;
6314 }
6315 else if (n > (l->lv_idx + l->lv_len) / 2)
6316 {
6317 /* closest to the end of the list */
6318 item = l->lv_last;
6319 idx = l->lv_len - 1;
6320 }
6321 else
6322 {
6323 /* closest to the cached index */
6324 item = l->lv_idx_item;
6325 idx = l->lv_idx;
6326 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006327 }
6328 else
6329 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006330 if (n < l->lv_len / 2)
6331 {
6332 /* closest to the start of the list */
6333 item = l->lv_first;
6334 idx = 0;
6335 }
6336 else
6337 {
6338 /* closest to the end of the list */
6339 item = l->lv_last;
6340 idx = l->lv_len - 1;
6341 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006342 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006343
6344 while (n > idx)
6345 {
6346 /* search forward */
6347 item = item->li_next;
6348 ++idx;
6349 }
6350 while (n < idx)
6351 {
6352 /* search backward */
6353 item = item->li_prev;
6354 --idx;
6355 }
6356
6357 /* cache the used index */
6358 l->lv_idx = idx;
6359 l->lv_idx_item = item;
6360
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006361 return item;
6362}
6363
6364/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006365 * Get list item "l[idx]" as a number.
6366 */
6367 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006368list_find_nr(
6369 list_T *l,
6370 long idx,
6371 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006372{
6373 listitem_T *li;
6374
6375 li = list_find(l, idx);
6376 if (li == NULL)
6377 {
6378 if (errorp != NULL)
6379 *errorp = TRUE;
6380 return -1L;
6381 }
6382 return get_tv_number_chk(&li->li_tv, errorp);
6383}
6384
6385/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006386 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6387 */
6388 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006389list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006390{
6391 listitem_T *li;
6392
6393 li = list_find(l, idx - 1);
6394 if (li == NULL)
6395 {
6396 EMSGN(_(e_listidx), idx);
6397 return NULL;
6398 }
6399 return get_tv_string(&li->li_tv);
6400}
6401
6402/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006403 * Locate "item" list "l" and return its index.
6404 * Returns -1 when "item" is not in the list.
6405 */
6406 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006407list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006408{
6409 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006410 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006411
6412 if (l == NULL)
6413 return -1;
6414 idx = 0;
6415 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6416 ++idx;
6417 if (li == NULL)
6418 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006419 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006420}
6421
6422/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423 * Append item "item" to the end of list "l".
6424 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006425 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006426list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006427{
6428 if (l->lv_last == NULL)
6429 {
6430 /* empty list */
6431 l->lv_first = item;
6432 l->lv_last = item;
6433 item->li_prev = NULL;
6434 }
6435 else
6436 {
6437 l->lv_last->li_next = item;
6438 item->li_prev = l->lv_last;
6439 l->lv_last = item;
6440 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006441 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006442 item->li_next = NULL;
6443}
6444
6445/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006446 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006447 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006448 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006449 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006450list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006451{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006452 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006453
Bram Moolenaar05159a02005-02-26 23:04:13 +00006454 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006455 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006456 copy_tv(tv, &li->li_tv);
6457 list_append(l, li);
6458 return OK;
6459}
6460
6461/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006462 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006463 * Return FAIL when out of memory.
6464 */
6465 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006466list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006467{
6468 listitem_T *li = listitem_alloc();
6469
6470 if (li == NULL)
6471 return FAIL;
6472 li->li_tv.v_type = VAR_DICT;
6473 li->li_tv.v_lock = 0;
6474 li->li_tv.vval.v_dict = dict;
6475 list_append(list, li);
6476 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006477 return OK;
6478}
6479
6480/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006481 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006482 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006483 * Returns FAIL when out of memory.
6484 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006485 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006486list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006487{
6488 listitem_T *li = listitem_alloc();
6489
6490 if (li == NULL)
6491 return FAIL;
6492 list_append(l, li);
6493 li->li_tv.v_type = VAR_STRING;
6494 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006495 if (str == NULL)
6496 li->li_tv.vval.v_string = NULL;
6497 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006498 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006499 return FAIL;
6500 return OK;
6501}
6502
6503/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006504 * Append "n" to list "l".
6505 * Returns FAIL when out of memory.
6506 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006507 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006508list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006509{
6510 listitem_T *li;
6511
6512 li = listitem_alloc();
6513 if (li == NULL)
6514 return FAIL;
6515 li->li_tv.v_type = VAR_NUMBER;
6516 li->li_tv.v_lock = 0;
6517 li->li_tv.vval.v_number = n;
6518 list_append(l, li);
6519 return OK;
6520}
6521
6522/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006523 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006524 * If "item" is NULL append at the end.
6525 * Return FAIL when out of memory.
6526 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006527 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006528list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006529{
Bram Moolenaar33570922005-01-25 22:26:29 +00006530 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006531
6532 if (ni == NULL)
6533 return FAIL;
6534 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006535 list_insert(l, ni, item);
6536 return OK;
6537}
6538
6539 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006540list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006541{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542 if (item == NULL)
6543 /* Append new item at end of list. */
6544 list_append(l, ni);
6545 else
6546 {
6547 /* Insert new item before existing item. */
6548 ni->li_prev = item->li_prev;
6549 ni->li_next = item;
6550 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006551 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006552 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006553 ++l->lv_idx;
6554 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006556 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006557 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006558 l->lv_idx_item = NULL;
6559 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006560 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006561 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563}
6564
6565/*
6566 * Extend "l1" with "l2".
6567 * If "bef" is NULL append at the end, otherwise insert before this item.
6568 * Returns FAIL when out of memory.
6569 */
6570 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006571list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006572{
Bram Moolenaar33570922005-01-25 22:26:29 +00006573 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006574 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006575
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006576 /* We also quit the loop when we have inserted the original item count of
6577 * the list, avoid a hang when we extend a list with itself. */
6578 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006579 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6580 return FAIL;
6581 return OK;
6582}
6583
6584/*
6585 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6586 * Return FAIL when out of memory.
6587 */
6588 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006589list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006590{
Bram Moolenaar33570922005-01-25 22:26:29 +00006591 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006592
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006593 if (l1 == NULL || l2 == NULL)
6594 return FAIL;
6595
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006596 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006597 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006598 if (l == NULL)
6599 return FAIL;
6600 tv->v_type = VAR_LIST;
6601 tv->vval.v_list = l;
6602
6603 /* append all items from the second list */
6604 return list_extend(l, l2, NULL);
6605}
6606
6607/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006608 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006609 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006610 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006611 * Returns NULL when out of memory.
6612 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006613 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006614list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006615{
Bram Moolenaar33570922005-01-25 22:26:29 +00006616 list_T *copy;
6617 listitem_T *item;
6618 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619
6620 if (orig == NULL)
6621 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006622
6623 copy = list_alloc();
6624 if (copy != NULL)
6625 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006626 if (copyID != 0)
6627 {
6628 /* Do this before adding the items, because one of the items may
6629 * refer back to this list. */
6630 orig->lv_copyID = copyID;
6631 orig->lv_copylist = copy;
6632 }
6633 for (item = orig->lv_first; item != NULL && !got_int;
6634 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006635 {
6636 ni = listitem_alloc();
6637 if (ni == NULL)
6638 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006639 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006640 {
6641 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6642 {
6643 vim_free(ni);
6644 break;
6645 }
6646 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006647 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006648 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006649 list_append(copy, ni);
6650 }
6651 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006652 if (item != NULL)
6653 {
6654 list_unref(copy);
6655 copy = NULL;
6656 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006657 }
6658
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006659 return copy;
6660}
6661
6662/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006663 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006664 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006665 * This used to be called list_remove, but that conflicts with a Sun header
6666 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006668 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006669vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006670{
Bram Moolenaar33570922005-01-25 22:26:29 +00006671 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006672
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006673 /* notify watchers */
6674 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006676 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006677 list_fix_watch(l, ip);
6678 if (ip == item2)
6679 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006680 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006681
6682 if (item2->li_next == NULL)
6683 l->lv_last = item->li_prev;
6684 else
6685 item2->li_next->li_prev = item->li_prev;
6686 if (item->li_prev == NULL)
6687 l->lv_first = item2->li_next;
6688 else
6689 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006690 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006691}
6692
6693/*
6694 * Return an allocated string with the string representation of a list.
6695 * May return NULL.
6696 */
6697 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006698list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006699{
6700 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006701
6702 if (tv->vval.v_list == NULL)
6703 return NULL;
6704 ga_init2(&ga, (int)sizeof(char), 80);
6705 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006706 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006707 {
6708 vim_free(ga.ga_data);
6709 return NULL;
6710 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006711 ga_append(&ga, ']');
6712 ga_append(&ga, NUL);
6713 return (char_u *)ga.ga_data;
6714}
6715
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006716typedef struct join_S {
6717 char_u *s;
6718 char_u *tofree;
6719} join_T;
6720
6721 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006722list_join_inner(
6723 garray_T *gap, /* to store the result in */
6724 list_T *l,
6725 char_u *sep,
6726 int echo_style,
6727 int copyID,
6728 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006729{
6730 int i;
6731 join_T *p;
6732 int len;
6733 int sumlen = 0;
6734 int first = TRUE;
6735 char_u *tofree;
6736 char_u numbuf[NUMBUFLEN];
6737 listitem_T *item;
6738 char_u *s;
6739
6740 /* Stringify each item in the list. */
6741 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6742 {
6743 if (echo_style)
6744 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6745 else
6746 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6747 if (s == NULL)
6748 return FAIL;
6749
6750 len = (int)STRLEN(s);
6751 sumlen += len;
6752
Bram Moolenaarcde88542015-08-11 19:14:00 +02006753 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006754 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6755 if (tofree != NULL || s != numbuf)
6756 {
6757 p->s = s;
6758 p->tofree = tofree;
6759 }
6760 else
6761 {
6762 p->s = vim_strnsave(s, len);
6763 p->tofree = p->s;
6764 }
6765
6766 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006767 if (did_echo_string_emsg) /* recursion error, bail out */
6768 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006769 }
6770
6771 /* Allocate result buffer with its total size, avoid re-allocation and
6772 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6773 if (join_gap->ga_len >= 2)
6774 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6775 if (ga_grow(gap, sumlen + 2) == FAIL)
6776 return FAIL;
6777
6778 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6779 {
6780 if (first)
6781 first = FALSE;
6782 else
6783 ga_concat(gap, sep);
6784 p = ((join_T *)join_gap->ga_data) + i;
6785
6786 if (p->s != NULL)
6787 ga_concat(gap, p->s);
6788 line_breakcheck();
6789 }
6790
6791 return OK;
6792}
6793
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006794/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006795 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006796 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006797 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006798 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006799 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006800list_join(
6801 garray_T *gap,
6802 list_T *l,
6803 char_u *sep,
6804 int echo_style,
6805 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006806{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006807 garray_T join_ga;
6808 int retval;
6809 join_T *p;
6810 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006811
Bram Moolenaard39a7512015-04-16 22:51:22 +02006812 if (l->lv_len < 1)
6813 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006814 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6815 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6816
6817 /* Dispose each item in join_ga. */
6818 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006819 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006820 p = (join_T *)join_ga.ga_data;
6821 for (i = 0; i < join_ga.ga_len; ++i)
6822 {
6823 vim_free(p->tofree);
6824 ++p;
6825 }
6826 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006827 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006828
6829 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006830}
6831
6832/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006833 * Return the next (unique) copy ID.
6834 * Used for serializing nested structures.
6835 */
6836 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006837get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006838{
6839 current_copyID += COPYID_INC;
6840 return current_copyID;
6841}
6842
6843/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006844 * Garbage collection for lists and dictionaries.
6845 *
6846 * We use reference counts to be able to free most items right away when they
6847 * are no longer used. But for composite items it's possible that it becomes
6848 * unused while the reference count is > 0: When there is a recursive
6849 * reference. Example:
6850 * :let l = [1, 2, 3]
6851 * :let d = {9: l}
6852 * :let l[1] = d
6853 *
6854 * Since this is quite unusual we handle this with garbage collection: every
6855 * once in a while find out which lists and dicts are not referenced from any
6856 * variable.
6857 *
6858 * Here is a good reference text about garbage collection (refers to Python
6859 * but it applies to all reference-counting mechanisms):
6860 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006861 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006862
6863/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006864 * Do garbage collection for lists and dicts.
6865 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006866 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006867 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006868garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006869{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006870 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006871 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006872 buf_T *buf;
6873 win_T *wp;
6874 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006875 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006876 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006877 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006878#ifdef FEAT_WINDOWS
6879 tabpage_T *tp;
6880#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006881
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006882 /* Only do this once. */
6883 want_garbage_collect = FALSE;
6884 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006885 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006886
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006887 /* We advance by two because we add one for items referenced through
6888 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006889 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006890
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006891 /*
6892 * 1. Go through all accessible variables and mark all lists and dicts
6893 * with copyID.
6894 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006895
6896 /* Don't free variables in the previous_funccal list unless they are only
6897 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006898 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006899 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6900 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006901 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6902 NULL);
6903 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6904 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006905 }
6906
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006907 /* script-local variables */
6908 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006909 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006910
6911 /* buffer-local variables */
6912 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006913 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6914 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006915
6916 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006917 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006918 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6919 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006920#ifdef FEAT_AUTOCMD
6921 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006922 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6923 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006924#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006925
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006926#ifdef FEAT_WINDOWS
6927 /* tabpage-local variables */
6928 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006929 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6930 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006931#endif
6932
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006933 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006934 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006935
6936 /* function-local variables */
6937 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6938 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006939 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6940 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006941 }
6942
Bram Moolenaard812df62008-11-09 12:46:09 +00006943 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006944 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006945
Bram Moolenaar1dced572012-04-05 16:54:08 +02006946#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006947 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006948#endif
6949
Bram Moolenaardb913952012-06-29 12:54:53 +02006950#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006951 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006952#endif
6953
6954#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006955 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006956#endif
6957
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006958#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006959 abort = abort || set_ref_in_channel(copyID);
6960#endif
6961
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006962 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006963 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006964 /*
6965 * 2. Free lists and dictionaries that are not referenced.
6966 */
6967 did_free = free_unref_items(copyID);
6968
6969 /*
6970 * 3. Check if any funccal can be freed now.
6971 */
6972 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006973 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006974 if (can_free_funccal(*pfc, copyID))
6975 {
6976 fc = *pfc;
6977 *pfc = fc->caller;
6978 free_funccal(fc, TRUE);
6979 did_free = TRUE;
6980 did_free_funccal = TRUE;
6981 }
6982 else
6983 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006984 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006985 if (did_free_funccal)
6986 /* When a funccal was freed some more items might be garbage
6987 * collected, so run again. */
6988 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006989 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006990 else if (p_verbose > 0)
6991 {
6992 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6993 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006994
6995 return did_free;
6996}
6997
6998/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006999 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007000 */
7001 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007002free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007003{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007004 dict_T *dd, *dd_next;
7005 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007006 int did_free = FALSE;
7007
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007009 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007010 */
7011 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007012 {
7013 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007014 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007016 /* Free the Dictionary and ordinary items it contains, but don't
7017 * recurse into Lists and Dictionaries, they will be in the list
7018 * of dicts or list of lists. */
7019 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007020 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007022 dd = dd_next;
7023 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024
7025 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007026 * Go through the list of lists and free items without the copyID.
7027 * But don't free a list that has a watcher (used in a for loop), these
7028 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029 */
7030 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007031 {
7032 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007033 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7034 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007035 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007036 /* Free the List and ordinary items it contains, but don't recurse
7037 * into Lists and Dictionaries, they will be in the list of dicts
7038 * or list of lists. */
7039 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007040 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007041 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007042 ll = ll_next;
7043 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007044
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007045 return did_free;
7046}
7047
7048/*
7049 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007050 * "list_stack" is used to add lists to be marked. Can be NULL.
7051 *
7052 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007053 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007054 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007055set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007056{
7057 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007058 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007059 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007060 hashtab_T *cur_ht;
7061 ht_stack_T *ht_stack = NULL;
7062 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007063
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007064 cur_ht = ht;
7065 for (;;)
7066 {
7067 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007068 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007069 /* Mark each item in the hashtab. If the item contains a hashtab
7070 * it is added to ht_stack, if it contains a list it is added to
7071 * list_stack. */
7072 todo = (int)cur_ht->ht_used;
7073 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7074 if (!HASHITEM_EMPTY(hi))
7075 {
7076 --todo;
7077 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7078 &ht_stack, list_stack);
7079 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007080 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007081
7082 if (ht_stack == NULL)
7083 break;
7084
7085 /* take an item from the stack */
7086 cur_ht = ht_stack->ht;
7087 tempitem = ht_stack;
7088 ht_stack = ht_stack->prev;
7089 free(tempitem);
7090 }
7091
7092 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007093}
7094
7095/*
7096 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007097 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7098 *
7099 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007100 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007101 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007102set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007103{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007104 listitem_T *li;
7105 int abort = FALSE;
7106 list_T *cur_l;
7107 list_stack_T *list_stack = NULL;
7108 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007109
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007110 cur_l = l;
7111 for (;;)
7112 {
7113 if (!abort)
7114 /* Mark each item in the list. If the item contains a hashtab
7115 * it is added to ht_stack, if it contains a list it is added to
7116 * list_stack. */
7117 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7118 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7119 ht_stack, &list_stack);
7120 if (list_stack == NULL)
7121 break;
7122
7123 /* take an item from the stack */
7124 cur_l = list_stack->list;
7125 tempitem = list_stack;
7126 list_stack = list_stack->prev;
7127 free(tempitem);
7128 }
7129
7130 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007131}
7132
7133/*
7134 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007135 * "list_stack" is used to add lists to be marked. Can be NULL.
7136 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7137 *
7138 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007139 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007140 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007141set_ref_in_item(
7142 typval_T *tv,
7143 int copyID,
7144 ht_stack_T **ht_stack,
7145 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007146{
7147 dict_T *dd;
7148 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007149 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007150
Bram Moolenaara03f2332016-02-06 18:09:59 +01007151 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007152 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007153 dd = tv->vval.v_dict;
7154 if (dd != NULL && dd->dv_copyID != copyID)
7155 {
7156 /* Didn't see this dict yet. */
7157 dd->dv_copyID = copyID;
7158 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007159 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007160 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7161 }
7162 else
7163 {
7164 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7165 if (newitem == NULL)
7166 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007167 else
7168 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007169 newitem->ht = &dd->dv_hashtab;
7170 newitem->prev = *ht_stack;
7171 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007172 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007173 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007174 }
7175 }
7176 else if (tv->v_type == VAR_LIST)
7177 {
7178 ll = tv->vval.v_list;
7179 if (ll != NULL && ll->lv_copyID != copyID)
7180 {
7181 /* Didn't see this list yet. */
7182 ll->lv_copyID = copyID;
7183 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007184 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007185 abort = set_ref_in_list(ll, copyID, ht_stack);
7186 }
7187 else
7188 {
7189 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007190 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007191 if (newitem == NULL)
7192 abort = TRUE;
7193 else
7194 {
7195 newitem->list = ll;
7196 newitem->prev = *list_stack;
7197 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007198 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007199 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007200 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007201 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007202 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007203}
7204
7205/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007206 * Allocate an empty header for a dictionary.
7207 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007208 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007209dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007210{
Bram Moolenaar33570922005-01-25 22:26:29 +00007211 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007212
Bram Moolenaar33570922005-01-25 22:26:29 +00007213 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007214 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007215 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007216 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007217 if (first_dict != NULL)
7218 first_dict->dv_used_prev = d;
7219 d->dv_used_next = first_dict;
7220 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007221 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007222
Bram Moolenaar33570922005-01-25 22:26:29 +00007223 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007224 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007225 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007226 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007227 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007228 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007229 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007230}
7231
7232/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007233 * Allocate an empty dict for a return value.
7234 * Returns OK or FAIL.
7235 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007236 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007237rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007238{
7239 dict_T *d = dict_alloc();
7240
7241 if (d == NULL)
7242 return FAIL;
7243
7244 rettv->vval.v_dict = d;
7245 rettv->v_type = VAR_DICT;
7246 ++d->dv_refcount;
7247 return OK;
7248}
7249
7250
7251/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252 * Unreference a Dictionary: decrement the reference count and free it when it
7253 * becomes zero.
7254 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007255 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007256dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007258 if (d != NULL && --d->dv_refcount <= 0)
7259 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007260}
7261
7262/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007263 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007264 * Ignores the reference count.
7265 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007266 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007267dict_free(
7268 dict_T *d,
7269 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007270{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007271 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007272 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007273 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007274
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007275 /* Remove the dict from the list of dicts for garbage collection. */
7276 if (d->dv_used_prev == NULL)
7277 first_dict = d->dv_used_next;
7278 else
7279 d->dv_used_prev->dv_used_next = d->dv_used_next;
7280 if (d->dv_used_next != NULL)
7281 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7282
7283 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007284 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007285 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007286 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007287 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007288 if (!HASHITEM_EMPTY(hi))
7289 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007290 /* Remove the item before deleting it, just in case there is
7291 * something recursive causing trouble. */
7292 di = HI2DI(hi);
7293 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007294 if (recurse || (di->di_tv.v_type != VAR_LIST
7295 && di->di_tv.v_type != VAR_DICT))
7296 clear_tv(&di->di_tv);
7297 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007298 --todo;
7299 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007300 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007301 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 vim_free(d);
7303}
7304
7305/*
7306 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307 * The "key" is copied to the new item.
7308 * Note that the value of the item "di_tv" still needs to be initialized!
7309 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007310 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007311 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007312dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007313{
Bram Moolenaar33570922005-01-25 22:26:29 +00007314 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007315
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007316 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007318 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007319 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007320 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007321 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007322 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007323}
7324
7325/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007326 * Make a copy of a Dictionary item.
7327 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007329dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007330{
Bram Moolenaar33570922005-01-25 22:26:29 +00007331 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007333 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7334 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007335 if (di != NULL)
7336 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007337 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007338 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007339 copy_tv(&org->di_tv, &di->di_tv);
7340 }
7341 return di;
7342}
7343
7344/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007345 * Remove item "item" from Dictionary "dict" and free it.
7346 */
7347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007348dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349{
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007351
Bram Moolenaar33570922005-01-25 22:26:29 +00007352 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 if (HASHITEM_EMPTY(hi))
7354 EMSG2(_(e_intern2), "dictitem_remove()");
7355 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007356 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007357 dictitem_free(item);
7358}
7359
7360/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007361 * Free a dict item. Also clears the value.
7362 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007363 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007364dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007365{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007366 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007367 if (item->di_flags & DI_FLAGS_ALLOC)
7368 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007369}
7370
7371/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007372 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7373 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007374 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007375 * Returns NULL when out of memory.
7376 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007377 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007378dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007379{
Bram Moolenaar33570922005-01-25 22:26:29 +00007380 dict_T *copy;
7381 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007382 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007383 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007384
7385 if (orig == NULL)
7386 return NULL;
7387
7388 copy = dict_alloc();
7389 if (copy != NULL)
7390 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007391 if (copyID != 0)
7392 {
7393 orig->dv_copyID = copyID;
7394 orig->dv_copydict = copy;
7395 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007396 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007397 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007398 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007399 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007400 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007401 --todo;
7402
7403 di = dictitem_alloc(hi->hi_key);
7404 if (di == NULL)
7405 break;
7406 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007407 {
7408 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7409 copyID) == FAIL)
7410 {
7411 vim_free(di);
7412 break;
7413 }
7414 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007415 else
7416 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7417 if (dict_add(copy, di) == FAIL)
7418 {
7419 dictitem_free(di);
7420 break;
7421 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007422 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007423 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007424
Bram Moolenaare9a41262005-01-15 22:18:47 +00007425 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007426 if (todo > 0)
7427 {
7428 dict_unref(copy);
7429 copy = NULL;
7430 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007431 }
7432
7433 return copy;
7434}
7435
7436/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007437 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007438 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007439 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007440 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007441dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007442{
Bram Moolenaar33570922005-01-25 22:26:29 +00007443 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007444}
7445
Bram Moolenaar8c711452005-01-14 21:53:12 +00007446/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007447 * Add a number or string entry to dictionary "d".
7448 * When "str" is NULL use number "nr", otherwise use "str".
7449 * Returns FAIL when out of memory and when key already exists.
7450 */
7451 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007452dict_add_nr_str(
7453 dict_T *d,
7454 char *key,
7455 long nr,
7456 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007457{
7458 dictitem_T *item;
7459
7460 item = dictitem_alloc((char_u *)key);
7461 if (item == NULL)
7462 return FAIL;
7463 item->di_tv.v_lock = 0;
7464 if (str == NULL)
7465 {
7466 item->di_tv.v_type = VAR_NUMBER;
7467 item->di_tv.vval.v_number = nr;
7468 }
7469 else
7470 {
7471 item->di_tv.v_type = VAR_STRING;
7472 item->di_tv.vval.v_string = vim_strsave(str);
7473 }
7474 if (dict_add(d, item) == FAIL)
7475 {
7476 dictitem_free(item);
7477 return FAIL;
7478 }
7479 return OK;
7480}
7481
7482/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007483 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007484 * Returns FAIL when out of memory and when key already exists.
7485 */
7486 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007487dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007488{
7489 dictitem_T *item;
7490
7491 item = dictitem_alloc((char_u *)key);
7492 if (item == NULL)
7493 return FAIL;
7494 item->di_tv.v_lock = 0;
7495 item->di_tv.v_type = VAR_LIST;
7496 item->di_tv.vval.v_list = list;
7497 if (dict_add(d, item) == FAIL)
7498 {
7499 dictitem_free(item);
7500 return FAIL;
7501 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007502 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007503 return OK;
7504}
7505
7506/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007507 * Get the number of items in a Dictionary.
7508 */
7509 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007510dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007511{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007512 if (d == NULL)
7513 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007514 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007515}
7516
7517/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518 * Find item "key[len]" in Dictionary "d".
7519 * If "len" is negative use strlen(key).
7520 * Returns NULL when not found.
7521 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007522 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007523dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007524{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007525#define AKEYLEN 200
7526 char_u buf[AKEYLEN];
7527 char_u *akey;
7528 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007529 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007530
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007531 if (len < 0)
7532 akey = key;
7533 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007534 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007535 tofree = akey = vim_strnsave(key, len);
7536 if (akey == NULL)
7537 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007538 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007539 else
7540 {
7541 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007542 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007543 akey = buf;
7544 }
7545
Bram Moolenaar33570922005-01-25 22:26:29 +00007546 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007547 vim_free(tofree);
7548 if (HASHITEM_EMPTY(hi))
7549 return NULL;
7550 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007551}
7552
7553/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007554 * Get a string item from a dictionary.
7555 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007556 * Returns NULL if the entry doesn't exist or out of memory.
7557 */
7558 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007559get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007560{
7561 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007562 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007563
7564 di = dict_find(d, key, -1);
7565 if (di == NULL)
7566 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007567 s = get_tv_string(&di->di_tv);
7568 if (save && s != NULL)
7569 s = vim_strsave(s);
7570 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007571}
7572
7573/*
7574 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007575 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007576 */
7577 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007578get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007579{
7580 dictitem_T *di;
7581
7582 di = dict_find(d, key, -1);
7583 if (di == NULL)
7584 return 0;
7585 return get_tv_number(&di->di_tv);
7586}
7587
7588/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007589 * Return an allocated string with the string representation of a Dictionary.
7590 * May return NULL.
7591 */
7592 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007593dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007594{
7595 garray_T ga;
7596 int first = TRUE;
7597 char_u *tofree;
7598 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007599 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007601 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007602 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007603
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007604 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007605 return NULL;
7606 ga_init2(&ga, (int)sizeof(char), 80);
7607 ga_append(&ga, '{');
7608
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007609 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007610 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007611 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007612 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007614 --todo;
7615
7616 if (first)
7617 first = FALSE;
7618 else
7619 ga_concat(&ga, (char_u *)", ");
7620
7621 tofree = string_quote(hi->hi_key, FALSE);
7622 if (tofree != NULL)
7623 {
7624 ga_concat(&ga, tofree);
7625 vim_free(tofree);
7626 }
7627 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007628 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007629 if (s != NULL)
7630 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007632 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007633 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007634 line_breakcheck();
7635
Bram Moolenaar8c711452005-01-14 21:53:12 +00007636 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007637 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007638 if (todo > 0)
7639 {
7640 vim_free(ga.ga_data);
7641 return NULL;
7642 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007643
7644 ga_append(&ga, '}');
7645 ga_append(&ga, NUL);
7646 return (char_u *)ga.ga_data;
7647}
7648
7649/*
7650 * Allocate a variable for a Dictionary and fill it from "*arg".
7651 * Return OK or FAIL. Returns NOTDONE for {expr}.
7652 */
7653 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007654get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655{
Bram Moolenaar33570922005-01-25 22:26:29 +00007656 dict_T *d = NULL;
7657 typval_T tvkey;
7658 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007659 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007660 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007661 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007662 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663
7664 /*
7665 * First check if it's not a curly-braces thing: {expr}.
7666 * Must do this without evaluating, otherwise a function may be called
7667 * twice. Unfortunately this means we need to call eval1() twice for the
7668 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007669 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007670 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007671 if (*start != '}')
7672 {
7673 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7674 return FAIL;
7675 if (*start == '}')
7676 return NOTDONE;
7677 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007678
7679 if (evaluate)
7680 {
7681 d = dict_alloc();
7682 if (d == NULL)
7683 return FAIL;
7684 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007685 tvkey.v_type = VAR_UNKNOWN;
7686 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687
7688 *arg = skipwhite(*arg + 1);
7689 while (**arg != '}' && **arg != NUL)
7690 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007691 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692 goto failret;
7693 if (**arg != ':')
7694 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007695 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007696 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007697 goto failret;
7698 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007699 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007700 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007701 key = get_tv_string_buf_chk(&tvkey, buf);
7702 if (key == NULL || *key == NUL)
7703 {
7704 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7705 if (key != NULL)
7706 EMSG(_(e_emptykey));
7707 clear_tv(&tvkey);
7708 goto failret;
7709 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007710 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007711
7712 *arg = skipwhite(*arg + 1);
7713 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7714 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007715 if (evaluate)
7716 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007717 goto failret;
7718 }
7719 if (evaluate)
7720 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007721 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007722 if (item != NULL)
7723 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007724 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007725 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007726 clear_tv(&tv);
7727 goto failret;
7728 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007729 item = dictitem_alloc(key);
7730 clear_tv(&tvkey);
7731 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007732 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007733 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007734 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007735 if (dict_add(d, item) == FAIL)
7736 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007737 }
7738 }
7739
7740 if (**arg == '}')
7741 break;
7742 if (**arg != ',')
7743 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007744 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007745 goto failret;
7746 }
7747 *arg = skipwhite(*arg + 1);
7748 }
7749
7750 if (**arg != '}')
7751 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007752 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007753failret:
7754 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007755 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007756 return FAIL;
7757 }
7758
7759 *arg = skipwhite(*arg + 1);
7760 if (evaluate)
7761 {
7762 rettv->v_type = VAR_DICT;
7763 rettv->vval.v_dict = d;
7764 ++d->dv_refcount;
7765 }
7766
7767 return OK;
7768}
7769
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007770#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007771#endif
7772
Bram Moolenaar17a13432016-01-24 14:22:10 +01007773 static char *
7774get_var_special_name(int nr)
7775{
7776 switch (nr)
7777 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007778 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007779 case VVAL_TRUE: return "v:true";
7780 case VVAL_NONE: return "v:none";
7781 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007782 }
7783 EMSG2(_(e_intern2), "get_var_special_name()");
7784 return "42";
7785}
7786
Bram Moolenaar8c711452005-01-14 21:53:12 +00007787/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007788 * Return a string with the string representation of a variable.
7789 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007790 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007791 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007792 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007793 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007794 */
7795 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007796echo_string(
7797 typval_T *tv,
7798 char_u **tofree,
7799 char_u *numbuf,
7800 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007801{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007802 static int recurse = 0;
7803 char_u *r = NULL;
7804
Bram Moolenaar33570922005-01-25 22:26:29 +00007805 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007806 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007807 if (!did_echo_string_emsg)
7808 {
7809 /* Only give this message once for a recursive call to avoid
7810 * flooding the user with errors. And stop iterating over lists
7811 * and dicts. */
7812 did_echo_string_emsg = TRUE;
7813 EMSG(_("E724: variable nested too deep for displaying"));
7814 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007815 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007816 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007817 }
7818 ++recurse;
7819
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007820 switch (tv->v_type)
7821 {
7822 case VAR_FUNC:
7823 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007824 r = tv->vval.v_string;
7825 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007826
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007827 case VAR_PARTIAL:
7828 *tofree = NULL;
7829 /* TODO: arguments */
7830 r = tv->vval.v_partial == NULL ? NULL : tv->vval.v_partial->pt_name;
7831 break;
7832
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007833 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007834 if (tv->vval.v_list == NULL)
7835 {
7836 *tofree = NULL;
7837 r = NULL;
7838 }
7839 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7840 {
7841 *tofree = NULL;
7842 r = (char_u *)"[...]";
7843 }
7844 else
7845 {
7846 tv->vval.v_list->lv_copyID = copyID;
7847 *tofree = list2string(tv, copyID);
7848 r = *tofree;
7849 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007850 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007851
Bram Moolenaar8c711452005-01-14 21:53:12 +00007852 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007853 if (tv->vval.v_dict == NULL)
7854 {
7855 *tofree = NULL;
7856 r = NULL;
7857 }
7858 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7859 {
7860 *tofree = NULL;
7861 r = (char_u *)"{...}";
7862 }
7863 else
7864 {
7865 tv->vval.v_dict->dv_copyID = copyID;
7866 *tofree = dict2string(tv, copyID);
7867 r = *tofree;
7868 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007869 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007870
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007871 case VAR_STRING:
7872 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007873 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007874 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007875 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007876 *tofree = NULL;
7877 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007878 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007879
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007880 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007881#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007882 *tofree = NULL;
7883 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7884 r = numbuf;
7885 break;
7886#endif
7887
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007888 case VAR_SPECIAL:
7889 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007890 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007891 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007892 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007893
Bram Moolenaar8502c702014-06-17 12:51:16 +02007894 if (--recurse == 0)
7895 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007896 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007897}
7898
7899/*
7900 * Return a string with the string representation of a variable.
7901 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7902 * "numbuf" is used for a number.
7903 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007904 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007905 */
7906 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007907tv2string(
7908 typval_T *tv,
7909 char_u **tofree,
7910 char_u *numbuf,
7911 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007912{
7913 switch (tv->v_type)
7914 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007915 case VAR_FUNC:
7916 *tofree = string_quote(tv->vval.v_string, TRUE);
7917 return *tofree;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007918 case VAR_PARTIAL:
7919 *tofree = string_quote(tv->vval.v_partial == NULL ? NULL
7920 : tv->vval.v_partial->pt_name, TRUE);
7921 return *tofree;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007922 case VAR_STRING:
7923 *tofree = string_quote(tv->vval.v_string, FALSE);
7924 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007925 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01007926#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007927 *tofree = NULL;
7928 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7929 return numbuf;
7930#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007931 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007932 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007933 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007934 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007935 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007936 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007937 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007938 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007939 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007940 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007941}
7942
7943/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007944 * Return string "str" in ' quotes, doubling ' characters.
7945 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007946 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007947 */
7948 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007949string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007950{
Bram Moolenaar33570922005-01-25 22:26:29 +00007951 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007952 char_u *p, *r, *s;
7953
Bram Moolenaar33570922005-01-25 22:26:29 +00007954 len = (function ? 13 : 3);
7955 if (str != NULL)
7956 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007957 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007958 for (p = str; *p != NUL; mb_ptr_adv(p))
7959 if (*p == '\'')
7960 ++len;
7961 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007962 s = r = alloc(len);
7963 if (r != NULL)
7964 {
7965 if (function)
7966 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007967 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007968 r += 10;
7969 }
7970 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007971 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007972 if (str != NULL)
7973 for (p = str; *p != NUL; )
7974 {
7975 if (*p == '\'')
7976 *r++ = '\'';
7977 MB_COPY_CHAR(p, r);
7978 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007979 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007980 if (function)
7981 *r++ = ')';
7982 *r++ = NUL;
7983 }
7984 return s;
7985}
7986
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007987#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007988/*
7989 * Convert the string "text" to a floating point number.
7990 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7991 * this always uses a decimal point.
7992 * Returns the length of the text that was consumed.
7993 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007994 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007995string2float(
7996 char_u *text,
7997 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007998{
7999 char *s = (char *)text;
8000 float_T f;
8001
8002 f = strtod(s, &s);
8003 *value = f;
8004 return (int)((char_u *)s - text);
8005}
8006#endif
8007
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 * Get the value of an environment variable.
8010 * "arg" is pointing to the '$'. It is advanced to after the name.
8011 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008012 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 */
8014 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008015get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016{
8017 char_u *string = NULL;
8018 int len;
8019 int cc;
8020 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008021 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022
8023 ++*arg;
8024 name = *arg;
8025 len = get_env_len(arg);
8026 if (evaluate)
8027 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008028 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008029 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008030
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008031 cc = name[len];
8032 name[len] = NUL;
8033 /* first try vim_getenv(), fast for normal environment vars */
8034 string = vim_getenv(name, &mustfree);
8035 if (string != NULL && *string != NUL)
8036 {
8037 if (!mustfree)
8038 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008040 else
8041 {
8042 if (mustfree)
8043 vim_free(string);
8044
8045 /* next try expanding things like $VIM and ${HOME} */
8046 string = expand_env_save(name - 1);
8047 if (string != NULL && *string == '$')
8048 {
8049 vim_free(string);
8050 string = NULL;
8051 }
8052 }
8053 name[len] = cc;
8054
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008055 rettv->v_type = VAR_STRING;
8056 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 }
8058
8059 return OK;
8060}
8061
8062/*
8063 * Array with names and number of arguments of all internal functions
8064 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8065 */
8066static struct fst
8067{
8068 char *f_name; /* function name */
8069 char f_min_argc; /* minimal number of arguments */
8070 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008071 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008072 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073} functions[] =
8074{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008075#ifdef FEAT_FLOAT
8076 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008077 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008078#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008079 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008080 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008081 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 {"append", 2, 2, f_append},
8083 {"argc", 0, 0, f_argc},
8084 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008085 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008086 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008087#ifdef FEAT_FLOAT
8088 {"asin", 1, 1, f_asin}, /* WJMc */
8089#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008090 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008091 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008092 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008093 {"assert_false", 1, 2, f_assert_false},
8094 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008095#ifdef FEAT_FLOAT
8096 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008097 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008100 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"bufexists", 1, 1, f_bufexists},
8102 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8103 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8104 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8105 {"buflisted", 1, 1, f_buflisted},
8106 {"bufloaded", 1, 1, f_bufloaded},
8107 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008108 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {"bufwinnr", 1, 1, f_bufwinnr},
8110 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008111 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008112 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008113 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008114#ifdef FEAT_FLOAT
8115 {"ceil", 1, 1, f_ceil},
8116#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008117#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008118 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008119 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8120 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008121 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008122 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008123 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008124 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008125 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008126 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008127 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008128 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8129 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008130 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008131 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008132#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008133 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008134 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008136 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008138#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008139 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008140 {"complete_add", 1, 1, f_complete_add},
8141 {"complete_check", 0, 0, f_complete_check},
8142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008144 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008145#ifdef FEAT_FLOAT
8146 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008147 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008148#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008149 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008151 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008152 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008153 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008155 {"diff_filler", 1, 1, f_diff_filler},
8156 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008157 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008158 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008160 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 {"eventhandler", 0, 0, f_eventhandler},
8162 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008163 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008165#ifdef FEAT_FLOAT
8166 {"exp", 1, 1, f_exp},
8167#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008168 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008169 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008170 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8172 {"filereadable", 1, 1, f_filereadable},
8173 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008174 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008175 {"finddir", 1, 3, f_finddir},
8176 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008177#ifdef FEAT_FLOAT
8178 {"float2nr", 1, 1, f_float2nr},
8179 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008180 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008181#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008182 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {"fnamemodify", 2, 2, f_fnamemodify},
8184 {"foldclosed", 1, 1, f_foldclosed},
8185 {"foldclosedend", 1, 1, f_foldclosedend},
8186 {"foldlevel", 1, 1, f_foldlevel},
8187 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008188 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008190 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008191 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008192 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008193 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008194 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 {"getchar", 0, 1, f_getchar},
8196 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008197 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {"getcmdline", 0, 0, f_getcmdline},
8199 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008200 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008201 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008202 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008203 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008204 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008205 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 {"getfsize", 1, 1, f_getfsize},
8207 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008208 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008209 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008210 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008211 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008212 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008213 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008214 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008215 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008217 {"gettabvar", 2, 3, f_gettabvar},
8218 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 {"getwinposx", 0, 0, f_getwinposx},
8220 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008221 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008222 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008223 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008224 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008226 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008227 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008228 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8230 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8231 {"histadd", 2, 2, f_histadd},
8232 {"histdel", 1, 2, f_histdel},
8233 {"histget", 1, 2, f_histget},
8234 {"histnr", 1, 1, f_histnr},
8235 {"hlID", 1, 1, f_hlID},
8236 {"hlexists", 1, 1, f_hlexists},
8237 {"hostname", 0, 0, f_hostname},
8238 {"iconv", 3, 3, f_iconv},
8239 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008240 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008241 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008243 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 {"inputrestore", 0, 0, f_inputrestore},
8245 {"inputsave", 0, 0, f_inputsave},
8246 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008247 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008248 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008250 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008251#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8252 {"isnan", 1, 1, f_isnan},
8253#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008254 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008255#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008256 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008257 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008258 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008259 {"job_start", 1, 2, f_job_start},
8260 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008261 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008262#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008263 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008264 {"js_decode", 1, 1, f_js_decode},
8265 {"js_encode", 1, 1, f_js_encode},
8266 {"json_decode", 1, 1, f_json_decode},
8267 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008268 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008270 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 {"libcall", 3, 3, f_libcall},
8272 {"libcallnr", 3, 3, f_libcallnr},
8273 {"line", 1, 1, f_line},
8274 {"line2byte", 1, 1, f_line2byte},
8275 {"lispindent", 1, 1, f_lispindent},
8276 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008277#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008278 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008279 {"log10", 1, 1, f_log10},
8280#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008281#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008282 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008283#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008284 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008285 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008286 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008287 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008288 {"matchadd", 2, 5, f_matchadd},
8289 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008290 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008291 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008292 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008293 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008294 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008295 {"max", 1, 1, f_max},
8296 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008297#ifdef vim_mkdir
8298 {"mkdir", 1, 3, f_mkdir},
8299#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008300 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008301#ifdef FEAT_MZSCHEME
8302 {"mzeval", 1, 1, f_mzeval},
8303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008305 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008306 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008307 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008308#ifdef FEAT_PERL
8309 {"perleval", 1, 1, f_perleval},
8310#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008311#ifdef FEAT_FLOAT
8312 {"pow", 2, 2, f_pow},
8313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008315 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008316 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008317#ifdef FEAT_PYTHON3
8318 {"py3eval", 1, 1, f_py3eval},
8319#endif
8320#ifdef FEAT_PYTHON
8321 {"pyeval", 1, 1, f_pyeval},
8322#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008323 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008324 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008325 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008326#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008327 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008328#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008329 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 {"remote_expr", 2, 3, f_remote_expr},
8331 {"remote_foreground", 1, 1, f_remote_foreground},
8332 {"remote_peek", 1, 2, f_remote_peek},
8333 {"remote_read", 1, 1, f_remote_read},
8334 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008335 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008337 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008339 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008340#ifdef FEAT_FLOAT
8341 {"round", 1, 1, f_round},
8342#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008343 {"screenattr", 2, 2, f_screenattr},
8344 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008345 {"screencol", 0, 0, f_screencol},
8346 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008347 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008348 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008349 {"searchpair", 3, 7, f_searchpair},
8350 {"searchpairpos", 3, 7, f_searchpairpos},
8351 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {"server2client", 2, 2, f_server2client},
8353 {"serverlist", 0, 0, f_serverlist},
8354 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008355 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008357 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008359 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008360 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008361 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008362 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008364 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008365 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008367#ifdef FEAT_CRYPT
8368 {"sha256", 1, 1, f_sha256},
8369#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008370 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008371 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008373#ifdef FEAT_FLOAT
8374 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008375 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008376#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008377 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008378 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008379 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008380 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008381 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008382#ifdef FEAT_FLOAT
8383 {"sqrt", 1, 1, f_sqrt},
8384 {"str2float", 1, 1, f_str2float},
8385#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008386 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008387 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008388 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389#ifdef HAVE_STRFTIME
8390 {"strftime", 1, 2, f_strftime},
8391#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008392 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008393 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 {"strlen", 1, 1, f_strlen},
8395 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008396 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008398 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008399 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 {"substitute", 4, 4, f_substitute},
8401 {"synID", 3, 3, f_synID},
8402 {"synIDattr", 2, 3, f_synIDattr},
8403 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008404 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008405 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008406 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008407 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008408 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008409 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008410 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008411 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008412 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008413#ifdef FEAT_FLOAT
8414 {"tan", 1, 1, f_tan},
8415 {"tanh", 1, 1, f_tanh},
8416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008418 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008419#ifdef FEAT_TIMERS
8420 {"timer_start", 2, 3, f_timer_start},
8421 {"timer_stop", 1, 1, f_timer_stop},
8422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 {"tolower", 1, 1, f_tolower},
8424 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008425 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008426#ifdef FEAT_FLOAT
8427 {"trunc", 1, 1, f_trunc},
8428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008430 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008431 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008432 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008433 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 {"virtcol", 1, 1, f_virtcol},
8435 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008436 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008437 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008438 {"win_getid", 0, 2, f_win_getid},
8439 {"win_gotoid", 1, 1, f_win_gotoid},
8440 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8441 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 {"winbufnr", 1, 1, f_winbufnr},
8443 {"wincol", 0, 0, f_wincol},
8444 {"winheight", 1, 1, f_winheight},
8445 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008446 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008448 {"winrestview", 1, 1, f_winrestview},
8449 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008451 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008452 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008453 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454};
8455
8456#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8457
8458/*
8459 * Function given to ExpandGeneric() to obtain the list of internal
8460 * or user defined function names.
8461 */
8462 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008463get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464{
8465 static int intidx = -1;
8466 char_u *name;
8467
8468 if (idx == 0)
8469 intidx = -1;
8470 if (intidx < 0)
8471 {
8472 name = get_user_func_name(xp, idx);
8473 if (name != NULL)
8474 return name;
8475 }
8476 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8477 {
8478 STRCPY(IObuff, functions[intidx].f_name);
8479 STRCAT(IObuff, "(");
8480 if (functions[intidx].f_max_argc == 0)
8481 STRCAT(IObuff, ")");
8482 return IObuff;
8483 }
8484
8485 return NULL;
8486}
8487
8488/*
8489 * Function given to ExpandGeneric() to obtain the list of internal or
8490 * user defined variable or function names.
8491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008493get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494{
8495 static int intidx = -1;
8496 char_u *name;
8497
8498 if (idx == 0)
8499 intidx = -1;
8500 if (intidx < 0)
8501 {
8502 name = get_function_name(xp, idx);
8503 if (name != NULL)
8504 return name;
8505 }
8506 return get_user_var_name(xp, ++intidx);
8507}
8508
8509#endif /* FEAT_CMDL_COMPL */
8510
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008511#if defined(EBCDIC) || defined(PROTO)
8512/*
8513 * Compare struct fst by function name.
8514 */
8515 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008516compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008517{
8518 struct fst *p1 = (struct fst *)s1;
8519 struct fst *p2 = (struct fst *)s2;
8520
8521 return STRCMP(p1->f_name, p2->f_name);
8522}
8523
8524/*
8525 * Sort the function table by function name.
8526 * The sorting of the table above is ASCII dependant.
8527 * On machines using EBCDIC we have to sort it.
8528 */
8529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008530sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008531{
8532 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8533
8534 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8535}
8536#endif
8537
8538
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539/*
8540 * Find internal function in table above.
8541 * Return index, or -1 if not found
8542 */
8543 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008544find_internal_func(
8545 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546{
8547 int first = 0;
8548 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8549 int cmp;
8550 int x;
8551
8552 /*
8553 * Find the function name in the table. Binary search.
8554 */
8555 while (first <= last)
8556 {
8557 x = first + ((unsigned)(last - first) >> 1);
8558 cmp = STRCMP(name, functions[x].f_name);
8559 if (cmp < 0)
8560 last = x - 1;
8561 else if (cmp > 0)
8562 first = x + 1;
8563 else
8564 return x;
8565 }
8566 return -1;
8567}
8568
8569/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008570 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8571 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008572 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8573 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008574 */
8575 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008576deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008577{
Bram Moolenaar33570922005-01-25 22:26:29 +00008578 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008579 int cc;
8580
Bram Moolenaar65639032016-03-16 21:40:30 +01008581 if (partialp != NULL)
8582 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008583
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008584 cc = name[*lenp];
8585 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008586 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008587 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008588 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008589 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008590 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008591 {
8592 *lenp = 0;
8593 return (char_u *)""; /* just in case */
8594 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008595 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008596 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008597 }
8598
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008599 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8600 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008601 partial_T *pt = v->di_tv.vval.v_partial;
8602
8603 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008604 {
8605 *lenp = 0;
8606 return (char_u *)""; /* just in case */
8607 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008608 if (partialp != NULL)
8609 *partialp = pt;
8610 *lenp = (int)STRLEN(pt->pt_name);
8611 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008612 }
8613
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008614 return name;
8615}
8616
8617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 * Allocate a variable for the result of a function.
8619 * Return OK or FAIL.
8620 */
8621 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008622get_func_tv(
8623 char_u *name, /* name of the function */
8624 int len, /* length of "name" */
8625 typval_T *rettv,
8626 char_u **arg, /* argument, pointing to the '(' */
8627 linenr_T firstline, /* first line of range */
8628 linenr_T lastline, /* last line of range */
8629 int *doesrange, /* return: function handled range */
8630 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008631 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008632 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633{
8634 char_u *argp;
8635 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008636 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 int argcount = 0; /* number of arguments found */
8638
8639 /*
8640 * Get the arguments.
8641 */
8642 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008643 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 {
8645 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8646 if (*argp == ')' || *argp == ',' || *argp == NUL)
8647 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8649 {
8650 ret = FAIL;
8651 break;
8652 }
8653 ++argcount;
8654 if (*argp != ',')
8655 break;
8656 }
8657 if (*argp == ')')
8658 ++argp;
8659 else
8660 ret = FAIL;
8661
8662 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008663 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008664 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008666 {
8667 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008668 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008669 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008670 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672
8673 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008674 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675
8676 *arg = skipwhite(argp);
8677 return ret;
8678}
8679
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008680#define ERROR_UNKNOWN 0
8681#define ERROR_TOOMANY 1
8682#define ERROR_TOOFEW 2
8683#define ERROR_SCRIPT 3
8684#define ERROR_DICT 4
8685#define ERROR_NONE 5
8686#define ERROR_OTHER 6
8687#define FLEN_FIXED 40
8688
8689/*
8690 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8691 * Change <SNR>123_name() to K_SNR 123_name().
8692 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8693 * (slow).
8694 */
8695 static char_u *
8696fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8697{
8698 int llen;
8699 char_u *fname;
8700 int i;
8701
8702 llen = eval_fname_script(name);
8703 if (llen > 0)
8704 {
8705 fname_buf[0] = K_SPECIAL;
8706 fname_buf[1] = KS_EXTRA;
8707 fname_buf[2] = (int)KE_SNR;
8708 i = 3;
8709 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8710 {
8711 if (current_SID <= 0)
8712 *error = ERROR_SCRIPT;
8713 else
8714 {
8715 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8716 i = (int)STRLEN(fname_buf);
8717 }
8718 }
8719 if (i + STRLEN(name + llen) < FLEN_FIXED)
8720 {
8721 STRCPY(fname_buf + i, name + llen);
8722 fname = fname_buf;
8723 }
8724 else
8725 {
8726 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8727 if (fname == NULL)
8728 *error = ERROR_OTHER;
8729 else
8730 {
8731 *tofree = fname;
8732 mch_memmove(fname, fname_buf, (size_t)i);
8733 STRCPY(fname + i, name + llen);
8734 }
8735 }
8736 }
8737 else
8738 fname = name;
8739 return fname;
8740}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741
8742/*
8743 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008744 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008745 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008747 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008748call_func(
8749 char_u *funcname, /* name of the function */
8750 int len, /* length of "name" */
8751 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008752 int argcount_in, /* number of "argvars" */
8753 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008754 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008755 linenr_T firstline, /* first line of range */
8756 linenr_T lastline, /* last line of range */
8757 int *doesrange, /* return: function handled range */
8758 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008759 partial_T *partial, /* optional, can be NULL */
8760 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761{
8762 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 int error = ERROR_NONE;
8764 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008767 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008769 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008770 int argcount = argcount_in;
8771 typval_T *argvars = argvars_in;
8772 dict_T *selfdict = selfdict_in;
8773 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
8774 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008775
8776 /* Make a copy of the name, if it comes from a funcref variable it could
8777 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008778 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008779 if (name == NULL)
8780 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008782 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783
8784 *doesrange = FALSE;
8785
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008786 if (partial != NULL)
8787 {
8788 if (partial->pt_dict != NULL)
8789 {
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008790 /* When the function has a partial with a dict and there is a dict
8791 * argument, use the dict argument. That is backwards compatible.
8792 */
8793 if (selfdict_in == NULL)
8794 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008795 }
8796 if (error == ERROR_NONE && partial->pt_argc > 0)
8797 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008798 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
8799 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
8800 for (i = 0; i < argcount_in; ++i)
8801 argv[i + argv_clear] = argvars_in[i];
8802 argvars = argv;
8803 argcount = partial->pt_argc + argcount_in;
8804 }
8805 }
8806
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807
8808 /* execute the function if no errors detected and executing */
8809 if (evaluate && error == ERROR_NONE)
8810 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008811 char_u *rfname = fname;
8812
8813 /* Ignore "g:" before a function name. */
8814 if (fname[0] == 'g' && fname[1] == ':')
8815 rfname = fname + 2;
8816
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008817 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8818 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 error = ERROR_UNKNOWN;
8820
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008821 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 {
8823 /*
8824 * User defined function.
8825 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008826 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008827
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008829 /* Trigger FuncUndefined event, may load the function. */
8830 if (fp == NULL
8831 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008832 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008833 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008835 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008836 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 }
8838#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008839 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008840 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008841 {
8842 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008843 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008844 }
8845
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 if (fp != NULL)
8847 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008848 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008850 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008852 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008854 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008855 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 else
8857 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008858 int did_save_redo = FALSE;
8859
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 /*
8861 * Call the user function.
8862 * Save and restore search patterns, script variables and
8863 * redo buffer.
8864 */
8865 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008866#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008867 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008868#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008869 {
8870 saveRedobuff();
8871 did_save_redo = TRUE;
8872 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008873 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008874 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008875 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008876 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8877 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8878 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008879 /* Function was unreferenced while being used, free it
8880 * now. */
8881 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008882 if (did_save_redo)
8883 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 restore_search_patterns();
8885 error = ERROR_NONE;
8886 }
8887 }
8888 }
8889 else
8890 {
8891 /*
8892 * Find the function name in the table, call its implementation.
8893 */
8894 i = find_internal_func(fname);
8895 if (i >= 0)
8896 {
8897 if (argcount < functions[i].f_min_argc)
8898 error = ERROR_TOOFEW;
8899 else if (argcount > functions[i].f_max_argc)
8900 error = ERROR_TOOMANY;
8901 else
8902 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008903 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008904 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 error = ERROR_NONE;
8906 }
8907 }
8908 }
8909 /*
8910 * The function call (or "FuncUndefined" autocommand sequence) might
8911 * have been aborted by an error, an interrupt, or an explicitly thrown
8912 * exception that has not been caught so far. This situation can be
8913 * tested for by calling aborting(). For an error in an internal
8914 * function or for the "E132" error in call_user_func(), however, the
8915 * throw point at which the "force_abort" flag (temporarily reset by
8916 * emsg()) is normally updated has not been reached yet. We need to
8917 * update that flag first to make aborting() reliable.
8918 */
8919 update_force_abort();
8920 }
8921 if (error == ERROR_NONE)
8922 ret = OK;
8923
8924 /*
8925 * Report an error unless the argument evaluation or function call has been
8926 * cancelled due to an aborting error, an interrupt, or an exception.
8927 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008928 if (!aborting())
8929 {
8930 switch (error)
8931 {
8932 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008933 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008934 break;
8935 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008936 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008937 break;
8938 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008939 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008940 name);
8941 break;
8942 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008943 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008944 name);
8945 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008946 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008947 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008948 name);
8949 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008950 }
8951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008953 while (argv_clear > 0)
8954 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008955 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008956 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957
8958 return ret;
8959}
8960
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008961/*
8962 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008963 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008964 */
8965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008966emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008967{
8968 char_u *p;
8969
8970 if (*name == K_SPECIAL)
8971 p = concat_str((char_u *)"<SNR>", name + 3);
8972 else
8973 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008974 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008975 if (p != name)
8976 vim_free(p);
8977}
8978
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008979/*
8980 * Return TRUE for a non-zero Number and a non-empty String.
8981 */
8982 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008983non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008984{
8985 return ((argvars[0].v_type == VAR_NUMBER
8986 && argvars[0].vval.v_number != 0)
8987 || (argvars[0].v_type == VAR_STRING
8988 && argvars[0].vval.v_string != NULL
8989 && *argvars[0].vval.v_string != NUL));
8990}
8991
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992/*********************************************
8993 * Implementation of the built-in functions
8994 */
8995
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008996#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008997static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008998
8999/*
9000 * Get the float value of "argvars[0]" into "f".
9001 * Returns FAIL when the argument is not a Number or Float.
9002 */
9003 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009004get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009005{
9006 if (argvars[0].v_type == VAR_FLOAT)
9007 {
9008 *f = argvars[0].vval.v_float;
9009 return OK;
9010 }
9011 if (argvars[0].v_type == VAR_NUMBER)
9012 {
9013 *f = (float_T)argvars[0].vval.v_number;
9014 return OK;
9015 }
9016 EMSG(_("E808: Number or Float required"));
9017 return FAIL;
9018}
9019
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009020/*
9021 * "abs(expr)" function
9022 */
9023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009024f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009025{
9026 if (argvars[0].v_type == VAR_FLOAT)
9027 {
9028 rettv->v_type = VAR_FLOAT;
9029 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9030 }
9031 else
9032 {
9033 varnumber_T n;
9034 int error = FALSE;
9035
9036 n = get_tv_number_chk(&argvars[0], &error);
9037 if (error)
9038 rettv->vval.v_number = -1;
9039 else if (n > 0)
9040 rettv->vval.v_number = n;
9041 else
9042 rettv->vval.v_number = -n;
9043 }
9044}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009045
9046/*
9047 * "acos()" function
9048 */
9049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009050f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009051{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009052 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009053
9054 rettv->v_type = VAR_FLOAT;
9055 if (get_float_arg(argvars, &f) == OK)
9056 rettv->vval.v_float = acos(f);
9057 else
9058 rettv->vval.v_float = 0.0;
9059}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009060#endif
9061
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009063 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 */
9065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009066f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067{
Bram Moolenaar33570922005-01-25 22:26:29 +00009068 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009070 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009071 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009073 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009074 && !tv_check_lock(l->lv_lock,
9075 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009076 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009078 }
9079 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009080 EMSG(_(e_listreq));
9081}
9082
9083/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009084 * "alloc_fail(id, countdown, repeat)" function
9085 */
9086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009087f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009088{
9089 if (argvars[0].v_type != VAR_NUMBER
9090 || argvars[0].vval.v_number <= 0
9091 || argvars[1].v_type != VAR_NUMBER
9092 || argvars[1].vval.v_number < 0
9093 || argvars[2].v_type != VAR_NUMBER)
9094 EMSG(_(e_invarg));
9095 else
9096 {
9097 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009098 if (alloc_fail_id >= aid_last)
9099 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009100 alloc_fail_countdown = argvars[1].vval.v_number;
9101 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009102 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009103 }
9104}
9105
9106/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009107 * "and(expr, expr)" function
9108 */
9109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009110f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009111{
9112 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9113 & get_tv_number_chk(&argvars[1], NULL);
9114}
9115
9116/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009117 * "append(lnum, string/list)" function
9118 */
9119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009120f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009121{
9122 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009123 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009124 list_T *l = NULL;
9125 listitem_T *li = NULL;
9126 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009127 long added = 0;
9128
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009129 /* When coming here from Insert mode, sync undo, so that this can be
9130 * undone separately from what was previously inserted. */
9131 if (u_sync_once == 2)
9132 {
9133 u_sync_once = 1; /* notify that u_sync() was called */
9134 u_sync(TRUE);
9135 }
9136
Bram Moolenaar0d660222005-01-07 21:51:51 +00009137 lnum = get_tv_lnum(argvars);
9138 if (lnum >= 0
9139 && lnum <= curbuf->b_ml.ml_line_count
9140 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009141 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009142 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009143 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009144 l = argvars[1].vval.v_list;
9145 if (l == NULL)
9146 return;
9147 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009148 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009149 for (;;)
9150 {
9151 if (l == NULL)
9152 tv = &argvars[1]; /* append a string */
9153 else if (li == NULL)
9154 break; /* end of list */
9155 else
9156 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009157 line = get_tv_string_chk(tv);
9158 if (line == NULL) /* type error */
9159 {
9160 rettv->vval.v_number = 1; /* Failed */
9161 break;
9162 }
9163 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009164 ++added;
9165 if (l == NULL)
9166 break;
9167 li = li->li_next;
9168 }
9169
9170 appended_lines_mark(lnum, added);
9171 if (curwin->w_cursor.lnum > lnum)
9172 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009174 else
9175 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176}
9177
9178/*
9179 * "argc()" function
9180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009182f_argc(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 = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185}
9186
9187/*
9188 * "argidx()" function
9189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009191f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009193 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194}
9195
9196/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009197 * "arglistid()" function
9198 */
9199 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009200f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009201{
9202 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009203
9204 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009205 wp = find_tabwin(&argvars[0], &argvars[1]);
9206 if (wp != NULL)
9207 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009208}
9209
9210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 * "argv(nr)" function
9212 */
9213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009214f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215{
9216 int idx;
9217
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009218 if (argvars[0].v_type != VAR_UNKNOWN)
9219 {
9220 idx = get_tv_number_chk(&argvars[0], NULL);
9221 if (idx >= 0 && idx < ARGCOUNT)
9222 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9223 else
9224 rettv->vval.v_string = NULL;
9225 rettv->v_type = VAR_STRING;
9226 }
9227 else if (rettv_list_alloc(rettv) == OK)
9228 for (idx = 0; idx < ARGCOUNT; ++idx)
9229 list_append_string(rettv->vval.v_list,
9230 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231}
9232
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009233static void prepare_assert_error(garray_T*gap);
9234static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9235static void assert_error(garray_T *gap);
9236static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009237
9238/*
9239 * Prepare "gap" for an assert error and add the sourcing position.
9240 */
9241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009242prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009243{
9244 char buf[NUMBUFLEN];
9245
9246 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009247 if (sourcing_name != NULL)
9248 {
9249 ga_concat(gap, sourcing_name);
9250 if (sourcing_lnum > 0)
9251 ga_concat(gap, (char_u *)" ");
9252 }
9253 if (sourcing_lnum > 0)
9254 {
9255 sprintf(buf, "line %ld", (long)sourcing_lnum);
9256 ga_concat(gap, (char_u *)buf);
9257 }
9258 if (sourcing_name != NULL || sourcing_lnum > 0)
9259 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009260}
9261
9262/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009263 * Append "str" to "gap", escaping unprintable characters.
9264 * Changes NL to \n, CR to \r, etc.
9265 */
9266 static void
9267ga_concat_esc(garray_T *gap, char_u *str)
9268{
9269 char_u *p;
9270 char_u buf[NUMBUFLEN];
9271
Bram Moolenaarf1551962016-03-15 12:55:58 +01009272 if (str == NULL)
9273 {
9274 ga_concat(gap, (char_u *)"NULL");
9275 return;
9276 }
9277
Bram Moolenaar23689172016-02-15 22:37:37 +01009278 for (p = str; *p != NUL; ++p)
9279 switch (*p)
9280 {
9281 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9282 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9283 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9284 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9285 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9286 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9287 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9288 default:
9289 if (*p < ' ')
9290 {
9291 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9292 ga_concat(gap, buf);
9293 }
9294 else
9295 ga_append(gap, *p);
9296 break;
9297 }
9298}
9299
9300/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009301 * Fill "gap" with information about an assert error.
9302 */
9303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009304fill_assert_error(
9305 garray_T *gap,
9306 typval_T *opt_msg_tv,
9307 char_u *exp_str,
9308 typval_T *exp_tv,
9309 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009310{
9311 char_u numbuf[NUMBUFLEN];
9312 char_u *tofree;
9313
9314 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9315 {
9316 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9317 vim_free(tofree);
9318 }
9319 else
9320 {
9321 ga_concat(gap, (char_u *)"Expected ");
9322 if (exp_str == NULL)
9323 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009324 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009325 vim_free(tofree);
9326 }
9327 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009328 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009329 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009330 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009331 vim_free(tofree);
9332 }
9333}
Bram Moolenaar43345542015-11-29 17:35:35 +01009334
9335/*
9336 * Add an assert error to v:errors.
9337 */
9338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009339assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009340{
9341 struct vimvar *vp = &vimvars[VV_ERRORS];
9342
9343 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9344 /* Make sure v:errors is a list. */
9345 set_vim_var_list(VV_ERRORS, list_alloc());
9346 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9347}
9348
Bram Moolenaar43345542015-11-29 17:35:35 +01009349/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009350 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009351 */
9352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009353f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009354{
9355 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009356
9357 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9358 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009359 prepare_assert_error(&ga);
9360 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9361 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009362 ga_clear(&ga);
9363 }
9364}
9365
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009366/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009367 * "assert_exception(string[, msg])" function
9368 */
9369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009370f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009371{
9372 garray_T ga;
9373 char *error;
9374
9375 error = (char *)get_tv_string_chk(&argvars[0]);
9376 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9377 {
9378 prepare_assert_error(&ga);
9379 ga_concat(&ga, (char_u *)"v:exception is not set");
9380 assert_error(&ga);
9381 ga_clear(&ga);
9382 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009383 else if (error != NULL
9384 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009385 {
9386 prepare_assert_error(&ga);
9387 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9388 &vimvars[VV_EXCEPTION].vv_tv);
9389 assert_error(&ga);
9390 ga_clear(&ga);
9391 }
9392}
9393
9394/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009395 * "assert_fails(cmd [, error])" function
9396 */
9397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009398f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009399{
9400 char_u *cmd = get_tv_string_chk(&argvars[0]);
9401 garray_T ga;
9402
9403 called_emsg = FALSE;
9404 suppress_errthrow = TRUE;
9405 emsg_silent = TRUE;
9406 do_cmdline_cmd(cmd);
9407 if (!called_emsg)
9408 {
9409 prepare_assert_error(&ga);
9410 ga_concat(&ga, (char_u *)"command did not fail: ");
9411 ga_concat(&ga, cmd);
9412 assert_error(&ga);
9413 ga_clear(&ga);
9414 }
9415 else if (argvars[1].v_type != VAR_UNKNOWN)
9416 {
9417 char_u buf[NUMBUFLEN];
9418 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9419
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009420 if (error == NULL
9421 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009422 {
9423 prepare_assert_error(&ga);
9424 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9425 &vimvars[VV_ERRMSG].vv_tv);
9426 assert_error(&ga);
9427 ga_clear(&ga);
9428 }
9429 }
9430
9431 called_emsg = FALSE;
9432 suppress_errthrow = FALSE;
9433 emsg_silent = FALSE;
9434 emsg_on_display = FALSE;
9435 set_vim_var_string(VV_ERRMSG, NULL, 0);
9436}
9437
9438/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009439 * Common for assert_true() and assert_false().
9440 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009442assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009443{
9444 int error = FALSE;
9445 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009446
Bram Moolenaar37127922016-02-06 20:29:28 +01009447 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009448 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009449 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009450 if (argvars[0].v_type != VAR_NUMBER
9451 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9452 || error)
9453 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009454 prepare_assert_error(&ga);
9455 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009456 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009457 NULL, &argvars[0]);
9458 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009459 ga_clear(&ga);
9460 }
9461}
9462
9463/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009464 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009465 */
9466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009467f_assert_false(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, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009470}
9471
9472/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009473 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009474 */
9475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009476f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009477{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009478 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009479}
9480
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009481#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009482/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009483 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009484 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009486f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009487{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009488 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009489
9490 rettv->v_type = VAR_FLOAT;
9491 if (get_float_arg(argvars, &f) == OK)
9492 rettv->vval.v_float = asin(f);
9493 else
9494 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009495}
9496
9497/*
9498 * "atan()" function
9499 */
9500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009501f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009502{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009503 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009504
9505 rettv->v_type = VAR_FLOAT;
9506 if (get_float_arg(argvars, &f) == OK)
9507 rettv->vval.v_float = atan(f);
9508 else
9509 rettv->vval.v_float = 0.0;
9510}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009511
9512/*
9513 * "atan2()" function
9514 */
9515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009516f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009517{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009518 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009519
9520 rettv->v_type = VAR_FLOAT;
9521 if (get_float_arg(argvars, &fx) == OK
9522 && get_float_arg(&argvars[1], &fy) == OK)
9523 rettv->vval.v_float = atan2(fx, fy);
9524 else
9525 rettv->vval.v_float = 0.0;
9526}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009527#endif
9528
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529/*
9530 * "browse(save, title, initdir, default)" function
9531 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009533f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534{
9535#ifdef FEAT_BROWSE
9536 int save;
9537 char_u *title;
9538 char_u *initdir;
9539 char_u *defname;
9540 char_u buf[NUMBUFLEN];
9541 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009542 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009544 save = get_tv_number_chk(&argvars[0], &error);
9545 title = get_tv_string_chk(&argvars[1]);
9546 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9547 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009549 if (error || title == NULL || initdir == NULL || defname == NULL)
9550 rettv->vval.v_string = NULL;
9551 else
9552 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009553 do_browse(save ? BROWSE_SAVE : 0,
9554 title, defname, NULL, initdir, NULL, curbuf);
9555#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009556 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009557#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009558 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009559}
9560
9561/*
9562 * "browsedir(title, initdir)" function
9563 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009565f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009566{
9567#ifdef FEAT_BROWSE
9568 char_u *title;
9569 char_u *initdir;
9570 char_u buf[NUMBUFLEN];
9571
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009572 title = get_tv_string_chk(&argvars[0]);
9573 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009574
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009575 if (title == NULL || initdir == NULL)
9576 rettv->vval.v_string = NULL;
9577 else
9578 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009579 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009581 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009583 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584}
9585
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009586static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009587
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588/*
9589 * Find a buffer by number or exact name.
9590 */
9591 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009592find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593{
9594 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009596 if (avar->v_type == VAR_NUMBER)
9597 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009598 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009600 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009601 if (buf == NULL)
9602 {
9603 /* No full path name match, try a match with a URL or a "nofile"
9604 * buffer, these don't use the full path. */
9605 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9606 if (buf->b_fname != NULL
9607 && (path_with_url(buf->b_fname)
9608#ifdef FEAT_QUICKFIX
9609 || bt_nofile(buf)
9610#endif
9611 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009612 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009613 break;
9614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 }
9616 return buf;
9617}
9618
9619/*
9620 * "bufexists(expr)" function
9621 */
9622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009623f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626}
9627
9628/*
9629 * "buflisted(expr)" function
9630 */
9631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009632f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633{
9634 buf_T *buf;
9635
9636 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009637 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638}
9639
9640/*
9641 * "bufloaded(expr)" function
9642 */
9643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009644f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645{
9646 buf_T *buf;
9647
9648 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009649 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650}
9651
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009652 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009653buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 int save_magic;
9656 char_u *save_cpo;
9657 buf_T *buf;
9658
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9660 save_magic = p_magic;
9661 p_magic = TRUE;
9662 save_cpo = p_cpo;
9663 p_cpo = (char_u *)"";
9664
9665 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009666 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667
9668 p_magic = save_magic;
9669 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009670 return buf;
9671}
9672
9673/*
9674 * Get buffer by number or pattern.
9675 */
9676 static buf_T *
9677get_buf_tv(typval_T *tv, int curtab_only)
9678{
9679 char_u *name = tv->vval.v_string;
9680 buf_T *buf;
9681
9682 if (tv->v_type == VAR_NUMBER)
9683 return buflist_findnr((int)tv->vval.v_number);
9684 if (tv->v_type != VAR_STRING)
9685 return NULL;
9686 if (name == NULL || *name == NUL)
9687 return curbuf;
9688 if (name[0] == '$' && name[1] == NUL)
9689 return lastbuf;
9690
9691 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692
9693 /* If not found, try expanding the name, like done for bufexists(). */
9694 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009695 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696
9697 return buf;
9698}
9699
9700/*
9701 * "bufname(expr)" function
9702 */
9703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009704f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705{
9706 buf_T *buf;
9707
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009708 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009710 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009711 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009713 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009715 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 --emsg_off;
9717}
9718
9719/*
9720 * "bufnr(expr)" function
9721 */
9722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009723f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724{
9725 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009726 int error = FALSE;
9727 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009729 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009731 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009732 --emsg_off;
9733
9734 /* If the buffer isn't found and the second argument is not zero create a
9735 * new buffer. */
9736 if (buf == NULL
9737 && argvars[1].v_type != VAR_UNKNOWN
9738 && get_tv_number_chk(&argvars[1], &error) != 0
9739 && !error
9740 && (name = get_tv_string_chk(&argvars[0])) != NULL
9741 && !error)
9742 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9743
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009745 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009747 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748}
9749
9750/*
9751 * "bufwinnr(nr)" function
9752 */
9753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009754f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755{
9756#ifdef FEAT_WINDOWS
9757 win_T *wp;
9758 int winnr = 0;
9759#endif
9760 buf_T *buf;
9761
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009762 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009764 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765#ifdef FEAT_WINDOWS
9766 for (wp = firstwin; wp; wp = wp->w_next)
9767 {
9768 ++winnr;
9769 if (wp->w_buffer == buf)
9770 break;
9771 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009772 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009774 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775#endif
9776 --emsg_off;
9777}
9778
9779/*
9780 * "byte2line(byte)" function
9781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009783f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784{
9785#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009786 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787#else
9788 long boff = 0;
9789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009790 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009792 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009794 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 (linenr_T)0, &boff);
9796#endif
9797}
9798
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009800byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009801{
9802#ifdef FEAT_MBYTE
9803 char_u *t;
9804#endif
9805 char_u *str;
9806 long idx;
9807
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009808 str = get_tv_string_chk(&argvars[0]);
9809 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009810 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009811 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009812 return;
9813
9814#ifdef FEAT_MBYTE
9815 t = str;
9816 for ( ; idx > 0; idx--)
9817 {
9818 if (*t == NUL) /* EOL reached */
9819 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009820 if (enc_utf8 && comp)
9821 t += utf_ptr2len(t);
9822 else
9823 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009824 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009825 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009826#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009827 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009828 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009829#endif
9830}
9831
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009832/*
9833 * "byteidx()" function
9834 */
9835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009836f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009837{
9838 byteidx(argvars, rettv, FALSE);
9839}
9840
9841/*
9842 * "byteidxcomp()" function
9843 */
9844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009845f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009846{
9847 byteidx(argvars, rettv, TRUE);
9848}
9849
Bram Moolenaardb913952012-06-29 12:54:53 +02009850 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009851func_call(
9852 char_u *name,
9853 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009854 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009855 dict_T *selfdict,
9856 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009857{
9858 listitem_T *item;
9859 typval_T argv[MAX_FUNC_ARGS + 1];
9860 int argc = 0;
9861 int dummy;
9862 int r = 0;
9863
9864 for (item = args->vval.v_list->lv_first; item != NULL;
9865 item = item->li_next)
9866 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009867 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +02009868 {
9869 EMSG(_("E699: Too many arguments"));
9870 break;
9871 }
9872 /* Make a copy of each argument. This is needed to be able to set
9873 * v_lock to VAR_FIXED in the copy without changing the original list.
9874 */
9875 copy_tv(&item->li_tv, &argv[argc++]);
9876 }
9877
9878 if (item == NULL)
9879 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9880 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009881 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +02009882
9883 /* Free the arguments. */
9884 while (argc > 0)
9885 clear_tv(&argv[--argc]);
9886
9887 return r;
9888}
9889
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009890/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009891 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009892 */
9893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009894f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009895{
9896 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009897 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00009898 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009899
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009900 if (argvars[1].v_type != VAR_LIST)
9901 {
9902 EMSG(_(e_listreq));
9903 return;
9904 }
9905 if (argvars[1].vval.v_list == NULL)
9906 return;
9907
9908 if (argvars[0].v_type == VAR_FUNC)
9909 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009910 else if (argvars[0].v_type == VAR_PARTIAL)
9911 {
9912 partial = argvars[0].vval.v_partial;
9913 func = partial->pt_name;
9914 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009915 else
9916 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009917 if (*func == NUL)
9918 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009919
Bram Moolenaare9a41262005-01-15 22:18:47 +00009920 if (argvars[2].v_type != VAR_UNKNOWN)
9921 {
9922 if (argvars[2].v_type != VAR_DICT)
9923 {
9924 EMSG(_(e_dictreq));
9925 return;
9926 }
9927 selfdict = argvars[2].vval.v_dict;
9928 }
9929
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009930 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009931}
9932
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009933#ifdef FEAT_FLOAT
9934/*
9935 * "ceil({float})" function
9936 */
9937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009938f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009939{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009940 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009941
9942 rettv->v_type = VAR_FLOAT;
9943 if (get_float_arg(argvars, &f) == OK)
9944 rettv->vval.v_float = ceil(f);
9945 else
9946 rettv->vval.v_float = 0.0;
9947}
9948#endif
9949
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01009950#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009951/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009952 * "ch_close()" function
9953 */
9954 static void
9955f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9956{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009957 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009958
9959 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +01009960 {
Bram Moolenaar8b374212016-02-24 20:43:06 +01009961 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +01009962 channel_clear(channel);
9963 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009964}
9965
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01009966/*
9967 * "ch_getbufnr()" function
9968 */
9969 static void
9970f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
9971{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009972 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01009973
9974 rettv->vval.v_number = -1;
9975 if (channel != NULL)
9976 {
9977 char_u *what = get_tv_string(&argvars[1]);
9978 int part;
9979
9980 if (STRCMP(what, "err") == 0)
9981 part = PART_ERR;
9982 else if (STRCMP(what, "out") == 0)
9983 part = PART_OUT;
9984 else if (STRCMP(what, "in") == 0)
9985 part = PART_IN;
9986 else
9987 part = PART_SOCK;
9988 if (channel->ch_part[part].ch_buffer != NULL)
9989 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
9990 }
9991}
9992
Bram Moolenaar02e83b42016-02-21 20:10:26 +01009993/*
9994 * "ch_getjob()" function
9995 */
9996 static void
9997f_ch_getjob(typval_T *argvars, typval_T *rettv)
9998{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009999 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010000
10001 if (channel != NULL)
10002 {
10003 rettv->v_type = VAR_JOB;
10004 rettv->vval.v_job = channel->ch_job;
10005 if (channel->ch_job != NULL)
10006 ++channel->ch_job->jv_refcount;
10007 }
10008}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010009
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010010/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010011 * "ch_log()" function
10012 */
10013 static void
10014f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10015{
10016 char_u *msg = get_tv_string(&argvars[0]);
10017 channel_T *channel = NULL;
10018
10019 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010020 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010021
10022 ch_log(channel, (char *)msg);
10023}
10024
10025/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010026 * "ch_logfile()" function
10027 */
10028 static void
10029f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10030{
10031 char_u *fname;
10032 char_u *opt = (char_u *)"";
10033 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010034
10035 fname = get_tv_string(&argvars[0]);
10036 if (argvars[1].v_type == VAR_STRING)
10037 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010038 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010039}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010040
10041/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010042 * "ch_open()" function
10043 */
10044 static void
10045f_ch_open(typval_T *argvars, typval_T *rettv)
10046{
Bram Moolenaar77073442016-02-13 23:23:53 +010010047 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010048 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010049}
10050
10051/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010052 * "ch_read()" function
10053 */
10054 static void
10055f_ch_read(typval_T *argvars, typval_T *rettv)
10056{
10057 common_channel_read(argvars, rettv, FALSE);
10058}
10059
10060/*
10061 * "ch_readraw()" function
10062 */
10063 static void
10064f_ch_readraw(typval_T *argvars, typval_T *rettv)
10065{
10066 common_channel_read(argvars, rettv, TRUE);
10067}
10068
10069/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010070 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010071 */
10072 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010073f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10074{
10075 ch_expr_common(argvars, rettv, TRUE);
10076}
10077
10078/*
10079 * "ch_sendexpr()" function
10080 */
10081 static void
10082f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10083{
10084 ch_expr_common(argvars, rettv, FALSE);
10085}
10086
10087/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010088 * "ch_evalraw()" function
10089 */
10090 static void
10091f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10092{
10093 ch_raw_common(argvars, rettv, TRUE);
10094}
10095
10096/*
10097 * "ch_sendraw()" function
10098 */
10099 static void
10100f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10101{
10102 ch_raw_common(argvars, rettv, FALSE);
10103}
10104
10105/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010106 * "ch_setoptions()" function
10107 */
10108 static void
10109f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10110{
10111 channel_T *channel;
10112 jobopt_T opt;
10113
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010114 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010115 if (channel == NULL)
10116 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010117 clear_job_options(&opt);
10118 if (get_job_options(&argvars[1], &opt,
10119 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010120 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010121 channel_set_options(channel, &opt);
10122}
10123
10124/*
10125 * "ch_status()" function
10126 */
10127 static void
10128f_ch_status(typval_T *argvars, typval_T *rettv)
10129{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010130 channel_T *channel;
10131
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010132 /* return an empty string by default */
10133 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010134 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010135
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010136 channel = get_channel_arg(&argvars[0], FALSE);
10137 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010138}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010139#endif
10140
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010141/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010142 * "changenr()" function
10143 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010145f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010146{
10147 rettv->vval.v_number = curbuf->b_u_seq_cur;
10148}
10149
10150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151 * "char2nr(string)" function
10152 */
10153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010154f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155{
10156#ifdef FEAT_MBYTE
10157 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010158 {
10159 int utf8 = 0;
10160
10161 if (argvars[1].v_type != VAR_UNKNOWN)
10162 utf8 = get_tv_number_chk(&argvars[1], NULL);
10163
10164 if (utf8)
10165 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10166 else
10167 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 else
10170#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010171 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172}
10173
10174/*
10175 * "cindent(lnum)" function
10176 */
10177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010178f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179{
10180#ifdef FEAT_CINDENT
10181 pos_T pos;
10182 linenr_T lnum;
10183
10184 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010185 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10187 {
10188 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010189 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 curwin->w_cursor = pos;
10191 }
10192 else
10193#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010194 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195}
10196
10197/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010198 * "clearmatches()" function
10199 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010201f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010202{
10203#ifdef FEAT_SEARCH_EXTRA
10204 clear_matches(curwin);
10205#endif
10206}
10207
10208/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 * "col(string)" function
10210 */
10211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010212f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213{
10214 colnr_T col = 0;
10215 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010216 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010218 fp = var2fpos(&argvars[0], FALSE, &fnum);
10219 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 {
10221 if (fp->col == MAXCOL)
10222 {
10223 /* '> can be MAXCOL, get the length of the line then */
10224 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010225 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 else
10227 col = MAXCOL;
10228 }
10229 else
10230 {
10231 col = fp->col + 1;
10232#ifdef FEAT_VIRTUALEDIT
10233 /* col(".") when the cursor is on the NUL at the end of the line
10234 * because of "coladd" can be seen as an extra column. */
10235 if (virtual_active() && fp == &curwin->w_cursor)
10236 {
10237 char_u *p = ml_get_cursor();
10238
10239 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10240 curwin->w_virtcol - curwin->w_cursor.coladd))
10241 {
10242# ifdef FEAT_MBYTE
10243 int l;
10244
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010245 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 col += l;
10247# else
10248 if (*p != NUL && p[1] == NUL)
10249 ++col;
10250# endif
10251 }
10252 }
10253#endif
10254 }
10255 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010256 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257}
10258
Bram Moolenaar572cb562005-08-05 21:35:02 +000010259#if defined(FEAT_INS_EXPAND)
10260/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010261 * "complete()" function
10262 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010264f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010265{
10266 int startcol;
10267
10268 if ((State & INSERT) == 0)
10269 {
10270 EMSG(_("E785: complete() can only be used in Insert mode"));
10271 return;
10272 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010273
10274 /* Check for undo allowed here, because if something was already inserted
10275 * the line was already saved for undo and this check isn't done. */
10276 if (!undo_allowed())
10277 return;
10278
Bram Moolenaarade00832006-03-10 21:46:58 +000010279 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10280 {
10281 EMSG(_(e_invarg));
10282 return;
10283 }
10284
10285 startcol = get_tv_number_chk(&argvars[0], NULL);
10286 if (startcol <= 0)
10287 return;
10288
10289 set_completion(startcol - 1, argvars[1].vval.v_list);
10290}
10291
10292/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010293 * "complete_add()" function
10294 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010296f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010297{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010298 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010299}
10300
10301/*
10302 * "complete_check()" function
10303 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010305f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010306{
10307 int saved = RedrawingDisabled;
10308
10309 RedrawingDisabled = 0;
10310 ins_compl_check_keys(0);
10311 rettv->vval.v_number = compl_interrupted;
10312 RedrawingDisabled = saved;
10313}
10314#endif
10315
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316/*
10317 * "confirm(message, buttons[, default [, type]])" function
10318 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010320f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321{
10322#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10323 char_u *message;
10324 char_u *buttons = NULL;
10325 char_u buf[NUMBUFLEN];
10326 char_u buf2[NUMBUFLEN];
10327 int def = 1;
10328 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010329 char_u *typestr;
10330 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010332 message = get_tv_string_chk(&argvars[0]);
10333 if (message == NULL)
10334 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010335 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010337 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10338 if (buttons == NULL)
10339 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010340 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010342 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010343 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010345 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10346 if (typestr == NULL)
10347 error = TRUE;
10348 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010350 switch (TOUPPER_ASC(*typestr))
10351 {
10352 case 'E': type = VIM_ERROR; break;
10353 case 'Q': type = VIM_QUESTION; break;
10354 case 'I': type = VIM_INFO; break;
10355 case 'W': type = VIM_WARNING; break;
10356 case 'G': type = VIM_GENERIC; break;
10357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 }
10359 }
10360 }
10361 }
10362
10363 if (buttons == NULL || *buttons == NUL)
10364 buttons = (char_u *)_("&Ok");
10365
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010366 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010367 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010368 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369#endif
10370}
10371
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010372/*
10373 * "copy()" function
10374 */
10375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010376f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010377{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010378 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010379}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010381#ifdef FEAT_FLOAT
10382/*
10383 * "cos()" function
10384 */
10385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010386f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010387{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010388 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010389
10390 rettv->v_type = VAR_FLOAT;
10391 if (get_float_arg(argvars, &f) == OK)
10392 rettv->vval.v_float = cos(f);
10393 else
10394 rettv->vval.v_float = 0.0;
10395}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010396
10397/*
10398 * "cosh()" function
10399 */
10400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010401f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010402{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010403 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010404
10405 rettv->v_type = VAR_FLOAT;
10406 if (get_float_arg(argvars, &f) == OK)
10407 rettv->vval.v_float = cosh(f);
10408 else
10409 rettv->vval.v_float = 0.0;
10410}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010411#endif
10412
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010414 * "count()" function
10415 */
10416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010417f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010418{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010419 long n = 0;
10420 int ic = FALSE;
10421
Bram Moolenaare9a41262005-01-15 22:18:47 +000010422 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010423 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010424 listitem_T *li;
10425 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010426 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010427
Bram Moolenaare9a41262005-01-15 22:18:47 +000010428 if ((l = argvars[0].vval.v_list) != NULL)
10429 {
10430 li = l->lv_first;
10431 if (argvars[2].v_type != VAR_UNKNOWN)
10432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010433 int error = FALSE;
10434
10435 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010436 if (argvars[3].v_type != VAR_UNKNOWN)
10437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010438 idx = get_tv_number_chk(&argvars[3], &error);
10439 if (!error)
10440 {
10441 li = list_find(l, idx);
10442 if (li == NULL)
10443 EMSGN(_(e_listidx), idx);
10444 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010445 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010446 if (error)
10447 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010448 }
10449
10450 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010451 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010452 ++n;
10453 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010454 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010455 else if (argvars[0].v_type == VAR_DICT)
10456 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010457 int todo;
10458 dict_T *d;
10459 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010460
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010461 if ((d = argvars[0].vval.v_dict) != NULL)
10462 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010463 int error = FALSE;
10464
Bram Moolenaare9a41262005-01-15 22:18:47 +000010465 if (argvars[2].v_type != VAR_UNKNOWN)
10466 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010467 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010468 if (argvars[3].v_type != VAR_UNKNOWN)
10469 EMSG(_(e_invarg));
10470 }
10471
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010472 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010473 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010474 {
10475 if (!HASHITEM_EMPTY(hi))
10476 {
10477 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010478 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010479 ++n;
10480 }
10481 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010482 }
10483 }
10484 else
10485 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010486 rettv->vval.v_number = n;
10487}
10488
10489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10491 *
10492 * Checks the existence of a cscope connection.
10493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010495f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496{
10497#ifdef FEAT_CSCOPE
10498 int num = 0;
10499 char_u *dbpath = NULL;
10500 char_u *prepend = NULL;
10501 char_u buf[NUMBUFLEN];
10502
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010503 if (argvars[0].v_type != VAR_UNKNOWN
10504 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010506 num = (int)get_tv_number(&argvars[0]);
10507 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010508 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010509 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510 }
10511
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010512 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513#endif
10514}
10515
10516/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010517 * "cursor(lnum, col)" function, or
10518 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010520 * Moves the cursor to the specified line and column.
10521 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010524f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525{
10526 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010527#ifdef FEAT_VIRTUALEDIT
10528 long coladd = 0;
10529#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010530 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010532 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010533 if (argvars[1].v_type == VAR_UNKNOWN)
10534 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010535 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010536 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010537
Bram Moolenaar493c1782014-05-28 14:34:46 +020010538 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010539 {
10540 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010541 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010542 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010543 line = pos.lnum;
10544 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010545#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010546 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010547#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010548 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010549 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010550 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010551 set_curswant = FALSE;
10552 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010553 }
10554 else
10555 {
10556 line = get_tv_lnum(argvars);
10557 col = get_tv_number_chk(&argvars[1], NULL);
10558#ifdef FEAT_VIRTUALEDIT
10559 if (argvars[2].v_type != VAR_UNKNOWN)
10560 coladd = get_tv_number_chk(&argvars[2], NULL);
10561#endif
10562 }
10563 if (line < 0 || col < 0
10564#ifdef FEAT_VIRTUALEDIT
10565 || coladd < 0
10566#endif
10567 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010568 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 if (line > 0)
10570 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571 if (col > 0)
10572 curwin->w_cursor.col = col - 1;
10573#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010574 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575#endif
10576
10577 /* Make sure the cursor is in a valid position. */
10578 check_cursor();
10579#ifdef FEAT_MBYTE
10580 /* Correct cursor for multi-byte character. */
10581 if (has_mbyte)
10582 mb_adjust_cursor();
10583#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010584
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010585 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010586 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587}
10588
10589/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010590 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 */
10592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010593f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010595 int noref = 0;
10596
10597 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010598 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010599 if (noref < 0 || noref > 1)
10600 EMSG(_(e_invarg));
10601 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010602 {
10603 current_copyID += COPYID_INC;
10604 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606}
10607
10608/*
10609 * "delete()" function
10610 */
10611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010612f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010614 char_u nbuf[NUMBUFLEN];
10615 char_u *name;
10616 char_u *flags;
10617
10618 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010620 return;
10621
10622 name = get_tv_string(&argvars[0]);
10623 if (name == NULL || *name == NUL)
10624 {
10625 EMSG(_(e_invarg));
10626 return;
10627 }
10628
10629 if (argvars[1].v_type != VAR_UNKNOWN)
10630 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010632 flags = (char_u *)"";
10633
10634 if (*flags == NUL)
10635 /* delete a file */
10636 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10637 else if (STRCMP(flags, "d") == 0)
10638 /* delete an empty directory */
10639 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10640 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010641 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010642 rettv->vval.v_number = delete_recursive(name);
10643 else
10644 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645}
10646
10647/*
10648 * "did_filetype()" function
10649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010651f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652{
10653#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010654 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655#endif
10656}
10657
10658/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010659 * "diff_filler()" function
10660 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010662f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010663{
10664#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010665 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010666#endif
10667}
10668
10669/*
10670 * "diff_hlID()" function
10671 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010673f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010674{
10675#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010676 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010677 static linenr_T prev_lnum = 0;
10678 static int changedtick = 0;
10679 static int fnum = 0;
10680 static int change_start = 0;
10681 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010682 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010683 int filler_lines;
10684 int col;
10685
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010686 if (lnum < 0) /* ignore type error in {lnum} arg */
10687 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010688 if (lnum != prev_lnum
10689 || changedtick != curbuf->b_changedtick
10690 || fnum != curbuf->b_fnum)
10691 {
10692 /* New line, buffer, change: need to get the values. */
10693 filler_lines = diff_check(curwin, lnum);
10694 if (filler_lines < 0)
10695 {
10696 if (filler_lines == -1)
10697 {
10698 change_start = MAXCOL;
10699 change_end = -1;
10700 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10701 hlID = HLF_ADD; /* added line */
10702 else
10703 hlID = HLF_CHD; /* changed line */
10704 }
10705 else
10706 hlID = HLF_ADD; /* added line */
10707 }
10708 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010709 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010710 prev_lnum = lnum;
10711 changedtick = curbuf->b_changedtick;
10712 fnum = curbuf->b_fnum;
10713 }
10714
10715 if (hlID == HLF_CHD || hlID == HLF_TXD)
10716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010717 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010718 if (col >= change_start && col <= change_end)
10719 hlID = HLF_TXD; /* changed text */
10720 else
10721 hlID = HLF_CHD; /* changed line */
10722 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010723 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010724#endif
10725}
10726
10727/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010728 * "disable_char_avail_for_testing({expr})" function
10729 */
10730 static void
10731f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10732{
10733 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10734}
10735
10736/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010737 * "empty({expr})" function
10738 */
10739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010740f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010741{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010742 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010743
10744 switch (argvars[0].v_type)
10745 {
10746 case VAR_STRING:
10747 case VAR_FUNC:
10748 n = argvars[0].vval.v_string == NULL
10749 || *argvars[0].vval.v_string == NUL;
10750 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010751 case VAR_PARTIAL:
10752 n = FALSE;
10753 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010754 case VAR_NUMBER:
10755 n = argvars[0].vval.v_number == 0;
10756 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010757 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010758#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010759 n = argvars[0].vval.v_float == 0.0;
10760 break;
10761#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010762 case VAR_LIST:
10763 n = argvars[0].vval.v_list == NULL
10764 || argvars[0].vval.v_list->lv_first == NULL;
10765 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010766 case VAR_DICT:
10767 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010768 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010769 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010770 case VAR_SPECIAL:
10771 n = argvars[0].vval.v_number != VVAL_TRUE;
10772 break;
10773
Bram Moolenaar835dc632016-02-07 14:27:38 +010010774 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010775#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010776 n = argvars[0].vval.v_job == NULL
10777 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10778 break;
10779#endif
10780 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010781#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010782 n = argvars[0].vval.v_channel == NULL
10783 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010784 break;
10785#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010786 case VAR_UNKNOWN:
10787 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10788 n = TRUE;
10789 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010790 }
10791
10792 rettv->vval.v_number = n;
10793}
10794
10795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796 * "escape({string}, {chars})" function
10797 */
10798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010799f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800{
10801 char_u buf[NUMBUFLEN];
10802
Bram Moolenaar758711c2005-02-02 23:11:38 +000010803 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10804 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010805 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806}
10807
10808/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010809 * "eval()" function
10810 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010812f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010813{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010814 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010815
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010816 s = get_tv_string_chk(&argvars[0]);
10817 if (s != NULL)
10818 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010819
Bram Moolenaar615b9972015-01-14 17:15:05 +010010820 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010821 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10822 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010823 if (p != NULL && !aborting())
10824 EMSG2(_(e_invexpr2), p);
10825 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010826 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010827 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010828 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010829 else if (*s != NUL)
10830 EMSG(_(e_trailing));
10831}
10832
10833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 * "eventhandler()" function
10835 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010837f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010839 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840}
10841
10842/*
10843 * "executable()" function
10844 */
10845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010846f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010848 char_u *name = get_tv_string(&argvars[0]);
10849
10850 /* Check in $PATH and also check directly if there is a directory name. */
10851 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10852 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010853}
10854
10855/*
10856 * "exepath()" function
10857 */
10858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010859f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010860{
10861 char_u *p = NULL;
10862
Bram Moolenaarb5971142015-03-21 17:32:19 +010010863 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010864 rettv->v_type = VAR_STRING;
10865 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866}
10867
10868/*
10869 * "exists()" function
10870 */
10871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010872f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873{
10874 char_u *p;
10875 char_u *name;
10876 int n = FALSE;
10877 int len = 0;
10878
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010879 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 if (*p == '$') /* environment variable */
10881 {
10882 /* first try "normal" environment variables (fast) */
10883 if (mch_getenv(p + 1) != NULL)
10884 n = TRUE;
10885 else
10886 {
10887 /* try expanding things like $VIM and ${HOME} */
10888 p = expand_env_save(p);
10889 if (p != NULL && *p != '$')
10890 n = TRUE;
10891 vim_free(p);
10892 }
10893 }
10894 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010895 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010896 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010897 if (*skipwhite(p) != NUL)
10898 n = FALSE; /* trailing garbage */
10899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 else if (*p == '*') /* internal or user defined function */
10901 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010902 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903 }
10904 else if (*p == ':')
10905 {
10906 n = cmd_exists(p + 1);
10907 }
10908 else if (*p == '#')
10909 {
10910#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010911 if (p[1] == '#')
10912 n = autocmd_supported(p + 2);
10913 else
10914 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915#endif
10916 }
10917 else /* internal variable */
10918 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010919 char_u *tofree;
10920 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010922 /* get_name_len() takes care of expanding curly braces */
10923 name = p;
10924 len = get_name_len(&p, &tofree, TRUE, FALSE);
10925 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010927 if (tofree != NULL)
10928 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010929 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010930 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010932 /* handle d.key, l[idx], f(expr) */
10933 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10934 if (n)
10935 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 }
10937 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010938 if (*p != NUL)
10939 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010941 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 }
10943
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010944 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945}
10946
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010947#ifdef FEAT_FLOAT
10948/*
10949 * "exp()" function
10950 */
10951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010952f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010953{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010954 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010955
10956 rettv->v_type = VAR_FLOAT;
10957 if (get_float_arg(argvars, &f) == OK)
10958 rettv->vval.v_float = exp(f);
10959 else
10960 rettv->vval.v_float = 0.0;
10961}
10962#endif
10963
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964/*
10965 * "expand()" function
10966 */
10967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010968f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969{
10970 char_u *s;
10971 int len;
10972 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010973 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010975 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010976 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010978 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010979 if (argvars[1].v_type != VAR_UNKNOWN
10980 && argvars[2].v_type != VAR_UNKNOWN
10981 && get_tv_number_chk(&argvars[2], &error)
10982 && !error)
10983 {
10984 rettv->v_type = VAR_LIST;
10985 rettv->vval.v_list = NULL;
10986 }
10987
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010988 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989 if (*s == '%' || *s == '#' || *s == '<')
10990 {
10991 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010992 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010994 if (rettv->v_type == VAR_LIST)
10995 {
10996 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10997 list_append_string(rettv->vval.v_list, result, -1);
10998 else
10999 vim_free(result);
11000 }
11001 else
11002 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003 }
11004 else
11005 {
11006 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011007 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011008 if (argvars[1].v_type != VAR_UNKNOWN
11009 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011010 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011011 if (!error)
11012 {
11013 ExpandInit(&xpc);
11014 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011015 if (p_wic)
11016 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011017 if (rettv->v_type == VAR_STRING)
11018 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11019 options, WILD_ALL);
11020 else if (rettv_list_alloc(rettv) != FAIL)
11021 {
11022 int i;
11023
11024 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11025 for (i = 0; i < xpc.xp_numfiles; i++)
11026 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11027 ExpandCleanup(&xpc);
11028 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011029 }
11030 else
11031 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 }
11033}
11034
11035/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011036 * Go over all entries in "d2" and add them to "d1".
11037 * When "action" is "error" then a duplicate key is an error.
11038 * When "action" is "force" then a duplicate key is overwritten.
11039 * Otherwise duplicate keys are ignored ("action" is "keep").
11040 */
11041 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011042dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011043{
11044 dictitem_T *di1;
11045 hashitem_T *hi2;
11046 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011047 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011048
11049 todo = (int)d2->dv_hashtab.ht_used;
11050 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11051 {
11052 if (!HASHITEM_EMPTY(hi2))
11053 {
11054 --todo;
11055 di1 = dict_find(d1, hi2->hi_key, -1);
11056 if (d1->dv_scope != 0)
11057 {
11058 /* Disallow replacing a builtin function in l: and g:.
11059 * Check the key to be valid when adding to any
11060 * scope. */
11061 if (d1->dv_scope == VAR_DEF_SCOPE
11062 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11063 && var_check_func_name(hi2->hi_key,
11064 di1 == NULL))
11065 break;
11066 if (!valid_varname(hi2->hi_key))
11067 break;
11068 }
11069 if (di1 == NULL)
11070 {
11071 di1 = dictitem_copy(HI2DI(hi2));
11072 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11073 dictitem_free(di1);
11074 }
11075 else if (*action == 'e')
11076 {
11077 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11078 break;
11079 }
11080 else if (*action == 'f' && HI2DI(hi2) != di1)
11081 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011082 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11083 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011084 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011085 clear_tv(&di1->di_tv);
11086 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11087 }
11088 }
11089 }
11090}
11091
11092/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011093 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011094 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011095 */
11096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011097f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011098{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011099 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011100
Bram Moolenaare9a41262005-01-15 22:18:47 +000011101 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011102 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011103 list_T *l1, *l2;
11104 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011105 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011106 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011107
Bram Moolenaare9a41262005-01-15 22:18:47 +000011108 l1 = argvars[0].vval.v_list;
11109 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011110 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011111 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011112 {
11113 if (argvars[2].v_type != VAR_UNKNOWN)
11114 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011115 before = get_tv_number_chk(&argvars[2], &error);
11116 if (error)
11117 return; /* type error; errmsg already given */
11118
Bram Moolenaar758711c2005-02-02 23:11:38 +000011119 if (before == l1->lv_len)
11120 item = NULL;
11121 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011122 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011123 item = list_find(l1, before);
11124 if (item == NULL)
11125 {
11126 EMSGN(_(e_listidx), before);
11127 return;
11128 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011129 }
11130 }
11131 else
11132 item = NULL;
11133 list_extend(l1, l2, item);
11134
Bram Moolenaare9a41262005-01-15 22:18:47 +000011135 copy_tv(&argvars[0], rettv);
11136 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011137 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011138 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11139 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011140 dict_T *d1, *d2;
11141 char_u *action;
11142 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011143
11144 d1 = argvars[0].vval.v_dict;
11145 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011146 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011147 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011148 {
11149 /* Check the third argument. */
11150 if (argvars[2].v_type != VAR_UNKNOWN)
11151 {
11152 static char *(av[]) = {"keep", "force", "error"};
11153
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011154 action = get_tv_string_chk(&argvars[2]);
11155 if (action == NULL)
11156 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011157 for (i = 0; i < 3; ++i)
11158 if (STRCMP(action, av[i]) == 0)
11159 break;
11160 if (i == 3)
11161 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011162 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011163 return;
11164 }
11165 }
11166 else
11167 action = (char_u *)"force";
11168
Bram Moolenaara9922d62013-05-30 13:01:18 +020011169 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011170
Bram Moolenaare9a41262005-01-15 22:18:47 +000011171 copy_tv(&argvars[0], rettv);
11172 }
11173 }
11174 else
11175 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011176}
11177
11178/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011179 * "feedkeys()" function
11180 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011182f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011183{
11184 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011185 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011186 char_u *keys, *flags;
11187 char_u nbuf[NUMBUFLEN];
11188 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011189 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011190 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011191
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011192 /* This is not allowed in the sandbox. If the commands would still be
11193 * executed in the sandbox it would be OK, but it probably happens later,
11194 * when "sandbox" is no longer set. */
11195 if (check_secure())
11196 return;
11197
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011198 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011199
11200 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011201 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011202 flags = get_tv_string_buf(&argvars[1], nbuf);
11203 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011204 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011205 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011206 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011207 case 'n': remap = FALSE; break;
11208 case 'm': remap = TRUE; break;
11209 case 't': typed = TRUE; break;
11210 case 'i': insert = TRUE; break;
11211 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011212 }
11213 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011214 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011215
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011216 if (*keys != NUL || execute)
11217 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011218 /* Need to escape K_SPECIAL and CSI before putting the string in the
11219 * typeahead buffer. */
11220 keys_esc = vim_strsave_escape_csi(keys);
11221 if (keys_esc != NULL)
11222 {
11223 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011224 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011225 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011226 if (vgetc_busy)
11227 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011228 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011229 {
11230 int save_msg_scroll = msg_scroll;
11231
11232 /* Avoid a 1 second delay when the keys start Insert mode. */
11233 msg_scroll = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011234 exec_normal(TRUE);
Bram Moolenaar9e496852016-03-11 19:31:47 +010011235 msg_scroll |= save_msg_scroll;
11236 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011237 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011238 }
11239}
11240
11241/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242 * "filereadable()" function
11243 */
11244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011245f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011247 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248 char_u *p;
11249 int n;
11250
Bram Moolenaarc236c162008-07-13 17:41:49 +000011251#ifndef O_NONBLOCK
11252# define O_NONBLOCK 0
11253#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011254 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011255 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11256 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 {
11258 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011259 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 }
11261 else
11262 n = FALSE;
11263
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011264 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265}
11266
11267/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011268 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269 * rights to write into.
11270 */
11271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011272f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011274 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275}
11276
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011277 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011278findfilendir(
11279 typval_T *argvars UNUSED,
11280 typval_T *rettv,
11281 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011282{
11283#ifdef FEAT_SEARCHPATH
11284 char_u *fname;
11285 char_u *fresult = NULL;
11286 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11287 char_u *p;
11288 char_u pathbuf[NUMBUFLEN];
11289 int count = 1;
11290 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011291 int error = FALSE;
11292#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011293
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011294 rettv->vval.v_string = NULL;
11295 rettv->v_type = VAR_STRING;
11296
11297#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011298 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011299
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011300 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011301 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011302 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11303 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011304 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011305 else
11306 {
11307 if (*p != NUL)
11308 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011309
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011310 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011311 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011312 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011313 }
11314
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011315 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11316 error = TRUE;
11317
11318 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011320 do
11321 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011322 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011323 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011324 fresult = find_file_in_path_option(first ? fname : NULL,
11325 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011326 0, first, path,
11327 find_what,
11328 curbuf->b_ffname,
11329 find_what == FINDFILE_DIR
11330 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011331 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011332
11333 if (fresult != NULL && rettv->v_type == VAR_LIST)
11334 list_append_string(rettv->vval.v_list, fresult, -1);
11335
11336 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011337 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011338
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011339 if (rettv->v_type == VAR_STRING)
11340 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011341#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011342}
11343
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011344static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11345static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011346
11347/*
11348 * Implementation of map() and filter().
11349 */
11350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011351filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011352{
11353 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011354 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011355 listitem_T *li, *nli;
11356 list_T *l = NULL;
11357 dictitem_T *di;
11358 hashtab_T *ht;
11359 hashitem_T *hi;
11360 dict_T *d = NULL;
11361 typval_T save_val;
11362 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011363 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011364 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011365 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011366 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011367 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011368 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011369 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011370
Bram Moolenaare9a41262005-01-15 22:18:47 +000011371 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011372 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011373 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011374 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011375 return;
11376 }
11377 else if (argvars[0].v_type == VAR_DICT)
11378 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011379 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011380 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011381 return;
11382 }
11383 else
11384 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011385 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011386 return;
11387 }
11388
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011389 expr = get_tv_string_buf_chk(&argvars[1], buf);
11390 /* On type errors, the preceding call has already displayed an error
11391 * message. Avoid a misleading error message for an empty string that
11392 * was not passed as argument. */
11393 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011394 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011395 prepare_vimvar(VV_VAL, &save_val);
11396 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011397
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011398 /* We reset "did_emsg" to be able to detect whether an error
11399 * occurred during evaluation of the expression. */
11400 save_did_emsg = did_emsg;
11401 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011402
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011403 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011404 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011405 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011406 vimvars[VV_KEY].vv_type = VAR_STRING;
11407
11408 ht = &d->dv_hashtab;
11409 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011410 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011411 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011412 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011413 if (!HASHITEM_EMPTY(hi))
11414 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011415 int r;
11416
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011417 --todo;
11418 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011419 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011420 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11421 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011422 break;
11423 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011424 r = filter_map_one(&di->di_tv, expr, map, &rem);
11425 clear_tv(&vimvars[VV_KEY].vv_tv);
11426 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011427 break;
11428 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011429 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011430 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11431 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011432 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011433 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011434 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011435 }
11436 }
11437 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011438 }
11439 else
11440 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011441 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11442
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011443 for (li = l->lv_first; li != NULL; li = nli)
11444 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011445 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011446 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011447 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011448 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011449 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011450 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011451 break;
11452 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011453 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011454 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011455 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011456 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011457
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011458 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011459 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011460
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011461 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011462 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011463
11464 copy_tv(&argvars[0], rettv);
11465}
11466
11467 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011468filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011469{
Bram Moolenaar33570922005-01-25 22:26:29 +000011470 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011471 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011472 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011473
Bram Moolenaar33570922005-01-25 22:26:29 +000011474 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011475 s = expr;
11476 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011477 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011478 if (*s != NUL) /* check for trailing chars after expr */
11479 {
11480 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011481 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011482 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011483 }
11484 if (map)
11485 {
11486 /* map(): replace the list item value */
11487 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011488 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011489 *tv = rettv;
11490 }
11491 else
11492 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011493 int error = FALSE;
11494
Bram Moolenaare9a41262005-01-15 22:18:47 +000011495 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011496 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011497 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011498 /* On type error, nothing has been removed; return FAIL to stop the
11499 * loop. The error message was given by get_tv_number_chk(). */
11500 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011501 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011502 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011503 retval = OK;
11504theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011505 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011506 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011507}
11508
11509/*
11510 * "filter()" function
11511 */
11512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011513f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011514{
11515 filter_map(argvars, rettv, FALSE);
11516}
11517
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011518/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011519 * "finddir({fname}[, {path}[, {count}]])" function
11520 */
11521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011522f_finddir(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_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011525}
11526
11527/*
11528 * "findfile({fname}[, {path}[, {count}]])" function
11529 */
11530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011531f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011532{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011533 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011534}
11535
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011536#ifdef FEAT_FLOAT
11537/*
11538 * "float2nr({float})" function
11539 */
11540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011541f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011542{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011543 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011544
11545 if (get_float_arg(argvars, &f) == OK)
11546 {
11547 if (f < -0x7fffffff)
11548 rettv->vval.v_number = -0x7fffffff;
11549 else if (f > 0x7fffffff)
11550 rettv->vval.v_number = 0x7fffffff;
11551 else
11552 rettv->vval.v_number = (varnumber_T)f;
11553 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011554}
11555
11556/*
11557 * "floor({float})" function
11558 */
11559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011560f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011561{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011562 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011563
11564 rettv->v_type = VAR_FLOAT;
11565 if (get_float_arg(argvars, &f) == OK)
11566 rettv->vval.v_float = floor(f);
11567 else
11568 rettv->vval.v_float = 0.0;
11569}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011570
11571/*
11572 * "fmod()" function
11573 */
11574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011575f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011576{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011577 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011578
11579 rettv->v_type = VAR_FLOAT;
11580 if (get_float_arg(argvars, &fx) == OK
11581 && get_float_arg(&argvars[1], &fy) == OK)
11582 rettv->vval.v_float = fmod(fx, fy);
11583 else
11584 rettv->vval.v_float = 0.0;
11585}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011586#endif
11587
Bram Moolenaar0d660222005-01-07 21:51:51 +000011588/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011589 * "fnameescape({string})" function
11590 */
11591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011592f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011593{
11594 rettv->vval.v_string = vim_strsave_fnameescape(
11595 get_tv_string(&argvars[0]), FALSE);
11596 rettv->v_type = VAR_STRING;
11597}
11598
11599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600 * "fnamemodify({fname}, {mods})" function
11601 */
11602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011603f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604{
11605 char_u *fname;
11606 char_u *mods;
11607 int usedlen = 0;
11608 int len;
11609 char_u *fbuf = NULL;
11610 char_u buf[NUMBUFLEN];
11611
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011612 fname = get_tv_string_chk(&argvars[0]);
11613 mods = get_tv_string_buf_chk(&argvars[1], buf);
11614 if (fname == NULL || mods == NULL)
11615 fname = NULL;
11616 else
11617 {
11618 len = (int)STRLEN(fname);
11619 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011622 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011623 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011624 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011626 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011627 vim_free(fbuf);
11628}
11629
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011630static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631
11632/*
11633 * "foldclosed()" function
11634 */
11635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011636foldclosed_both(
11637 typval_T *argvars UNUSED,
11638 typval_T *rettv,
11639 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640{
11641#ifdef FEAT_FOLDING
11642 linenr_T lnum;
11643 linenr_T first, last;
11644
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011645 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011646 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11647 {
11648 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11649 {
11650 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011651 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011653 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011654 return;
11655 }
11656 }
11657#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011658 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659}
11660
11661/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011662 * "foldclosed()" function
11663 */
11664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011665f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011666{
11667 foldclosed_both(argvars, rettv, FALSE);
11668}
11669
11670/*
11671 * "foldclosedend()" function
11672 */
11673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011674f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011675{
11676 foldclosed_both(argvars, rettv, TRUE);
11677}
11678
11679/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680 * "foldlevel()" function
11681 */
11682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011683f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684{
11685#ifdef FEAT_FOLDING
11686 linenr_T lnum;
11687
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011688 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011690 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692}
11693
11694/*
11695 * "foldtext()" function
11696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011698f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011699{
11700#ifdef FEAT_FOLDING
11701 linenr_T lnum;
11702 char_u *s;
11703 char_u *r;
11704 int len;
11705 char *txt;
11706#endif
11707
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011708 rettv->v_type = VAR_STRING;
11709 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011711 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11712 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11713 <= curbuf->b_ml.ml_line_count
11714 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715 {
11716 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011717 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11718 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719 {
11720 if (!linewhite(lnum))
11721 break;
11722 ++lnum;
11723 }
11724
11725 /* Find interesting text in this line. */
11726 s = skipwhite(ml_get(lnum));
11727 /* skip C comment-start */
11728 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011729 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011731 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011732 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011733 {
11734 s = skipwhite(ml_get(lnum + 1));
11735 if (*s == '*')
11736 s = skipwhite(s + 1);
11737 }
11738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011739 txt = _("+-%s%3ld lines: ");
11740 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011741 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011742 + 20 /* for %3ld */
11743 + STRLEN(s))); /* concatenated */
11744 if (r != NULL)
11745 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011746 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11747 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11748 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749 len = (int)STRLEN(r);
11750 STRCAT(r, s);
11751 /* remove 'foldmarker' and 'commentstring' */
11752 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011753 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754 }
11755 }
11756#endif
11757}
11758
11759/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011760 * "foldtextresult(lnum)" function
11761 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011763f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011764{
11765#ifdef FEAT_FOLDING
11766 linenr_T lnum;
11767 char_u *text;
11768 char_u buf[51];
11769 foldinfo_T foldinfo;
11770 int fold_count;
11771#endif
11772
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011773 rettv->v_type = VAR_STRING;
11774 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011775#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011776 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011777 /* treat illegal types and illegal string values for {lnum} the same */
11778 if (lnum < 0)
11779 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011780 fold_count = foldedCount(curwin, lnum, &foldinfo);
11781 if (fold_count > 0)
11782 {
11783 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11784 &foldinfo, buf);
11785 if (text == buf)
11786 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011787 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011788 }
11789#endif
11790}
11791
11792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793 * "foreground()" function
11794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011796f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798#ifdef FEAT_GUI
11799 if (gui.in_use)
11800 gui_mch_set_foreground();
11801#else
11802# ifdef WIN32
11803 win32_set_foreground();
11804# endif
11805#endif
11806}
11807
11808/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011809 * "function()" function
11810 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011812f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011813{
11814 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011815 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011816 int use_string = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011817
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011818 if (argvars[0].v_type == VAR_FUNC)
11819 {
11820 /* function(MyFunc, [arg], dict) */
11821 s = argvars[0].vval.v_string;
11822 }
11823 else if (argvars[0].v_type == VAR_PARTIAL
11824 && argvars[0].vval.v_partial != NULL)
11825 /* function(dict.MyFunc, [arg]) */
11826 s = argvars[0].vval.v_partial->pt_name;
11827 else
11828 {
11829 /* function('MyFunc', [arg], dict) */
11830 s = get_tv_string(&argvars[0]);
11831 use_string = TRUE;
11832 }
11833
11834 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011835 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011836 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011837 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
11838 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011839 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011840 else
11841 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011842 int dict_idx = 0;
11843 int arg_idx = 0;
11844 list_T *list = NULL;
11845
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011846 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011847 {
11848 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011849 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011850
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011851 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11852 * also be called from another script. Using trans_function_name()
11853 * would also work, but some plugins depend on the name being
11854 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011855 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011856 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
11857 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011858 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011859 STRCPY(name, sid_buf);
11860 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011861 }
11862 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011863 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011864 name = vim_strsave(s);
11865
11866 if (argvars[1].v_type != VAR_UNKNOWN)
11867 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011868 if (argvars[2].v_type != VAR_UNKNOWN)
11869 {
11870 /* function(name, [args], dict) */
11871 arg_idx = 1;
11872 dict_idx = 2;
11873 }
11874 else if (argvars[1].v_type == VAR_DICT)
11875 /* function(name, dict) */
11876 dict_idx = 1;
11877 else
11878 /* function(name, [args]) */
11879 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010011880 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011881 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011882 if (argvars[dict_idx].v_type != VAR_DICT)
11883 {
11884 EMSG(_("E922: expected a dict"));
11885 vim_free(name);
11886 return;
11887 }
11888 if (argvars[dict_idx].vval.v_dict == NULL)
11889 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011890 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011891 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011892 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011893 if (argvars[arg_idx].v_type != VAR_LIST)
11894 {
11895 EMSG(_("E923: Second argument of function() must be a list or a dict"));
11896 vim_free(name);
11897 return;
11898 }
11899 list = argvars[arg_idx].vval.v_list;
11900 if (list == NULL || list->lv_len == 0)
11901 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011902 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011903 }
11904 if (dict_idx > 0 || arg_idx > 0)
11905 {
11906 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011907
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011908 if (pt != NULL)
11909 {
11910 if (arg_idx > 0)
11911 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011912 listitem_T *li;
11913 int i = 0;
11914
11915 pt->pt_argv = (typval_T *)alloc(
11916 sizeof(typval_T) * list->lv_len);
11917 if (pt->pt_argv == NULL)
11918 {
11919 vim_free(pt);
11920 vim_free(name);
11921 return;
11922 }
11923 else
11924 {
11925 pt->pt_argc = list->lv_len;
11926 for (li = list->lv_first; li != NULL; li = li->li_next)
11927 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
11928 }
11929 }
11930
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010011931 /* For "function(dict.func, [], dict)" and "func" is a partial
11932 * use "dict". That is backwards compatible. */
11933 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 }
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010011938 else if (argvars[0].v_type == VAR_PARTIAL)
11939 {
11940 pt->pt_dict = argvars[0].vval.v_partial->pt_dict;
11941 ++pt->pt_dict->dv_refcount;
11942 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011943
11944 pt->pt_refcount = 1;
11945 pt->pt_name = name;
11946 func_ref(pt->pt_name);
11947 }
11948 rettv->v_type = VAR_PARTIAL;
11949 rettv->vval.v_partial = pt;
11950 }
11951 else
11952 {
11953 rettv->v_type = VAR_FUNC;
11954 rettv->vval.v_string = name;
11955 func_ref(name);
11956 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011957 }
11958}
11959
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011960 static void
11961partial_free(partial_T *pt)
11962{
11963 int i;
11964
11965 for (i = 0; i < pt->pt_argc; ++i)
11966 clear_tv(&pt->pt_argv[i]);
11967 vim_free(pt->pt_argv);
11968 func_unref(pt->pt_name);
11969 vim_free(pt->pt_name);
11970 vim_free(pt);
11971}
11972
11973/*
11974 * Unreference a closure: decrement the reference count and free it when it
11975 * becomes zero.
11976 */
11977 void
11978partial_unref(partial_T *pt)
11979{
11980 if (pt != NULL && --pt->pt_refcount <= 0)
11981 partial_free(pt);
11982}
11983
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011984/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011985 * "garbagecollect()" function
11986 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011988f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011989{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011990 /* This is postponed until we are back at the toplevel, because we may be
11991 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11992 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011993
11994 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11995 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011996}
11997
11998/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011999 * "get()" function
12000 */
12001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012002f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012003{
Bram Moolenaar33570922005-01-25 22:26:29 +000012004 listitem_T *li;
12005 list_T *l;
12006 dictitem_T *di;
12007 dict_T *d;
12008 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012009
Bram Moolenaare9a41262005-01-15 22:18:47 +000012010 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012011 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012012 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012013 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012014 int error = FALSE;
12015
12016 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12017 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012018 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012019 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012020 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012021 else if (argvars[0].v_type == VAR_DICT)
12022 {
12023 if ((d = argvars[0].vval.v_dict) != NULL)
12024 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012025 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012026 if (di != NULL)
12027 tv = &di->di_tv;
12028 }
12029 }
12030 else
12031 EMSG2(_(e_listdictarg), "get()");
12032
12033 if (tv == NULL)
12034 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012035 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012036 copy_tv(&argvars[2], rettv);
12037 }
12038 else
12039 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012040}
12041
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012042static 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 +000012043
12044/*
12045 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012046 * Return a range (from start to end) of lines in rettv from the specified
12047 * buffer.
12048 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012049 */
12050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012051get_buffer_lines(
12052 buf_T *buf,
12053 linenr_T start,
12054 linenr_T end,
12055 int retlist,
12056 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012057{
12058 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012059
Bram Moolenaar959a1432013-12-14 12:17:38 +010012060 rettv->v_type = VAR_STRING;
12061 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012062 if (retlist && rettv_list_alloc(rettv) == FAIL)
12063 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012064
12065 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12066 return;
12067
12068 if (!retlist)
12069 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012070 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12071 p = ml_get_buf(buf, start, FALSE);
12072 else
12073 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012074 rettv->vval.v_string = vim_strsave(p);
12075 }
12076 else
12077 {
12078 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012079 return;
12080
12081 if (start < 1)
12082 start = 1;
12083 if (end > buf->b_ml.ml_line_count)
12084 end = buf->b_ml.ml_line_count;
12085 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012086 if (list_append_string(rettv->vval.v_list,
12087 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012088 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012089 }
12090}
12091
12092/*
12093 * "getbufline()" function
12094 */
12095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012096f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012097{
12098 linenr_T lnum;
12099 linenr_T end;
12100 buf_T *buf;
12101
12102 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12103 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012104 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012105 --emsg_off;
12106
Bram Moolenaar661b1822005-07-28 22:36:45 +000012107 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012108 if (argvars[2].v_type == VAR_UNKNOWN)
12109 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012110 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012111 end = get_tv_lnum_buf(&argvars[2], buf);
12112
Bram Moolenaar342337a2005-07-21 21:11:17 +000012113 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012114}
12115
Bram Moolenaar0d660222005-01-07 21:51:51 +000012116/*
12117 * "getbufvar()" function
12118 */
12119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012120f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012121{
12122 buf_T *buf;
12123 buf_T *save_curbuf;
12124 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012125 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012126 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012127
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012128 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12129 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012130 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012131 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012132
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012133 rettv->v_type = VAR_STRING;
12134 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012135
12136 if (buf != NULL && varname != NULL)
12137 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012138 /* set curbuf to be our buf, temporarily */
12139 save_curbuf = curbuf;
12140 curbuf = buf;
12141
Bram Moolenaar0d660222005-01-07 21:51:51 +000012142 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012143 {
12144 if (get_option_tv(&varname, rettv, TRUE) == OK)
12145 done = TRUE;
12146 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012147 else if (STRCMP(varname, "changedtick") == 0)
12148 {
12149 rettv->v_type = VAR_NUMBER;
12150 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012151 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012152 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012153 else
12154 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012155 /* Look up the variable. */
12156 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12157 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12158 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012159 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012160 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012161 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012162 done = TRUE;
12163 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012164 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012165
12166 /* restore previous notion of curbuf */
12167 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012168 }
12169
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012170 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12171 /* use the default value */
12172 copy_tv(&argvars[2], rettv);
12173
Bram Moolenaar0d660222005-01-07 21:51:51 +000012174 --emsg_off;
12175}
12176
12177/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012178 * "getchar()" function
12179 */
12180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012181f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012182{
12183 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012184 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012186 /* Position the cursor. Needed after a message that ends in a space. */
12187 windgoto(msg_row, msg_col);
12188
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189 ++no_mapping;
12190 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012191 for (;;)
12192 {
12193 if (argvars[0].v_type == VAR_UNKNOWN)
12194 /* getchar(): blocking wait. */
12195 n = safe_vgetc();
12196 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12197 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012198 n = vpeekc_any();
12199 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012200 /* illegal argument or getchar(0) and no char avail: return zero */
12201 n = 0;
12202 else
12203 /* getchar(0) and char avail: return char */
12204 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012205
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012206 if (n == K_IGNORE)
12207 continue;
12208 break;
12209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012210 --no_mapping;
12211 --allow_keys;
12212
Bram Moolenaar219b8702006-11-01 14:32:36 +000012213 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12214 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12215 vimvars[VV_MOUSE_COL].vv_nr = 0;
12216
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012217 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218 if (IS_SPECIAL(n) || mod_mask != 0)
12219 {
12220 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12221 int i = 0;
12222
12223 /* Turn a special key into three bytes, plus modifier. */
12224 if (mod_mask != 0)
12225 {
12226 temp[i++] = K_SPECIAL;
12227 temp[i++] = KS_MODIFIER;
12228 temp[i++] = mod_mask;
12229 }
12230 if (IS_SPECIAL(n))
12231 {
12232 temp[i++] = K_SPECIAL;
12233 temp[i++] = K_SECOND(n);
12234 temp[i++] = K_THIRD(n);
12235 }
12236#ifdef FEAT_MBYTE
12237 else if (has_mbyte)
12238 i += (*mb_char2bytes)(n, temp + i);
12239#endif
12240 else
12241 temp[i++] = n;
12242 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012243 rettv->v_type = VAR_STRING;
12244 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012245
12246#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012247 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012248 {
12249 int row = mouse_row;
12250 int col = mouse_col;
12251 win_T *win;
12252 linenr_T lnum;
12253# ifdef FEAT_WINDOWS
12254 win_T *wp;
12255# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012256 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012257
12258 if (row >= 0 && col >= 0)
12259 {
12260 /* Find the window at the mouse coordinates and compute the
12261 * text position. */
12262 win = mouse_find_win(&row, &col);
12263 (void)mouse_comp_pos(win, &row, &col, &lnum);
12264# ifdef FEAT_WINDOWS
12265 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012266 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012267# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012268 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012269 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12270 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12271 }
12272 }
12273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012274 }
12275}
12276
12277/*
12278 * "getcharmod()" function
12279 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012281f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012282{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012283 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284}
12285
12286/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012287 * "getcharsearch()" function
12288 */
12289 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012290f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012291{
12292 if (rettv_dict_alloc(rettv) != FAIL)
12293 {
12294 dict_T *dict = rettv->vval.v_dict;
12295
12296 dict_add_nr_str(dict, "char", 0L, last_csearch());
12297 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12298 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12299 }
12300}
12301
12302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303 * "getcmdline()" function
12304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012306f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012307{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012308 rettv->v_type = VAR_STRING;
12309 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310}
12311
12312/*
12313 * "getcmdpos()" function
12314 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012316f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012317{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012318 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012319}
12320
12321/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012322 * "getcmdtype()" function
12323 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012325f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012326{
12327 rettv->v_type = VAR_STRING;
12328 rettv->vval.v_string = alloc(2);
12329 if (rettv->vval.v_string != NULL)
12330 {
12331 rettv->vval.v_string[0] = get_cmdline_type();
12332 rettv->vval.v_string[1] = NUL;
12333 }
12334}
12335
12336/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012337 * "getcmdwintype()" function
12338 */
12339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012340f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012341{
12342 rettv->v_type = VAR_STRING;
12343 rettv->vval.v_string = NULL;
12344#ifdef FEAT_CMDWIN
12345 rettv->vval.v_string = alloc(2);
12346 if (rettv->vval.v_string != NULL)
12347 {
12348 rettv->vval.v_string[0] = cmdwin_type;
12349 rettv->vval.v_string[1] = NUL;
12350 }
12351#endif
12352}
12353
12354/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355 * "getcwd()" function
12356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012358f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012359{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012360 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012361 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012363 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012364 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012365
12366 wp = find_tabwin(&argvars[0], &argvars[1]);
12367 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012369 if (wp->w_localdir != NULL)
12370 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12371 else if(globaldir != NULL)
12372 rettv->vval.v_string = vim_strsave(globaldir);
12373 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012374 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012375 cwd = alloc(MAXPATHL);
12376 if (cwd != NULL)
12377 {
12378 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12379 rettv->vval.v_string = vim_strsave(cwd);
12380 vim_free(cwd);
12381 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012382 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012383#ifdef BACKSLASH_IN_FILENAME
12384 if (rettv->vval.v_string != NULL)
12385 slash_adjust(rettv->vval.v_string);
12386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387 }
12388}
12389
12390/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012391 * "getfontname()" function
12392 */
12393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012394f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012395{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012396 rettv->v_type = VAR_STRING;
12397 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012398#ifdef FEAT_GUI
12399 if (gui.in_use)
12400 {
12401 GuiFont font;
12402 char_u *name = NULL;
12403
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012404 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012405 {
12406 /* Get the "Normal" font. Either the name saved by
12407 * hl_set_font_name() or from the font ID. */
12408 font = gui.norm_font;
12409 name = hl_get_font_name();
12410 }
12411 else
12412 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012413 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012414 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12415 return;
12416 font = gui_mch_get_font(name, FALSE);
12417 if (font == NOFONT)
12418 return; /* Invalid font name, return empty string. */
12419 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012420 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012421 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012422 gui_mch_free_font(font);
12423 }
12424#endif
12425}
12426
12427/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012428 * "getfperm({fname})" function
12429 */
12430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012431f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012432{
12433 char_u *fname;
12434 struct stat st;
12435 char_u *perm = NULL;
12436 char_u flags[] = "rwx";
12437 int i;
12438
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012439 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012440
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012441 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012442 if (mch_stat((char *)fname, &st) >= 0)
12443 {
12444 perm = vim_strsave((char_u *)"---------");
12445 if (perm != NULL)
12446 {
12447 for (i = 0; i < 9; i++)
12448 {
12449 if (st.st_mode & (1 << (8 - i)))
12450 perm[i] = flags[i % 3];
12451 }
12452 }
12453 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012454 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012455}
12456
12457/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012458 * "getfsize({fname})" function
12459 */
12460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012461f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012462{
12463 char_u *fname;
12464 struct stat st;
12465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012466 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012468 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012469
12470 if (mch_stat((char *)fname, &st) >= 0)
12471 {
12472 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012473 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012474 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012476 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012477
12478 /* non-perfect check for overflow */
12479 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12480 rettv->vval.v_number = -2;
12481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482 }
12483 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012484 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485}
12486
12487/*
12488 * "getftime({fname})" function
12489 */
12490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012491f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012492{
12493 char_u *fname;
12494 struct stat st;
12495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012496 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012497
12498 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012499 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012501 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502}
12503
12504/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012505 * "getftype({fname})" function
12506 */
12507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012508f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012509{
12510 char_u *fname;
12511 struct stat st;
12512 char_u *type = NULL;
12513 char *t;
12514
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012515 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012516
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012517 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012518 if (mch_lstat((char *)fname, &st) >= 0)
12519 {
12520#ifdef S_ISREG
12521 if (S_ISREG(st.st_mode))
12522 t = "file";
12523 else if (S_ISDIR(st.st_mode))
12524 t = "dir";
12525# ifdef S_ISLNK
12526 else if (S_ISLNK(st.st_mode))
12527 t = "link";
12528# endif
12529# ifdef S_ISBLK
12530 else if (S_ISBLK(st.st_mode))
12531 t = "bdev";
12532# endif
12533# ifdef S_ISCHR
12534 else if (S_ISCHR(st.st_mode))
12535 t = "cdev";
12536# endif
12537# ifdef S_ISFIFO
12538 else if (S_ISFIFO(st.st_mode))
12539 t = "fifo";
12540# endif
12541# ifdef S_ISSOCK
12542 else if (S_ISSOCK(st.st_mode))
12543 t = "fifo";
12544# endif
12545 else
12546 t = "other";
12547#else
12548# ifdef S_IFMT
12549 switch (st.st_mode & S_IFMT)
12550 {
12551 case S_IFREG: t = "file"; break;
12552 case S_IFDIR: t = "dir"; break;
12553# ifdef S_IFLNK
12554 case S_IFLNK: t = "link"; break;
12555# endif
12556# ifdef S_IFBLK
12557 case S_IFBLK: t = "bdev"; break;
12558# endif
12559# ifdef S_IFCHR
12560 case S_IFCHR: t = "cdev"; break;
12561# endif
12562# ifdef S_IFIFO
12563 case S_IFIFO: t = "fifo"; break;
12564# endif
12565# ifdef S_IFSOCK
12566 case S_IFSOCK: t = "socket"; break;
12567# endif
12568 default: t = "other";
12569 }
12570# else
12571 if (mch_isdir(fname))
12572 t = "dir";
12573 else
12574 t = "file";
12575# endif
12576#endif
12577 type = vim_strsave((char_u *)t);
12578 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012579 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012580}
12581
12582/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012583 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012584 */
12585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012586f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012587{
12588 linenr_T lnum;
12589 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012590 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012591
12592 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012593 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012594 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012595 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012596 retlist = FALSE;
12597 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012598 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012599 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012600 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012601 retlist = TRUE;
12602 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012603
Bram Moolenaar342337a2005-07-21 21:11:17 +000012604 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012605}
12606
12607/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012608 * "getmatches()" function
12609 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012611f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012612{
12613#ifdef FEAT_SEARCH_EXTRA
12614 dict_T *dict;
12615 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012616 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012617
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012618 if (rettv_list_alloc(rettv) == OK)
12619 {
12620 while (cur != NULL)
12621 {
12622 dict = dict_alloc();
12623 if (dict == NULL)
12624 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012625 if (cur->match.regprog == NULL)
12626 {
12627 /* match added with matchaddpos() */
12628 for (i = 0; i < MAXPOSMATCH; ++i)
12629 {
12630 llpos_T *llpos;
12631 char buf[6];
12632 list_T *l;
12633
12634 llpos = &cur->pos.pos[i];
12635 if (llpos->lnum == 0)
12636 break;
12637 l = list_alloc();
12638 if (l == NULL)
12639 break;
12640 list_append_number(l, (varnumber_T)llpos->lnum);
12641 if (llpos->col > 0)
12642 {
12643 list_append_number(l, (varnumber_T)llpos->col);
12644 list_append_number(l, (varnumber_T)llpos->len);
12645 }
12646 sprintf(buf, "pos%d", i + 1);
12647 dict_add_list(dict, buf, l);
12648 }
12649 }
12650 else
12651 {
12652 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12653 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012654 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012655 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12656 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012657# ifdef FEAT_CONCEAL
12658 if (cur->conceal_char)
12659 {
12660 char_u buf[MB_MAXBYTES + 1];
12661
12662 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12663 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12664 }
12665# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012666 list_append_dict(rettv->vval.v_list, dict);
12667 cur = cur->next;
12668 }
12669 }
12670#endif
12671}
12672
12673/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012674 * "getpid()" function
12675 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012677f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012678{
12679 rettv->vval.v_number = mch_get_pid();
12680}
12681
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012682static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012683
12684/*
12685 * "getcurpos()" function
12686 */
12687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012688f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012689{
12690 getpos_both(argvars, rettv, TRUE);
12691}
12692
Bram Moolenaar18081e32008-02-20 19:11:07 +000012693/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012694 * "getpos(string)" function
12695 */
12696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012697f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012698{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012699 getpos_both(argvars, rettv, FALSE);
12700}
12701
12702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012703getpos_both(
12704 typval_T *argvars,
12705 typval_T *rettv,
12706 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012707{
Bram Moolenaara5525202006-03-02 22:52:09 +000012708 pos_T *fp;
12709 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012710 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012711
12712 if (rettv_list_alloc(rettv) == OK)
12713 {
12714 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012715 if (getcurpos)
12716 fp = &curwin->w_cursor;
12717 else
12718 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012719 if (fnum != -1)
12720 list_append_number(l, (varnumber_T)fnum);
12721 else
12722 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012723 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12724 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012725 list_append_number(l, (fp != NULL)
12726 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012727 : (varnumber_T)0);
12728 list_append_number(l,
12729#ifdef FEAT_VIRTUALEDIT
12730 (fp != NULL) ? (varnumber_T)fp->coladd :
12731#endif
12732 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012733 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012734 {
12735 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012736 list_append_number(l, curwin->w_curswant == MAXCOL ?
12737 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012738 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012739 }
12740 else
12741 rettv->vval.v_number = FALSE;
12742}
12743
12744/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012745 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012746 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012748f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012749{
12750#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012751 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012752#endif
12753
Bram Moolenaar2641f772005-03-25 21:58:17 +000012754#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012755 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012756 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012757 wp = NULL;
12758 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12759 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012760 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012761 if (wp == NULL)
12762 return;
12763 }
12764
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012765 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012766 }
12767#endif
12768}
12769
12770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771 * "getreg()" function
12772 */
12773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012774f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775{
12776 char_u *strregname;
12777 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012778 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012779 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012780 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012782 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012784 strregname = get_tv_string_chk(&argvars[0]);
12785 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012786 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012787 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012788 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012789 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12790 return_list = get_tv_number_chk(&argvars[2], &error);
12791 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012794 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012795
12796 if (error)
12797 return;
12798
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799 regname = (strregname == NULL ? '"' : *strregname);
12800 if (regname == 0)
12801 regname = '"';
12802
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012803 if (return_list)
12804 {
12805 rettv->v_type = VAR_LIST;
12806 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12807 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012808 if (rettv->vval.v_list != NULL)
12809 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012810 }
12811 else
12812 {
12813 rettv->v_type = VAR_STRING;
12814 rettv->vval.v_string = get_reg_contents(regname,
12815 arg2 ? GREG_EXPR_SRC : 0);
12816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817}
12818
12819/*
12820 * "getregtype()" function
12821 */
12822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012823f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824{
12825 char_u *strregname;
12826 int regname;
12827 char_u buf[NUMBUFLEN + 2];
12828 long reglen = 0;
12829
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012830 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012831 {
12832 strregname = get_tv_string_chk(&argvars[0]);
12833 if (strregname == NULL) /* type error; errmsg already given */
12834 {
12835 rettv->v_type = VAR_STRING;
12836 rettv->vval.v_string = NULL;
12837 return;
12838 }
12839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840 else
12841 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012842 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843
12844 regname = (strregname == NULL ? '"' : *strregname);
12845 if (regname == 0)
12846 regname = '"';
12847
12848 buf[0] = NUL;
12849 buf[1] = NUL;
12850 switch (get_reg_type(regname, &reglen))
12851 {
12852 case MLINE: buf[0] = 'V'; break;
12853 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854 case MBLOCK:
12855 buf[0] = Ctrl_V;
12856 sprintf((char *)buf + 1, "%ld", reglen + 1);
12857 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012859 rettv->v_type = VAR_STRING;
12860 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012861}
12862
12863/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012864 * "gettabvar()" function
12865 */
12866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012867f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012868{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012869 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012870 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012871 dictitem_T *v;
12872 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012873 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012874
12875 rettv->v_type = VAR_STRING;
12876 rettv->vval.v_string = NULL;
12877
12878 varname = get_tv_string_chk(&argvars[1]);
12879 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12880 if (tp != NULL && varname != NULL)
12881 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012882 /* Set tp to be our tabpage, temporarily. Also set the window to the
12883 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012884 if (switch_win(&oldcurwin, &oldtabpage,
12885 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012886 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012887 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012888 /* look up the variable */
12889 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12890 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12891 if (v != NULL)
12892 {
12893 copy_tv(&v->di_tv, rettv);
12894 done = TRUE;
12895 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012896 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012897
12898 /* restore previous notion of curwin */
12899 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012900 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012901
12902 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012903 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012904}
12905
12906/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012907 * "gettabwinvar()" function
12908 */
12909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012910f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012911{
12912 getwinvar(argvars, rettv, 1);
12913}
12914
12915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916 * "getwinposx()" function
12917 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012919f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012920{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012921 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922#ifdef FEAT_GUI
12923 if (gui.in_use)
12924 {
12925 int x, y;
12926
12927 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012928 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929 }
12930#endif
12931}
12932
12933/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010012934 * "win_findbuf()" function
12935 */
12936 static void
12937f_win_findbuf(typval_T *argvars, typval_T *rettv)
12938{
12939 if (rettv_list_alloc(rettv) != FAIL)
12940 win_findbuf(argvars, rettv->vval.v_list);
12941}
12942
12943/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010012944 * "win_getid()" function
12945 */
12946 static void
12947f_win_getid(typval_T *argvars, typval_T *rettv)
12948{
12949 rettv->vval.v_number = win_getid(argvars);
12950}
12951
12952/*
12953 * "win_gotoid()" function
12954 */
12955 static void
12956f_win_gotoid(typval_T *argvars, typval_T *rettv)
12957{
12958 rettv->vval.v_number = win_gotoid(argvars);
12959}
12960
12961/*
12962 * "win_id2tabwin()" function
12963 */
12964 static void
12965f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
12966{
12967 if (rettv_list_alloc(rettv) != FAIL)
12968 win_id2tabwin(argvars, rettv->vval.v_list);
12969}
12970
12971/*
12972 * "win_id2win()" function
12973 */
12974 static void
12975f_win_id2win(typval_T *argvars, typval_T *rettv)
12976{
12977 rettv->vval.v_number = win_id2win(argvars);
12978}
12979
12980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012981 * "getwinposy()" function
12982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012984f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012986 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012987#ifdef FEAT_GUI
12988 if (gui.in_use)
12989 {
12990 int x, y;
12991
12992 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012993 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012994 }
12995#endif
12996}
12997
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012998/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012999 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013000 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013001 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013002find_win_by_nr(
13003 typval_T *vp,
13004 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013005{
13006#ifdef FEAT_WINDOWS
13007 win_T *wp;
13008#endif
13009 int nr;
13010
13011 nr = get_tv_number_chk(vp, NULL);
13012
13013#ifdef FEAT_WINDOWS
13014 if (nr < 0)
13015 return NULL;
13016 if (nr == 0)
13017 return curwin;
13018
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013019 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13020 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013021 if (--nr <= 0)
13022 break;
13023 return wp;
13024#else
13025 if (nr == 0 || nr == 1)
13026 return curwin;
13027 return NULL;
13028#endif
13029}
13030
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013032 * Find window specified by "wvp" in tabpage "tvp".
13033 */
13034 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013035find_tabwin(
13036 typval_T *wvp, /* VAR_UNKNOWN for current window */
13037 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013038{
13039 win_T *wp = NULL;
13040 tabpage_T *tp = NULL;
13041 long n;
13042
13043 if (wvp->v_type != VAR_UNKNOWN)
13044 {
13045 if (tvp->v_type != VAR_UNKNOWN)
13046 {
13047 n = get_tv_number(tvp);
13048 if (n >= 0)
13049 tp = find_tabpage(n);
13050 }
13051 else
13052 tp = curtab;
13053
13054 if (tp != NULL)
13055 wp = find_win_by_nr(wvp, tp);
13056 }
13057 else
13058 wp = curwin;
13059
13060 return wp;
13061}
13062
13063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013064 * "getwinvar()" function
13065 */
13066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013067f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013068{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013069 getwinvar(argvars, rettv, 0);
13070}
13071
13072/*
13073 * getwinvar() and gettabwinvar()
13074 */
13075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013076getwinvar(
13077 typval_T *argvars,
13078 typval_T *rettv,
13079 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013080{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013081 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013082 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013083 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013084 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013085 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013086#ifdef FEAT_WINDOWS
13087 win_T *oldcurwin;
13088 tabpage_T *oldtabpage;
13089 int need_switch_win;
13090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013091
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013092#ifdef FEAT_WINDOWS
13093 if (off == 1)
13094 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13095 else
13096 tp = curtab;
13097#endif
13098 win = find_win_by_nr(&argvars[off], tp);
13099 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013100 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013101
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013102 rettv->v_type = VAR_STRING;
13103 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013104
13105 if (win != NULL && varname != NULL)
13106 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013107#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013108 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013109 * otherwise the window is not valid. Only do this when needed,
13110 * autocommands get blocked. */
13111 need_switch_win = !(tp == curtab && win == curwin);
13112 if (!need_switch_win
13113 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13114#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013115 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013116 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013117 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013118 if (get_option_tv(&varname, rettv, 1) == OK)
13119 done = TRUE;
13120 }
13121 else
13122 {
13123 /* Look up the variable. */
13124 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13125 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13126 varname, FALSE);
13127 if (v != NULL)
13128 {
13129 copy_tv(&v->di_tv, rettv);
13130 done = TRUE;
13131 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013133 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013134
Bram Moolenaarba117c22015-09-29 16:53:22 +020013135#ifdef FEAT_WINDOWS
13136 if (need_switch_win)
13137 /* restore previous notion of curwin */
13138 restore_win(oldcurwin, oldtabpage, TRUE);
13139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140 }
13141
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013142 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13143 /* use the default return value */
13144 copy_tv(&argvars[off + 2], rettv);
13145
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146 --emsg_off;
13147}
13148
13149/*
13150 * "glob()" function
13151 */
13152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013153f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013155 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013156 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013157 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013159 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013160 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013161 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013162 if (argvars[1].v_type != VAR_UNKNOWN)
13163 {
13164 if (get_tv_number_chk(&argvars[1], &error))
13165 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013166 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013167 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013168 if (get_tv_number_chk(&argvars[2], &error))
13169 {
13170 rettv->v_type = VAR_LIST;
13171 rettv->vval.v_list = NULL;
13172 }
13173 if (argvars[3].v_type != VAR_UNKNOWN
13174 && get_tv_number_chk(&argvars[3], &error))
13175 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013176 }
13177 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013178 if (!error)
13179 {
13180 ExpandInit(&xpc);
13181 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013182 if (p_wic)
13183 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013184 if (rettv->v_type == VAR_STRING)
13185 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013186 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013187 else if (rettv_list_alloc(rettv) != FAIL)
13188 {
13189 int i;
13190
13191 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13192 NULL, options, WILD_ALL_KEEP);
13193 for (i = 0; i < xpc.xp_numfiles; i++)
13194 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13195
13196 ExpandCleanup(&xpc);
13197 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013198 }
13199 else
13200 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201}
13202
13203/*
13204 * "globpath()" function
13205 */
13206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013207f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013208{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013209 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013211 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013212 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013213 garray_T ga;
13214 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013216 /* When the optional second argument is non-zero, don't remove matches
13217 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013218 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013219 if (argvars[2].v_type != VAR_UNKNOWN)
13220 {
13221 if (get_tv_number_chk(&argvars[2], &error))
13222 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013223 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013224 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013225 if (get_tv_number_chk(&argvars[3], &error))
13226 {
13227 rettv->v_type = VAR_LIST;
13228 rettv->vval.v_list = NULL;
13229 }
13230 if (argvars[4].v_type != VAR_UNKNOWN
13231 && get_tv_number_chk(&argvars[4], &error))
13232 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013233 }
13234 }
13235 if (file != NULL && !error)
13236 {
13237 ga_init2(&ga, (int)sizeof(char_u *), 10);
13238 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13239 if (rettv->v_type == VAR_STRING)
13240 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13241 else if (rettv_list_alloc(rettv) != FAIL)
13242 for (i = 0; i < ga.ga_len; ++i)
13243 list_append_string(rettv->vval.v_list,
13244 ((char_u **)(ga.ga_data))[i], -1);
13245 ga_clear_strings(&ga);
13246 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013247 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013248 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249}
13250
13251/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013252 * "glob2regpat()" function
13253 */
13254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013255f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013256{
13257 char_u *pat = get_tv_string_chk(&argvars[0]);
13258
13259 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013260 rettv->vval.v_string = (pat == NULL)
13261 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013262}
13263
13264/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265 * "has()" function
13266 */
13267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013268f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013269{
13270 int i;
13271 char_u *name;
13272 int n = FALSE;
13273 static char *(has_list[]) =
13274 {
13275#ifdef AMIGA
13276 "amiga",
13277# ifdef FEAT_ARP
13278 "arp",
13279# endif
13280#endif
13281#ifdef __BEOS__
13282 "beos",
13283#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013284#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285 "mac",
13286#endif
13287#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013288 "macunix", /* built with 'darwin' enabled */
13289#endif
13290#if defined(__APPLE__) && __APPLE__ == 1
13291 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293#ifdef __QNX__
13294 "qnx",
13295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296#ifdef UNIX
13297 "unix",
13298#endif
13299#ifdef VMS
13300 "vms",
13301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302#ifdef WIN32
13303 "win32",
13304#endif
13305#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13306 "win32unix",
13307#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013308#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309 "win64",
13310#endif
13311#ifdef EBCDIC
13312 "ebcdic",
13313#endif
13314#ifndef CASE_INSENSITIVE_FILENAME
13315 "fname_case",
13316#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013317#ifdef HAVE_ACL
13318 "acl",
13319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320#ifdef FEAT_ARABIC
13321 "arabic",
13322#endif
13323#ifdef FEAT_AUTOCMD
13324 "autocmd",
13325#endif
13326#ifdef FEAT_BEVAL
13327 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013328# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13329 "balloon_multiline",
13330# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331#endif
13332#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13333 "builtin_terms",
13334# ifdef ALL_BUILTIN_TCAPS
13335 "all_builtin_terms",
13336# endif
13337#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013338#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13339 || defined(FEAT_GUI_W32) \
13340 || defined(FEAT_GUI_MOTIF))
13341 "browsefilter",
13342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343#ifdef FEAT_BYTEOFF
13344 "byte_offset",
13345#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013346#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013347 "channel",
13348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349#ifdef FEAT_CINDENT
13350 "cindent",
13351#endif
13352#ifdef FEAT_CLIENTSERVER
13353 "clientserver",
13354#endif
13355#ifdef FEAT_CLIPBOARD
13356 "clipboard",
13357#endif
13358#ifdef FEAT_CMDL_COMPL
13359 "cmdline_compl",
13360#endif
13361#ifdef FEAT_CMDHIST
13362 "cmdline_hist",
13363#endif
13364#ifdef FEAT_COMMENTS
13365 "comments",
13366#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013367#ifdef FEAT_CONCEAL
13368 "conceal",
13369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013370#ifdef FEAT_CRYPT
13371 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013372 "crypt-blowfish",
13373 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013374#endif
13375#ifdef FEAT_CSCOPE
13376 "cscope",
13377#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013378#ifdef FEAT_CURSORBIND
13379 "cursorbind",
13380#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013381#ifdef CURSOR_SHAPE
13382 "cursorshape",
13383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384#ifdef DEBUG
13385 "debug",
13386#endif
13387#ifdef FEAT_CON_DIALOG
13388 "dialog_con",
13389#endif
13390#ifdef FEAT_GUI_DIALOG
13391 "dialog_gui",
13392#endif
13393#ifdef FEAT_DIFF
13394 "diff",
13395#endif
13396#ifdef FEAT_DIGRAPHS
13397 "digraphs",
13398#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013399#ifdef FEAT_DIRECTX
13400 "directx",
13401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402#ifdef FEAT_DND
13403 "dnd",
13404#endif
13405#ifdef FEAT_EMACS_TAGS
13406 "emacs_tags",
13407#endif
13408 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013409 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410#ifdef FEAT_SEARCH_EXTRA
13411 "extra_search",
13412#endif
13413#ifdef FEAT_FKMAP
13414 "farsi",
13415#endif
13416#ifdef FEAT_SEARCHPATH
13417 "file_in_path",
13418#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013419#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013420 "filterpipe",
13421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013422#ifdef FEAT_FIND_ID
13423 "find_in_path",
13424#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013425#ifdef FEAT_FLOAT
13426 "float",
13427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428#ifdef FEAT_FOLDING
13429 "folding",
13430#endif
13431#ifdef FEAT_FOOTER
13432 "footer",
13433#endif
13434#if !defined(USE_SYSTEM) && defined(UNIX)
13435 "fork",
13436#endif
13437#ifdef FEAT_GETTEXT
13438 "gettext",
13439#endif
13440#ifdef FEAT_GUI
13441 "gui",
13442#endif
13443#ifdef FEAT_GUI_ATHENA
13444# ifdef FEAT_GUI_NEXTAW
13445 "gui_neXtaw",
13446# else
13447 "gui_athena",
13448# endif
13449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450#ifdef FEAT_GUI_GTK
13451 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013452# ifdef USE_GTK3
13453 "gui_gtk3",
13454# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013455 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013456# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013458#ifdef FEAT_GUI_GNOME
13459 "gui_gnome",
13460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013461#ifdef FEAT_GUI_MAC
13462 "gui_mac",
13463#endif
13464#ifdef FEAT_GUI_MOTIF
13465 "gui_motif",
13466#endif
13467#ifdef FEAT_GUI_PHOTON
13468 "gui_photon",
13469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470#ifdef FEAT_GUI_W32
13471 "gui_win32",
13472#endif
13473#ifdef FEAT_HANGULIN
13474 "hangul_input",
13475#endif
13476#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13477 "iconv",
13478#endif
13479#ifdef FEAT_INS_EXPAND
13480 "insert_expand",
13481#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013482#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013483 "job",
13484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013485#ifdef FEAT_JUMPLIST
13486 "jumplist",
13487#endif
13488#ifdef FEAT_KEYMAP
13489 "keymap",
13490#endif
13491#ifdef FEAT_LANGMAP
13492 "langmap",
13493#endif
13494#ifdef FEAT_LIBCALL
13495 "libcall",
13496#endif
13497#ifdef FEAT_LINEBREAK
13498 "linebreak",
13499#endif
13500#ifdef FEAT_LISP
13501 "lispindent",
13502#endif
13503#ifdef FEAT_LISTCMDS
13504 "listcmds",
13505#endif
13506#ifdef FEAT_LOCALMAP
13507 "localmap",
13508#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013509#ifdef FEAT_LUA
13510# ifndef DYNAMIC_LUA
13511 "lua",
13512# endif
13513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013514#ifdef FEAT_MENU
13515 "menu",
13516#endif
13517#ifdef FEAT_SESSION
13518 "mksession",
13519#endif
13520#ifdef FEAT_MODIFY_FNAME
13521 "modify_fname",
13522#endif
13523#ifdef FEAT_MOUSE
13524 "mouse",
13525#endif
13526#ifdef FEAT_MOUSESHAPE
13527 "mouseshape",
13528#endif
13529#if defined(UNIX) || defined(VMS)
13530# ifdef FEAT_MOUSE_DEC
13531 "mouse_dec",
13532# endif
13533# ifdef FEAT_MOUSE_GPM
13534 "mouse_gpm",
13535# endif
13536# ifdef FEAT_MOUSE_JSB
13537 "mouse_jsbterm",
13538# endif
13539# ifdef FEAT_MOUSE_NET
13540 "mouse_netterm",
13541# endif
13542# ifdef FEAT_MOUSE_PTERM
13543 "mouse_pterm",
13544# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013545# ifdef FEAT_MOUSE_SGR
13546 "mouse_sgr",
13547# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013548# ifdef FEAT_SYSMOUSE
13549 "mouse_sysmouse",
13550# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013551# ifdef FEAT_MOUSE_URXVT
13552 "mouse_urxvt",
13553# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554# ifdef FEAT_MOUSE_XTERM
13555 "mouse_xterm",
13556# endif
13557#endif
13558#ifdef FEAT_MBYTE
13559 "multi_byte",
13560#endif
13561#ifdef FEAT_MBYTE_IME
13562 "multi_byte_ime",
13563#endif
13564#ifdef FEAT_MULTI_LANG
13565 "multi_lang",
13566#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013567#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013568#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013569 "mzscheme",
13570#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572#ifdef FEAT_OLE
13573 "ole",
13574#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013575 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576#ifdef FEAT_PATH_EXTRA
13577 "path_extra",
13578#endif
13579#ifdef FEAT_PERL
13580#ifndef DYNAMIC_PERL
13581 "perl",
13582#endif
13583#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013584#ifdef FEAT_PERSISTENT_UNDO
13585 "persistent_undo",
13586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587#ifdef FEAT_PYTHON
13588#ifndef DYNAMIC_PYTHON
13589 "python",
13590#endif
13591#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013592#ifdef FEAT_PYTHON3
13593#ifndef DYNAMIC_PYTHON3
13594 "python3",
13595#endif
13596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597#ifdef FEAT_POSTSCRIPT
13598 "postscript",
13599#endif
13600#ifdef FEAT_PRINTER
13601 "printer",
13602#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013603#ifdef FEAT_PROFILE
13604 "profile",
13605#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013606#ifdef FEAT_RELTIME
13607 "reltime",
13608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609#ifdef FEAT_QUICKFIX
13610 "quickfix",
13611#endif
13612#ifdef FEAT_RIGHTLEFT
13613 "rightleft",
13614#endif
13615#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13616 "ruby",
13617#endif
13618#ifdef FEAT_SCROLLBIND
13619 "scrollbind",
13620#endif
13621#ifdef FEAT_CMDL_INFO
13622 "showcmd",
13623 "cmdline_info",
13624#endif
13625#ifdef FEAT_SIGNS
13626 "signs",
13627#endif
13628#ifdef FEAT_SMARTINDENT
13629 "smartindent",
13630#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013631#ifdef STARTUPTIME
13632 "startuptime",
13633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634#ifdef FEAT_STL_OPT
13635 "statusline",
13636#endif
13637#ifdef FEAT_SUN_WORKSHOP
13638 "sun_workshop",
13639#endif
13640#ifdef FEAT_NETBEANS_INTG
13641 "netbeans_intg",
13642#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013643#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013644 "spell",
13645#endif
13646#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647 "syntax",
13648#endif
13649#if defined(USE_SYSTEM) || !defined(UNIX)
13650 "system",
13651#endif
13652#ifdef FEAT_TAG_BINS
13653 "tag_binary",
13654#endif
13655#ifdef FEAT_TAG_OLDSTATIC
13656 "tag_old_static",
13657#endif
13658#ifdef FEAT_TAG_ANYWHITE
13659 "tag_any_white",
13660#endif
13661#ifdef FEAT_TCL
13662# ifndef DYNAMIC_TCL
13663 "tcl",
13664# endif
13665#endif
13666#ifdef TERMINFO
13667 "terminfo",
13668#endif
13669#ifdef FEAT_TERMRESPONSE
13670 "termresponse",
13671#endif
13672#ifdef FEAT_TEXTOBJ
13673 "textobjects",
13674#endif
13675#ifdef HAVE_TGETENT
13676 "tgetent",
13677#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010013678#ifdef FEAT_TIMERS
13679 "timers",
13680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681#ifdef FEAT_TITLE
13682 "title",
13683#endif
13684#ifdef FEAT_TOOLBAR
13685 "toolbar",
13686#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013687#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13688 "unnamedplus",
13689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690#ifdef FEAT_USR_CMDS
13691 "user-commands", /* was accidentally included in 5.4 */
13692 "user_commands",
13693#endif
13694#ifdef FEAT_VIMINFO
13695 "viminfo",
13696#endif
13697#ifdef FEAT_VERTSPLIT
13698 "vertsplit",
13699#endif
13700#ifdef FEAT_VIRTUALEDIT
13701 "virtualedit",
13702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013703 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704#ifdef FEAT_VISUALEXTRA
13705 "visualextra",
13706#endif
13707#ifdef FEAT_VREPLACE
13708 "vreplace",
13709#endif
13710#ifdef FEAT_WILDIGN
13711 "wildignore",
13712#endif
13713#ifdef FEAT_WILDMENU
13714 "wildmenu",
13715#endif
13716#ifdef FEAT_WINDOWS
13717 "windows",
13718#endif
13719#ifdef FEAT_WAK
13720 "winaltkeys",
13721#endif
13722#ifdef FEAT_WRITEBACKUP
13723 "writebackup",
13724#endif
13725#ifdef FEAT_XIM
13726 "xim",
13727#endif
13728#ifdef FEAT_XFONTSET
13729 "xfontset",
13730#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013731#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013732 "xpm",
13733 "xpm_w32", /* for backward compatibility */
13734#else
13735# if defined(HAVE_XPM)
13736 "xpm",
13737# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739#ifdef USE_XSMP
13740 "xsmp",
13741#endif
13742#ifdef USE_XSMP_INTERACT
13743 "xsmp_interact",
13744#endif
13745#ifdef FEAT_XCLIPBOARD
13746 "xterm_clipboard",
13747#endif
13748#ifdef FEAT_XTERM_SAVE
13749 "xterm_save",
13750#endif
13751#if defined(UNIX) && defined(FEAT_X11)
13752 "X11",
13753#endif
13754 NULL
13755 };
13756
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013757 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758 for (i = 0; has_list[i] != NULL; ++i)
13759 if (STRICMP(name, has_list[i]) == 0)
13760 {
13761 n = TRUE;
13762 break;
13763 }
13764
13765 if (n == FALSE)
13766 {
13767 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013768 {
13769 if (name[5] == '-'
13770 && STRLEN(name) > 11
13771 && vim_isdigit(name[6])
13772 && vim_isdigit(name[8])
13773 && vim_isdigit(name[10]))
13774 {
13775 int major = atoi((char *)name + 6);
13776 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013777
13778 /* Expect "patch-9.9.01234". */
13779 n = (major < VIM_VERSION_MAJOR
13780 || (major == VIM_VERSION_MAJOR
13781 && (minor < VIM_VERSION_MINOR
13782 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013783 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013784 }
13785 else
13786 n = has_patch(atoi((char *)name + 5));
13787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788 else if (STRICMP(name, "vim_starting") == 0)
13789 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013790#ifdef FEAT_MBYTE
13791 else if (STRICMP(name, "multi_byte_encoding") == 0)
13792 n = has_mbyte;
13793#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013794#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13795 else if (STRICMP(name, "balloon_multiline") == 0)
13796 n = multiline_balloon_available();
13797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798#ifdef DYNAMIC_TCL
13799 else if (STRICMP(name, "tcl") == 0)
13800 n = tcl_enabled(FALSE);
13801#endif
13802#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13803 else if (STRICMP(name, "iconv") == 0)
13804 n = iconv_enabled(FALSE);
13805#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013806#ifdef DYNAMIC_LUA
13807 else if (STRICMP(name, "lua") == 0)
13808 n = lua_enabled(FALSE);
13809#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013810#ifdef DYNAMIC_MZSCHEME
13811 else if (STRICMP(name, "mzscheme") == 0)
13812 n = mzscheme_enabled(FALSE);
13813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814#ifdef DYNAMIC_RUBY
13815 else if (STRICMP(name, "ruby") == 0)
13816 n = ruby_enabled(FALSE);
13817#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013818#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819#ifdef DYNAMIC_PYTHON
13820 else if (STRICMP(name, "python") == 0)
13821 n = python_enabled(FALSE);
13822#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013823#endif
13824#ifdef FEAT_PYTHON3
13825#ifdef DYNAMIC_PYTHON3
13826 else if (STRICMP(name, "python3") == 0)
13827 n = python3_enabled(FALSE);
13828#endif
13829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830#ifdef DYNAMIC_PERL
13831 else if (STRICMP(name, "perl") == 0)
13832 n = perl_enabled(FALSE);
13833#endif
13834#ifdef FEAT_GUI
13835 else if (STRICMP(name, "gui_running") == 0)
13836 n = (gui.in_use || gui.starting);
13837# ifdef FEAT_GUI_W32
13838 else if (STRICMP(name, "gui_win32s") == 0)
13839 n = gui_is_win32s();
13840# endif
13841# ifdef FEAT_BROWSE
13842 else if (STRICMP(name, "browse") == 0)
13843 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13844# endif
13845#endif
13846#ifdef FEAT_SYN_HL
13847 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013848 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849#endif
13850#if defined(WIN3264)
13851 else if (STRICMP(name, "win95") == 0)
13852 n = mch_windows95();
13853#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013854#ifdef FEAT_NETBEANS_INTG
13855 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013856 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858 }
13859
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013860 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861}
13862
13863/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013864 * "has_key()" function
13865 */
13866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013867f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013868{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013869 if (argvars[0].v_type != VAR_DICT)
13870 {
13871 EMSG(_(e_dictreq));
13872 return;
13873 }
13874 if (argvars[0].vval.v_dict == NULL)
13875 return;
13876
13877 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013878 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013879}
13880
13881/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013882 * "haslocaldir()" function
13883 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013885f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013886{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013887 win_T *wp = NULL;
13888
13889 wp = find_tabwin(&argvars[0], &argvars[1]);
13890 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013891}
13892
13893/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894 * "hasmapto()" function
13895 */
13896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013897f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898{
13899 char_u *name;
13900 char_u *mode;
13901 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013902 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013904 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013905 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013906 mode = (char_u *)"nvo";
13907 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013908 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013909 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013910 if (argvars[2].v_type != VAR_UNKNOWN)
13911 abbr = get_tv_number(&argvars[2]);
13912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913
Bram Moolenaar2c932302006-03-18 21:42:09 +000013914 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013915 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013917 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013918}
13919
13920/*
13921 * "histadd()" function
13922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013924f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925{
13926#ifdef FEAT_CMDHIST
13927 int histype;
13928 char_u *str;
13929 char_u buf[NUMBUFLEN];
13930#endif
13931
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013932 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933 if (check_restricted() || check_secure())
13934 return;
13935#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013936 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13937 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938 if (histype >= 0)
13939 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013940 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941 if (*str != NUL)
13942 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013943 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013944 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013945 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013946 return;
13947 }
13948 }
13949#endif
13950}
13951
13952/*
13953 * "histdel()" function
13954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013956f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957{
13958#ifdef FEAT_CMDHIST
13959 int n;
13960 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013961 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013963 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13964 if (str == NULL)
13965 n = 0;
13966 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013968 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013969 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013970 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013971 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013972 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973 else
13974 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013975 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013976 get_tv_string_buf(&argvars[1], buf));
13977 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978#endif
13979}
13980
13981/*
13982 * "histget()" function
13983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013985f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986{
13987#ifdef FEAT_CMDHIST
13988 int type;
13989 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013990 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013991
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013992 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13993 if (str == NULL)
13994 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013995 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013996 {
13997 type = get_histtype(str);
13998 if (argvars[1].v_type == VAR_UNKNOWN)
13999 idx = get_history_idx(type);
14000 else
14001 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14002 /* -1 on type error */
14003 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014006 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014008 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009}
14010
14011/*
14012 * "histnr()" function
14013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014015f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016{
14017 int i;
14018
14019#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014020 char_u *history = get_tv_string_chk(&argvars[0]);
14021
14022 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014023 if (i >= HIST_CMD && i < HIST_COUNT)
14024 i = get_history_idx(i);
14025 else
14026#endif
14027 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014028 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014029}
14030
14031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032 * "highlightID(name)" function
14033 */
14034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014035f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014036{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014037 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038}
14039
14040/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014041 * "highlight_exists()" function
14042 */
14043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014044f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014045{
14046 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14047}
14048
14049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050 * "hostname()" function
14051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014053f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054{
14055 char_u hostname[256];
14056
14057 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014058 rettv->v_type = VAR_STRING;
14059 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060}
14061
14062/*
14063 * iconv() function
14064 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014066f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014067{
14068#ifdef FEAT_MBYTE
14069 char_u buf1[NUMBUFLEN];
14070 char_u buf2[NUMBUFLEN];
14071 char_u *from, *to, *str;
14072 vimconv_T vimconv;
14073#endif
14074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014075 rettv->v_type = VAR_STRING;
14076 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014077
14078#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014079 str = get_tv_string(&argvars[0]);
14080 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14081 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014082 vimconv.vc_type = CONV_NONE;
14083 convert_setup(&vimconv, from, to);
14084
14085 /* If the encodings are equal, no conversion needed. */
14086 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014087 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014089 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090
14091 convert_setup(&vimconv, NULL, NULL);
14092 vim_free(from);
14093 vim_free(to);
14094#endif
14095}
14096
14097/*
14098 * "indent()" function
14099 */
14100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014101f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102{
14103 linenr_T lnum;
14104
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014105 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014106 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014107 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014108 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014109 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014110}
14111
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014112/*
14113 * "index()" function
14114 */
14115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014116f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014117{
Bram Moolenaar33570922005-01-25 22:26:29 +000014118 list_T *l;
14119 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014120 long idx = 0;
14121 int ic = FALSE;
14122
14123 rettv->vval.v_number = -1;
14124 if (argvars[0].v_type != VAR_LIST)
14125 {
14126 EMSG(_(e_listreq));
14127 return;
14128 }
14129 l = argvars[0].vval.v_list;
14130 if (l != NULL)
14131 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014132 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014133 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014134 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014135 int error = FALSE;
14136
Bram Moolenaar758711c2005-02-02 23:11:38 +000014137 /* Start at specified item. Use the cached index that list_find()
14138 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014139 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014140 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014141 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014142 ic = get_tv_number_chk(&argvars[3], &error);
14143 if (error)
14144 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014145 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014146
Bram Moolenaar758711c2005-02-02 23:11:38 +000014147 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014148 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014149 {
14150 rettv->vval.v_number = idx;
14151 break;
14152 }
14153 }
14154}
14155
Bram Moolenaar071d4272004-06-13 20:20:40 +000014156static int inputsecret_flag = 0;
14157
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014158static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014159
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014161 * This function is used by f_input() and f_inputdialog() functions. The third
14162 * argument to f_input() specifies the type of completion to use at the
14163 * prompt. The third argument to f_inputdialog() specifies the value to return
14164 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014165 */
14166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014167get_user_input(
14168 typval_T *argvars,
14169 typval_T *rettv,
14170 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014171{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014172 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173 char_u *p = NULL;
14174 int c;
14175 char_u buf[NUMBUFLEN];
14176 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014177 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014178 int xp_type = EXPAND_NOTHING;
14179 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014181 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014182 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014183
14184#ifdef NO_CONSOLE_INPUT
14185 /* While starting up, there is no place to enter text. */
14186 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014187 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188#endif
14189
14190 cmd_silent = FALSE; /* Want to see the prompt. */
14191 if (prompt != NULL)
14192 {
14193 /* Only the part of the message after the last NL is considered as
14194 * prompt for the command line */
14195 p = vim_strrchr(prompt, '\n');
14196 if (p == NULL)
14197 p = prompt;
14198 else
14199 {
14200 ++p;
14201 c = *p;
14202 *p = NUL;
14203 msg_start();
14204 msg_clr_eos();
14205 msg_puts_attr(prompt, echo_attr);
14206 msg_didout = FALSE;
14207 msg_starthere();
14208 *p = c;
14209 }
14210 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014212 if (argvars[1].v_type != VAR_UNKNOWN)
14213 {
14214 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14215 if (defstr != NULL)
14216 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014218 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014219 {
14220 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014221 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014222 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014223
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014224 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014225 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014226
Bram Moolenaar4463f292005-09-25 22:20:24 +000014227 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14228 if (xp_name == NULL)
14229 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014230
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014231 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014232
Bram Moolenaar4463f292005-09-25 22:20:24 +000014233 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14234 &xp_arg) == FAIL)
14235 return;
14236 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014237 }
14238
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014239 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014240 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014241 int save_ex_normal_busy = ex_normal_busy;
14242 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014243 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014244 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14245 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014246 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014247 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014248 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014249 && argvars[1].v_type != VAR_UNKNOWN
14250 && argvars[2].v_type != VAR_UNKNOWN)
14251 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14252 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014253
14254 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014256 /* since the user typed this, no need to wait for return */
14257 need_wait_return = FALSE;
14258 msg_didout = FALSE;
14259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014260 cmd_silent = cmd_silent_save;
14261}
14262
14263/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014264 * "input()" function
14265 * Also handles inputsecret() when inputsecret is set.
14266 */
14267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014268f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014269{
14270 get_user_input(argvars, rettv, FALSE);
14271}
14272
14273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274 * "inputdialog()" function
14275 */
14276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014277f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278{
14279#if defined(FEAT_GUI_TEXTDIALOG)
14280 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14281 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14282 {
14283 char_u *message;
14284 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014285 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014287 message = get_tv_string_chk(&argvars[0]);
14288 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014289 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014290 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291 else
14292 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014293 if (message != NULL && defstr != NULL
14294 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014295 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014296 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297 else
14298 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014299 if (message != NULL && defstr != NULL
14300 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014301 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014302 rettv->vval.v_string = vim_strsave(
14303 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014304 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014305 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014306 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014307 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014308 }
14309 else
14310#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014311 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312}
14313
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014314/*
14315 * "inputlist()" function
14316 */
14317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014318f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014319{
14320 listitem_T *li;
14321 int selected;
14322 int mouse_used;
14323
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014324#ifdef NO_CONSOLE_INPUT
14325 /* While starting up, there is no place to enter text. */
14326 if (no_console_input())
14327 return;
14328#endif
14329 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14330 {
14331 EMSG2(_(e_listarg), "inputlist()");
14332 return;
14333 }
14334
14335 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014336 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014337 lines_left = Rows; /* avoid more prompt */
14338 msg_scroll = TRUE;
14339 msg_clr_eos();
14340
14341 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14342 {
14343 msg_puts(get_tv_string(&li->li_tv));
14344 msg_putchar('\n');
14345 }
14346
14347 /* Ask for choice. */
14348 selected = prompt_for_number(&mouse_used);
14349 if (mouse_used)
14350 selected -= lines_left;
14351
14352 rettv->vval.v_number = selected;
14353}
14354
14355
Bram Moolenaar071d4272004-06-13 20:20:40 +000014356static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14357
14358/*
14359 * "inputrestore()" function
14360 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014362f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363{
14364 if (ga_userinput.ga_len > 0)
14365 {
14366 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14368 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014369 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370 }
14371 else if (p_verbose > 1)
14372 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014373 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014374 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375 }
14376}
14377
14378/*
14379 * "inputsave()" function
14380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014382f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014384 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385 if (ga_grow(&ga_userinput, 1) == OK)
14386 {
14387 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14388 + ga_userinput.ga_len);
14389 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014390 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391 }
14392 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014393 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394}
14395
14396/*
14397 * "inputsecret()" function
14398 */
14399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014400f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401{
14402 ++cmdline_star;
14403 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014404 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405 --cmdline_star;
14406 --inputsecret_flag;
14407}
14408
14409/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014410 * "insert()" function
14411 */
14412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014413f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014414{
14415 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014416 listitem_T *item;
14417 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014418 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014419
14420 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014421 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014422 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014423 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014424 {
14425 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014426 before = get_tv_number_chk(&argvars[2], &error);
14427 if (error)
14428 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014429
Bram Moolenaar758711c2005-02-02 23:11:38 +000014430 if (before == l->lv_len)
14431 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014432 else
14433 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014434 item = list_find(l, before);
14435 if (item == NULL)
14436 {
14437 EMSGN(_(e_listidx), before);
14438 l = NULL;
14439 }
14440 }
14441 if (l != NULL)
14442 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014443 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014444 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014445 }
14446 }
14447}
14448
14449/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014450 * "invert(expr)" function
14451 */
14452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014453f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014454{
14455 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14456}
14457
14458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459 * "isdirectory()" function
14460 */
14461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014462f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014464 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465}
14466
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014467/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014468 * "islocked()" function
14469 */
14470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014471f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014472{
14473 lval_T lv;
14474 char_u *end;
14475 dictitem_T *di;
14476
14477 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014478 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14479 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014480 if (end != NULL && lv.ll_name != NULL)
14481 {
14482 if (*end != NUL)
14483 EMSG(_(e_trailing));
14484 else
14485 {
14486 if (lv.ll_tv == NULL)
14487 {
14488 if (check_changedtick(lv.ll_name))
14489 rettv->vval.v_number = 1; /* always locked */
14490 else
14491 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014492 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014493 if (di != NULL)
14494 {
14495 /* Consider a variable locked when:
14496 * 1. the variable itself is locked
14497 * 2. the value of the variable is locked.
14498 * 3. the List or Dict value is locked.
14499 */
14500 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14501 || tv_islocked(&di->di_tv));
14502 }
14503 }
14504 }
14505 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014506 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014507 else if (lv.ll_newkey != NULL)
14508 EMSG2(_(e_dictkey), lv.ll_newkey);
14509 else if (lv.ll_list != NULL)
14510 /* List item. */
14511 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14512 else
14513 /* Dictionary item. */
14514 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14515 }
14516 }
14517
14518 clear_lval(&lv);
14519}
14520
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014521#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14522/*
14523 * "isnan()" function
14524 */
14525 static void
14526f_isnan(typval_T *argvars, typval_T *rettv)
14527{
14528 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14529 && isnan(argvars[0].vval.v_float);
14530}
14531#endif
14532
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014533static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014534
14535/*
14536 * Turn a dict into a list:
14537 * "what" == 0: list of keys
14538 * "what" == 1: list of values
14539 * "what" == 2: list of items
14540 */
14541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014542dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014543{
Bram Moolenaar33570922005-01-25 22:26:29 +000014544 list_T *l2;
14545 dictitem_T *di;
14546 hashitem_T *hi;
14547 listitem_T *li;
14548 listitem_T *li2;
14549 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014550 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014551
Bram Moolenaar8c711452005-01-14 21:53:12 +000014552 if (argvars[0].v_type != VAR_DICT)
14553 {
14554 EMSG(_(e_dictreq));
14555 return;
14556 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014557 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014558 return;
14559
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014560 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014561 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014562
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014563 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014564 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014565 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014566 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014567 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014568 --todo;
14569 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014570
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014571 li = listitem_alloc();
14572 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014573 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014574 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014575
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014576 if (what == 0)
14577 {
14578 /* keys() */
14579 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014580 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014581 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14582 }
14583 else if (what == 1)
14584 {
14585 /* values() */
14586 copy_tv(&di->di_tv, &li->li_tv);
14587 }
14588 else
14589 {
14590 /* items() */
14591 l2 = list_alloc();
14592 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014593 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014594 li->li_tv.vval.v_list = l2;
14595 if (l2 == NULL)
14596 break;
14597 ++l2->lv_refcount;
14598
14599 li2 = listitem_alloc();
14600 if (li2 == NULL)
14601 break;
14602 list_append(l2, li2);
14603 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014604 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014605 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14606
14607 li2 = listitem_alloc();
14608 if (li2 == NULL)
14609 break;
14610 list_append(l2, li2);
14611 copy_tv(&di->di_tv, &li2->li_tv);
14612 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014613 }
14614 }
14615}
14616
14617/*
14618 * "items(dict)" function
14619 */
14620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014621f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014622{
14623 dict_list(argvars, rettv, 2);
14624}
14625
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014626#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014627/*
14628 * Get the job from the argument.
14629 * Returns NULL if the job is invalid.
14630 */
14631 static job_T *
14632get_job_arg(typval_T *tv)
14633{
14634 job_T *job;
14635
14636 if (tv->v_type != VAR_JOB)
14637 {
14638 EMSG2(_(e_invarg2), get_tv_string(tv));
14639 return NULL;
14640 }
14641 job = tv->vval.v_job;
14642
14643 if (job == NULL)
14644 EMSG(_("E916: not a valid job"));
14645 return job;
14646}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014647
Bram Moolenaar835dc632016-02-07 14:27:38 +010014648/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014649 * "job_getchannel()" function
14650 */
14651 static void
14652f_job_getchannel(typval_T *argvars, typval_T *rettv)
14653{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014654 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014655
Bram Moolenaar65edff82016-02-21 16:40:11 +010014656 if (job != NULL)
14657 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014658 rettv->v_type = VAR_CHANNEL;
14659 rettv->vval.v_channel = job->jv_channel;
14660 if (job->jv_channel != NULL)
14661 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014662 }
14663}
14664
14665/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010014666 * "job_info()" function
14667 */
14668 static void
14669f_job_info(typval_T *argvars, typval_T *rettv)
14670{
14671 job_T *job = get_job_arg(&argvars[0]);
14672
14673 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
14674 job_info(job, rettv->vval.v_dict);
14675}
14676
14677/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014678 * "job_setoptions()" function
14679 */
14680 static void
14681f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14682{
14683 job_T *job = get_job_arg(&argvars[0]);
14684 jobopt_T opt;
14685
14686 if (job == NULL)
14687 return;
14688 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014689 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014690 return;
14691 job_set_options(job, &opt);
14692}
14693
14694/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014695 * "job_start()" function
14696 */
14697 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010014698f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014699{
Bram Moolenaar835dc632016-02-07 14:27:38 +010014700 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014701 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014702}
14703
14704/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014705 * "job_status()" function
14706 */
14707 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014708f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014709{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014710 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014711
Bram Moolenaar65edff82016-02-21 16:40:11 +010014712 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014713 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010014714 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010014715 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010014716 }
14717}
14718
14719/*
14720 * "job_stop()" function
14721 */
14722 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014723f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014724{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014725 job_T *job = get_job_arg(&argvars[0]);
14726
14727 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014728 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014729}
14730#endif
14731
Bram Moolenaar071d4272004-06-13 20:20:40 +000014732/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014733 * "join()" function
14734 */
14735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014736f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014737{
14738 garray_T ga;
14739 char_u *sep;
14740
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014741 if (argvars[0].v_type != VAR_LIST)
14742 {
14743 EMSG(_(e_listreq));
14744 return;
14745 }
14746 if (argvars[0].vval.v_list == NULL)
14747 return;
14748 if (argvars[1].v_type == VAR_UNKNOWN)
14749 sep = (char_u *)" ";
14750 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014751 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014752
14753 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014754
14755 if (sep != NULL)
14756 {
14757 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014758 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014759 ga_append(&ga, NUL);
14760 rettv->vval.v_string = (char_u *)ga.ga_data;
14761 }
14762 else
14763 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014764}
14765
14766/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014767 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014768 */
14769 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014770f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014771{
14772 js_read_T reader;
14773
14774 reader.js_buf = get_tv_string(&argvars[0]);
14775 reader.js_fill = NULL;
14776 reader.js_used = 0;
14777 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14778 EMSG(_(e_invarg));
14779}
14780
14781/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014782 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014783 */
14784 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014785f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014786{
14787 rettv->v_type = VAR_STRING;
14788 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14789}
14790
14791/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014792 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014793 */
14794 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014795f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014796{
14797 js_read_T reader;
14798
14799 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014800 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014801 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014802 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014803 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014804}
14805
14806/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014807 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014808 */
14809 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014810f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014811{
14812 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014813 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014814}
14815
14816/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014817 * "keys()" function
14818 */
14819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014820f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014821{
14822 dict_list(argvars, rettv, 0);
14823}
14824
14825/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014826 * "last_buffer_nr()" function.
14827 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014829f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014830{
14831 int n = 0;
14832 buf_T *buf;
14833
14834 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14835 if (n < buf->b_fnum)
14836 n = buf->b_fnum;
14837
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014838 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014839}
14840
14841/*
14842 * "len()" function
14843 */
14844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014845f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014846{
14847 switch (argvars[0].v_type)
14848 {
14849 case VAR_STRING:
14850 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014851 rettv->vval.v_number = (varnumber_T)STRLEN(
14852 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014853 break;
14854 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014855 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014856 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014857 case VAR_DICT:
14858 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14859 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014860 case VAR_UNKNOWN:
14861 case VAR_SPECIAL:
14862 case VAR_FLOAT:
14863 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010014864 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014865 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014866 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014867 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014868 break;
14869 }
14870}
14871
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014872static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014873
14874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014875libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014876{
14877#ifdef FEAT_LIBCALL
14878 char_u *string_in;
14879 char_u **string_result;
14880 int nr_result;
14881#endif
14882
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014883 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014884 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014885 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014886
14887 if (check_restricted() || check_secure())
14888 return;
14889
14890#ifdef FEAT_LIBCALL
14891 /* The first two args must be strings, otherwise its meaningless */
14892 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14893 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014894 string_in = NULL;
14895 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014896 string_in = argvars[2].vval.v_string;
14897 if (type == VAR_NUMBER)
14898 string_result = NULL;
14899 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014900 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014901 if (mch_libcall(argvars[0].vval.v_string,
14902 argvars[1].vval.v_string,
14903 string_in,
14904 argvars[2].vval.v_number,
14905 string_result,
14906 &nr_result) == OK
14907 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014908 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014909 }
14910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014911}
14912
14913/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014914 * "libcall()" function
14915 */
14916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014917f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014918{
14919 libcall_common(argvars, rettv, VAR_STRING);
14920}
14921
14922/*
14923 * "libcallnr()" function
14924 */
14925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014926f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014927{
14928 libcall_common(argvars, rettv, VAR_NUMBER);
14929}
14930
14931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014932 * "line(string)" function
14933 */
14934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014935f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014936{
14937 linenr_T lnum = 0;
14938 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014939 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014940
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014941 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014942 if (fp != NULL)
14943 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014944 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014945}
14946
14947/*
14948 * "line2byte(lnum)" function
14949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014951f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014952{
14953#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014954 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014955#else
14956 linenr_T lnum;
14957
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014958 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014959 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014960 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014962 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14963 if (rettv->vval.v_number >= 0)
14964 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014965#endif
14966}
14967
14968/*
14969 * "lispindent(lnum)" function
14970 */
14971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014972f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014973{
14974#ifdef FEAT_LISP
14975 pos_T pos;
14976 linenr_T lnum;
14977
14978 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014979 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014980 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14981 {
14982 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014983 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014984 curwin->w_cursor = pos;
14985 }
14986 else
14987#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014988 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014989}
14990
14991/*
14992 * "localtime()" function
14993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014995f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014996{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014997 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014998}
14999
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015000static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015001
15002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015003get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015004{
15005 char_u *keys;
15006 char_u *which;
15007 char_u buf[NUMBUFLEN];
15008 char_u *keys_buf = NULL;
15009 char_u *rhs;
15010 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015011 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015012 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015013 mapblock_T *mp;
15014 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015
15016 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015017 rettv->v_type = VAR_STRING;
15018 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015019
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015020 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015021 if (*keys == NUL)
15022 return;
15023
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015024 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015025 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015026 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015027 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015028 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015029 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015030 if (argvars[3].v_type != VAR_UNKNOWN)
15031 get_dict = get_tv_number(&argvars[3]);
15032 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034 else
15035 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015036 if (which == NULL)
15037 return;
15038
Bram Moolenaar071d4272004-06-13 20:20:40 +000015039 mode = get_map_mode(&which, 0);
15040
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015041 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015042 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015043 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015044
15045 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015046 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015047 /* Return a string. */
15048 if (rhs != NULL)
15049 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015050
Bram Moolenaarbd743252010-10-20 21:23:33 +020015051 }
15052 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15053 {
15054 /* Return a dictionary. */
15055 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15056 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15057 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058
Bram Moolenaarbd743252010-10-20 21:23:33 +020015059 dict_add_nr_str(dict, "lhs", 0L, lhs);
15060 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15061 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15062 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15063 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15064 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15065 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015066 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015067 dict_add_nr_str(dict, "mode", 0L, mapmode);
15068
15069 vim_free(lhs);
15070 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015071 }
15072}
15073
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015074#ifdef FEAT_FLOAT
15075/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015076 * "log()" function
15077 */
15078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015079f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015080{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015081 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015082
15083 rettv->v_type = VAR_FLOAT;
15084 if (get_float_arg(argvars, &f) == OK)
15085 rettv->vval.v_float = log(f);
15086 else
15087 rettv->vval.v_float = 0.0;
15088}
15089
15090/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015091 * "log10()" function
15092 */
15093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015094f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015095{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015096 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015097
15098 rettv->v_type = VAR_FLOAT;
15099 if (get_float_arg(argvars, &f) == OK)
15100 rettv->vval.v_float = log10(f);
15101 else
15102 rettv->vval.v_float = 0.0;
15103}
15104#endif
15105
Bram Moolenaar1dced572012-04-05 16:54:08 +020015106#ifdef FEAT_LUA
15107/*
15108 * "luaeval()" function
15109 */
15110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015111f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015112{
15113 char_u *str;
15114 char_u buf[NUMBUFLEN];
15115
15116 str = get_tv_string_buf(&argvars[0], buf);
15117 do_luaeval(str, argvars + 1, rettv);
15118}
15119#endif
15120
Bram Moolenaar071d4272004-06-13 20:20:40 +000015121/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015122 * "map()" function
15123 */
15124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015125f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015126{
15127 filter_map(argvars, rettv, TRUE);
15128}
15129
15130/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015131 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015132 */
15133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015134f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015135{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015136 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015137}
15138
15139/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015140 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141 */
15142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015143f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015144{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015145 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015146}
15147
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015148static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015149
15150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015151find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015152{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015153 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015154 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015155 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015156 char_u *pat;
15157 regmatch_T regmatch;
15158 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015159 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015160 char_u *save_cpo;
15161 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015162 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015163 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015164 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015165 list_T *l = NULL;
15166 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015167 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015168 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015169
15170 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15171 save_cpo = p_cpo;
15172 p_cpo = (char_u *)"";
15173
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015174 rettv->vval.v_number = -1;
15175 if (type == 3)
15176 {
15177 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015178 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015179 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015180 }
15181 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015182 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015183 rettv->v_type = VAR_STRING;
15184 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015186
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015187 if (argvars[0].v_type == VAR_LIST)
15188 {
15189 if ((l = argvars[0].vval.v_list) == NULL)
15190 goto theend;
15191 li = l->lv_first;
15192 }
15193 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015194 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015195 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015196 len = (long)STRLEN(str);
15197 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015198
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015199 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15200 if (pat == NULL)
15201 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015202
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015203 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015205 int error = FALSE;
15206
15207 start = get_tv_number_chk(&argvars[2], &error);
15208 if (error)
15209 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015210 if (l != NULL)
15211 {
15212 li = list_find(l, start);
15213 if (li == NULL)
15214 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015215 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015216 }
15217 else
15218 {
15219 if (start < 0)
15220 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015221 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015222 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015223 /* When "count" argument is there ignore matches before "start",
15224 * otherwise skip part of the string. Differs when pattern is "^"
15225 * or "\<". */
15226 if (argvars[3].v_type != VAR_UNKNOWN)
15227 startcol = start;
15228 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015229 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015230 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015231 len -= start;
15232 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015233 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015234
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015235 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015236 nth = get_tv_number_chk(&argvars[3], &error);
15237 if (error)
15238 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015239 }
15240
15241 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15242 if (regmatch.regprog != NULL)
15243 {
15244 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015245
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015246 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015247 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015248 if (l != NULL)
15249 {
15250 if (li == NULL)
15251 {
15252 match = FALSE;
15253 break;
15254 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015255 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015256 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015257 if (str == NULL)
15258 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015259 }
15260
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015261 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015262
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015263 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015264 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015265 if (l == NULL && !match)
15266 break;
15267
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015268 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015269 if (l != NULL)
15270 {
15271 li = li->li_next;
15272 ++idx;
15273 }
15274 else
15275 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015276#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015277 startcol = (colnr_T)(regmatch.startp[0]
15278 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015279#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015280 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015281#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015282 if (startcol > (colnr_T)len
15283 || str + startcol <= regmatch.startp[0])
15284 {
15285 match = FALSE;
15286 break;
15287 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015288 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015289 }
15290
15291 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015292 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015293 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015294 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015295 int i;
15296
15297 /* return list with matched string and submatches */
15298 for (i = 0; i < NSUBEXP; ++i)
15299 {
15300 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015301 {
15302 if (list_append_string(rettv->vval.v_list,
15303 (char_u *)"", 0) == FAIL)
15304 break;
15305 }
15306 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015307 regmatch.startp[i],
15308 (int)(regmatch.endp[i] - regmatch.startp[i]))
15309 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015310 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015311 }
15312 }
15313 else if (type == 2)
15314 {
15315 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015316 if (l != NULL)
15317 copy_tv(&li->li_tv, rettv);
15318 else
15319 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015321 }
15322 else if (l != NULL)
15323 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015324 else
15325 {
15326 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015327 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328 (varnumber_T)(regmatch.startp[0] - str);
15329 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015330 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015331 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015332 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333 }
15334 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015335 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015336 }
15337
15338theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015339 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015340 p_cpo = save_cpo;
15341}
15342
15343/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015344 * "match()" function
15345 */
15346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015347f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015348{
15349 find_some_match(argvars, rettv, 1);
15350}
15351
15352/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015353 * "matchadd()" function
15354 */
15355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015356f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015357{
15358#ifdef FEAT_SEARCH_EXTRA
15359 char_u buf[NUMBUFLEN];
15360 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15361 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15362 int prio = 10; /* default priority */
15363 int id = -1;
15364 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015365 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015366
15367 rettv->vval.v_number = -1;
15368
15369 if (grp == NULL || pat == NULL)
15370 return;
15371 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015372 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015373 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015374 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015375 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015376 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015377 if (argvars[4].v_type != VAR_UNKNOWN)
15378 {
15379 if (argvars[4].v_type != VAR_DICT)
15380 {
15381 EMSG(_(e_dictreq));
15382 return;
15383 }
15384 if (dict_find(argvars[4].vval.v_dict,
15385 (char_u *)"conceal", -1) != NULL)
15386 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15387 (char_u *)"conceal", FALSE);
15388 }
15389 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015390 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015391 if (error == TRUE)
15392 return;
15393 if (id >= 1 && id <= 3)
15394 {
15395 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15396 return;
15397 }
15398
Bram Moolenaar6561d522015-07-21 15:48:27 +020015399 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15400 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015401#endif
15402}
15403
15404/*
15405 * "matchaddpos()" function
15406 */
15407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015408f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015409{
15410#ifdef FEAT_SEARCH_EXTRA
15411 char_u buf[NUMBUFLEN];
15412 char_u *group;
15413 int prio = 10;
15414 int id = -1;
15415 int error = FALSE;
15416 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015417 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015418
15419 rettv->vval.v_number = -1;
15420
15421 group = get_tv_string_buf_chk(&argvars[0], buf);
15422 if (group == NULL)
15423 return;
15424
15425 if (argvars[1].v_type != VAR_LIST)
15426 {
15427 EMSG2(_(e_listarg), "matchaddpos()");
15428 return;
15429 }
15430 l = argvars[1].vval.v_list;
15431 if (l == NULL)
15432 return;
15433
15434 if (argvars[2].v_type != VAR_UNKNOWN)
15435 {
15436 prio = get_tv_number_chk(&argvars[2], &error);
15437 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015438 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015439 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015440 if (argvars[4].v_type != VAR_UNKNOWN)
15441 {
15442 if (argvars[4].v_type != VAR_DICT)
15443 {
15444 EMSG(_(e_dictreq));
15445 return;
15446 }
15447 if (dict_find(argvars[4].vval.v_dict,
15448 (char_u *)"conceal", -1) != NULL)
15449 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15450 (char_u *)"conceal", FALSE);
15451 }
15452 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015453 }
15454 if (error == TRUE)
15455 return;
15456
15457 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15458 if (id == 1 || id == 2)
15459 {
15460 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15461 return;
15462 }
15463
Bram Moolenaar6561d522015-07-21 15:48:27 +020015464 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15465 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015466#endif
15467}
15468
15469/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015470 * "matcharg()" function
15471 */
15472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015473f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015474{
15475 if (rettv_list_alloc(rettv) == OK)
15476 {
15477#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015478 int id = get_tv_number(&argvars[0]);
15479 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015480
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015481 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015482 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015483 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15484 {
15485 list_append_string(rettv->vval.v_list,
15486 syn_id2name(m->hlg_id), -1);
15487 list_append_string(rettv->vval.v_list, m->pattern, -1);
15488 }
15489 else
15490 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015491 list_append_string(rettv->vval.v_list, NULL, -1);
15492 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015493 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015494 }
15495#endif
15496 }
15497}
15498
15499/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015500 * "matchdelete()" function
15501 */
15502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015503f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015504{
15505#ifdef FEAT_SEARCH_EXTRA
15506 rettv->vval.v_number = match_delete(curwin,
15507 (int)get_tv_number(&argvars[0]), TRUE);
15508#endif
15509}
15510
15511/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015512 * "matchend()" function
15513 */
15514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015515f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015516{
15517 find_some_match(argvars, rettv, 0);
15518}
15519
15520/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015521 * "matchlist()" function
15522 */
15523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015524f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015525{
15526 find_some_match(argvars, rettv, 3);
15527}
15528
15529/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015530 * "matchstr()" function
15531 */
15532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015533f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015534{
15535 find_some_match(argvars, rettv, 2);
15536}
15537
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015538static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015539
15540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015541max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015542{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015543 long n = 0;
15544 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015545 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015546
15547 if (argvars[0].v_type == VAR_LIST)
15548 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015549 list_T *l;
15550 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015551
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015552 l = argvars[0].vval.v_list;
15553 if (l != NULL)
15554 {
15555 li = l->lv_first;
15556 if (li != NULL)
15557 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015558 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015559 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015560 {
15561 li = li->li_next;
15562 if (li == NULL)
15563 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015564 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015565 if (domax ? i > n : i < n)
15566 n = i;
15567 }
15568 }
15569 }
15570 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015571 else if (argvars[0].v_type == VAR_DICT)
15572 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015573 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015574 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015575 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015576 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015577
15578 d = argvars[0].vval.v_dict;
15579 if (d != NULL)
15580 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015581 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015582 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015583 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015584 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015585 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015586 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015587 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015588 if (first)
15589 {
15590 n = i;
15591 first = FALSE;
15592 }
15593 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015594 n = i;
15595 }
15596 }
15597 }
15598 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015599 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015600 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015601 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015602}
15603
15604/*
15605 * "max()" function
15606 */
15607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015608f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015609{
15610 max_min(argvars, rettv, TRUE);
15611}
15612
15613/*
15614 * "min()" function
15615 */
15616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015617f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015618{
15619 max_min(argvars, rettv, FALSE);
15620}
15621
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015622static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015623
15624/*
15625 * Create the directory in which "dir" is located, and higher levels when
15626 * needed.
15627 */
15628 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015629mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015630{
15631 char_u *p;
15632 char_u *updir;
15633 int r = FAIL;
15634
15635 /* Get end of directory name in "dir".
15636 * We're done when it's "/" or "c:/". */
15637 p = gettail_sep(dir);
15638 if (p <= get_past_head(dir))
15639 return OK;
15640
15641 /* If the directory exists we're done. Otherwise: create it.*/
15642 updir = vim_strnsave(dir, (int)(p - dir));
15643 if (updir == NULL)
15644 return FAIL;
15645 if (mch_isdir(updir))
15646 r = OK;
15647 else if (mkdir_recurse(updir, prot) == OK)
15648 r = vim_mkdir_emsg(updir, prot);
15649 vim_free(updir);
15650 return r;
15651}
15652
15653#ifdef vim_mkdir
15654/*
15655 * "mkdir()" function
15656 */
15657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015658f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015659{
15660 char_u *dir;
15661 char_u buf[NUMBUFLEN];
15662 int prot = 0755;
15663
15664 rettv->vval.v_number = FAIL;
15665 if (check_restricted() || check_secure())
15666 return;
15667
15668 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015669 if (*dir == NUL)
15670 rettv->vval.v_number = FAIL;
15671 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015672 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015673 if (*gettail(dir) == NUL)
15674 /* remove trailing slashes */
15675 *gettail_sep(dir) = NUL;
15676
15677 if (argvars[1].v_type != VAR_UNKNOWN)
15678 {
15679 if (argvars[2].v_type != VAR_UNKNOWN)
15680 prot = get_tv_number_chk(&argvars[2], NULL);
15681 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15682 mkdir_recurse(dir, prot);
15683 }
15684 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015685 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015686}
15687#endif
15688
Bram Moolenaar0d660222005-01-07 21:51:51 +000015689/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690 * "mode()" function
15691 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015693f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015695 char_u buf[3];
15696
15697 buf[1] = NUL;
15698 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015699
Bram Moolenaar071d4272004-06-13 20:20:40 +000015700 if (VIsual_active)
15701 {
15702 if (VIsual_select)
15703 buf[0] = VIsual_mode + 's' - 'v';
15704 else
15705 buf[0] = VIsual_mode;
15706 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015707 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015708 || State == CONFIRM)
15709 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015710 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015711 if (State == ASKMORE)
15712 buf[1] = 'm';
15713 else if (State == CONFIRM)
15714 buf[1] = '?';
15715 }
15716 else if (State == EXTERNCMD)
15717 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015718 else if (State & INSERT)
15719 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015720#ifdef FEAT_VREPLACE
15721 if (State & VREPLACE_FLAG)
15722 {
15723 buf[0] = 'R';
15724 buf[1] = 'v';
15725 }
15726 else
15727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728 if (State & REPLACE_FLAG)
15729 buf[0] = 'R';
15730 else
15731 buf[0] = 'i';
15732 }
15733 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015734 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015736 if (exmode_active)
15737 buf[1] = 'v';
15738 }
15739 else if (exmode_active)
15740 {
15741 buf[0] = 'c';
15742 buf[1] = 'e';
15743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015744 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015745 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015747 if (finish_op)
15748 buf[1] = 'o';
15749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015751 /* Clear out the minor mode when the argument is not a non-zero number or
15752 * non-empty string. */
15753 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015754 buf[1] = NUL;
15755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015756 rettv->vval.v_string = vim_strsave(buf);
15757 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758}
15759
Bram Moolenaar429fa852013-04-15 12:27:36 +020015760#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015761/*
15762 * "mzeval()" function
15763 */
15764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015765f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015766{
15767 char_u *str;
15768 char_u buf[NUMBUFLEN];
15769
15770 str = get_tv_string_buf(&argvars[0], buf);
15771 do_mzeval(str, rettv);
15772}
Bram Moolenaar75676462013-01-30 14:55:42 +010015773
15774 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015775mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015776{
15777 typval_T argvars[3];
15778
15779 argvars[0].v_type = VAR_STRING;
15780 argvars[0].vval.v_string = name;
15781 copy_tv(args, &argvars[1]);
15782 argvars[2].v_type = VAR_UNKNOWN;
15783 f_call(argvars, rettv);
15784 clear_tv(&argvars[1]);
15785}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015786#endif
15787
Bram Moolenaar071d4272004-06-13 20:20:40 +000015788/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015789 * "nextnonblank()" function
15790 */
15791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015792f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015793{
15794 linenr_T lnum;
15795
15796 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15797 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015798 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015799 {
15800 lnum = 0;
15801 break;
15802 }
15803 if (*skipwhite(ml_get(lnum)) != NUL)
15804 break;
15805 }
15806 rettv->vval.v_number = lnum;
15807}
15808
15809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015810 * "nr2char()" function
15811 */
15812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015813f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015814{
15815 char_u buf[NUMBUFLEN];
15816
15817#ifdef FEAT_MBYTE
15818 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015819 {
15820 int utf8 = 0;
15821
15822 if (argvars[1].v_type != VAR_UNKNOWN)
15823 utf8 = get_tv_number_chk(&argvars[1], NULL);
15824 if (utf8)
15825 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15826 else
15827 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015829 else
15830#endif
15831 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015832 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015833 buf[1] = NUL;
15834 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015835 rettv->v_type = VAR_STRING;
15836 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015837}
15838
15839/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015840 * "or(expr, expr)" function
15841 */
15842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015843f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015844{
15845 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15846 | get_tv_number_chk(&argvars[1], NULL);
15847}
15848
15849/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015850 * "pathshorten()" function
15851 */
15852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015853f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015854{
15855 char_u *p;
15856
15857 rettv->v_type = VAR_STRING;
15858 p = get_tv_string_chk(&argvars[0]);
15859 if (p == NULL)
15860 rettv->vval.v_string = NULL;
15861 else
15862 {
15863 p = vim_strsave(p);
15864 rettv->vval.v_string = p;
15865 if (p != NULL)
15866 shorten_dir(p);
15867 }
15868}
15869
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015870#ifdef FEAT_PERL
15871/*
15872 * "perleval()" function
15873 */
15874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015875f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015876{
15877 char_u *str;
15878 char_u buf[NUMBUFLEN];
15879
15880 str = get_tv_string_buf(&argvars[0], buf);
15881 do_perleval(str, rettv);
15882}
15883#endif
15884
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015885#ifdef FEAT_FLOAT
15886/*
15887 * "pow()" function
15888 */
15889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015890f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015891{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015892 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015893
15894 rettv->v_type = VAR_FLOAT;
15895 if (get_float_arg(argvars, &fx) == OK
15896 && get_float_arg(&argvars[1], &fy) == OK)
15897 rettv->vval.v_float = pow(fx, fy);
15898 else
15899 rettv->vval.v_float = 0.0;
15900}
15901#endif
15902
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015903/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015904 * "prevnonblank()" function
15905 */
15906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015907f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015908{
15909 linenr_T lnum;
15910
15911 lnum = get_tv_lnum(argvars);
15912 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15913 lnum = 0;
15914 else
15915 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15916 --lnum;
15917 rettv->vval.v_number = lnum;
15918}
15919
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015920/* This dummy va_list is here because:
15921 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15922 * - locally in the function results in a "used before set" warning
15923 * - using va_start() to initialize it gives "function with fixed args" error */
15924static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015925
Bram Moolenaar8c711452005-01-14 21:53:12 +000015926/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015927 * "printf()" function
15928 */
15929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015930f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015931{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015932 char_u buf[NUMBUFLEN];
15933 int len;
15934 char_u *s;
15935 int saved_did_emsg = did_emsg;
15936 char *fmt;
15937
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015938 rettv->v_type = VAR_STRING;
15939 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015940
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015941 /* Get the required length, allocate the buffer and do it for real. */
15942 did_emsg = FALSE;
15943 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15944 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15945 if (!did_emsg)
15946 {
15947 s = alloc(len + 1);
15948 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015949 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015950 rettv->vval.v_string = s;
15951 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015952 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015953 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015954 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015955}
15956
15957/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015958 * "pumvisible()" function
15959 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015961f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015962{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015963#ifdef FEAT_INS_EXPAND
15964 if (pum_visible())
15965 rettv->vval.v_number = 1;
15966#endif
15967}
15968
Bram Moolenaardb913952012-06-29 12:54:53 +020015969#ifdef FEAT_PYTHON3
15970/*
15971 * "py3eval()" function
15972 */
15973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015974f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015975{
15976 char_u *str;
15977 char_u buf[NUMBUFLEN];
15978
15979 str = get_tv_string_buf(&argvars[0], buf);
15980 do_py3eval(str, rettv);
15981}
15982#endif
15983
15984#ifdef FEAT_PYTHON
15985/*
15986 * "pyeval()" function
15987 */
15988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015989f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015990{
15991 char_u *str;
15992 char_u buf[NUMBUFLEN];
15993
15994 str = get_tv_string_buf(&argvars[0], buf);
15995 do_pyeval(str, rettv);
15996}
15997#endif
15998
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015999/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016000 * "range()" function
16001 */
16002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016003f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016004{
16005 long start;
16006 long end;
16007 long stride = 1;
16008 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016009 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016010
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016011 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016012 if (argvars[1].v_type == VAR_UNKNOWN)
16013 {
16014 end = start - 1;
16015 start = 0;
16016 }
16017 else
16018 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016019 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016020 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016021 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016022 }
16023
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016024 if (error)
16025 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016026 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016027 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016028 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016029 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016030 else
16031 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016032 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016033 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016034 if (list_append_number(rettv->vval.v_list,
16035 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016036 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016037 }
16038}
16039
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016040/*
16041 * "readfile()" function
16042 */
16043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016044f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016045{
16046 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016047 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016048 char_u *fname;
16049 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016050 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16051 int io_size = sizeof(buf);
16052 int readlen; /* size of last fread() */
16053 char_u *prev = NULL; /* previously read bytes, if any */
16054 long prevlen = 0; /* length of data in prev */
16055 long prevsize = 0; /* size of prev buffer */
16056 long maxline = MAXLNUM;
16057 long cnt = 0;
16058 char_u *p; /* position in buf */
16059 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016060
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016061 if (argvars[1].v_type != VAR_UNKNOWN)
16062 {
16063 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16064 binary = TRUE;
16065 if (argvars[2].v_type != VAR_UNKNOWN)
16066 maxline = get_tv_number(&argvars[2]);
16067 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016068
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016069 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016070 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016071
16072 /* Always open the file in binary mode, library functions have a mind of
16073 * their own about CR-LF conversion. */
16074 fname = get_tv_string(&argvars[0]);
16075 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16076 {
16077 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16078 return;
16079 }
16080
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016081 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016082 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016083 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016084
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016085 /* This for loop processes what was read, but is also entered at end
16086 * of file so that either:
16087 * - an incomplete line gets written
16088 * - a "binary" file gets an empty line at the end if it ends in a
16089 * newline. */
16090 for (p = buf, start = buf;
16091 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16092 ++p)
16093 {
16094 if (*p == '\n' || readlen <= 0)
16095 {
16096 listitem_T *li;
16097 char_u *s = NULL;
16098 long_u len = p - start;
16099
16100 /* Finished a line. Remove CRs before NL. */
16101 if (readlen > 0 && !binary)
16102 {
16103 while (len > 0 && start[len - 1] == '\r')
16104 --len;
16105 /* removal may cross back to the "prev" string */
16106 if (len == 0)
16107 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16108 --prevlen;
16109 }
16110 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016111 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016112 else
16113 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016114 /* Change "prev" buffer to be the right size. This way
16115 * the bytes are only copied once, and very long lines are
16116 * allocated only once. */
16117 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016118 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016119 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016120 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016121 prev = NULL; /* the list will own the string */
16122 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016123 }
16124 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016125 if (s == NULL)
16126 {
16127 do_outofmem_msg((long_u) prevlen + len + 1);
16128 failed = TRUE;
16129 break;
16130 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016131
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016132 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016133 {
16134 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016135 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016136 break;
16137 }
16138 li->li_tv.v_type = VAR_STRING;
16139 li->li_tv.v_lock = 0;
16140 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016141 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016142
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016143 start = p + 1; /* step over newline */
16144 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016145 break;
16146 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016147 else if (*p == NUL)
16148 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016149#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016150 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16151 * when finding the BF and check the previous two bytes. */
16152 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016153 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016154 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16155 * + 1, these may be in the "prev" string. */
16156 char_u back1 = p >= buf + 1 ? p[-1]
16157 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16158 char_u back2 = p >= buf + 2 ? p[-2]
16159 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16160 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016161
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016162 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016163 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016164 char_u *dest = p - 2;
16165
16166 /* Usually a BOM is at the beginning of a file, and so at
16167 * the beginning of a line; then we can just step over it.
16168 */
16169 if (start == dest)
16170 start = p + 1;
16171 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016172 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016173 /* have to shuffle buf to close gap */
16174 int adjust_prevlen = 0;
16175
16176 if (dest < buf)
16177 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016178 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016179 dest = buf;
16180 }
16181 if (readlen > p - buf + 1)
16182 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16183 readlen -= 3 - adjust_prevlen;
16184 prevlen -= adjust_prevlen;
16185 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016186 }
16187 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016188 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016189#endif
16190 } /* for */
16191
16192 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16193 break;
16194 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016195 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016196 /* There's part of a line in buf, store it in "prev". */
16197 if (p - start + prevlen >= prevsize)
16198 {
16199 /* need bigger "prev" buffer */
16200 char_u *newprev;
16201
16202 /* A common use case is ordinary text files and "prev" gets a
16203 * fragment of a line, so the first allocation is made
16204 * small, to avoid repeatedly 'allocing' large and
16205 * 'reallocing' small. */
16206 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016207 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016208 else
16209 {
16210 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016211 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016212 prevsize = grow50pc > growmin ? grow50pc : growmin;
16213 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016214 newprev = prev == NULL ? alloc(prevsize)
16215 : vim_realloc(prev, prevsize);
16216 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016217 {
16218 do_outofmem_msg((long_u)prevsize);
16219 failed = TRUE;
16220 break;
16221 }
16222 prev = newprev;
16223 }
16224 /* Add the line part to end of "prev". */
16225 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016226 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016227 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016228 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016229
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016230 /*
16231 * For a negative line count use only the lines at the end of the file,
16232 * free the rest.
16233 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016234 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016235 while (cnt > -maxline)
16236 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016237 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016238 --cnt;
16239 }
16240
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016241 if (failed)
16242 {
16243 list_free(rettv->vval.v_list, TRUE);
16244 /* readfile doc says an empty list is returned on error */
16245 rettv->vval.v_list = list_alloc();
16246 }
16247
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016248 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016249 fclose(fd);
16250}
16251
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016252#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016253static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016254
16255/*
16256 * Convert a List to proftime_T.
16257 * Return FAIL when there is something wrong.
16258 */
16259 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016260list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016261{
16262 long n1, n2;
16263 int error = FALSE;
16264
16265 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16266 || arg->vval.v_list->lv_len != 2)
16267 return FAIL;
16268 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16269 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16270# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016271 tm->HighPart = n1;
16272 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016273# else
16274 tm->tv_sec = n1;
16275 tm->tv_usec = n2;
16276# endif
16277 return error ? FAIL : OK;
16278}
16279#endif /* FEAT_RELTIME */
16280
16281/*
16282 * "reltime()" function
16283 */
16284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016285f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016286{
16287#ifdef FEAT_RELTIME
16288 proftime_T res;
16289 proftime_T start;
16290
16291 if (argvars[0].v_type == VAR_UNKNOWN)
16292 {
16293 /* No arguments: get current time. */
16294 profile_start(&res);
16295 }
16296 else if (argvars[1].v_type == VAR_UNKNOWN)
16297 {
16298 if (list2proftime(&argvars[0], &res) == FAIL)
16299 return;
16300 profile_end(&res);
16301 }
16302 else
16303 {
16304 /* Two arguments: compute the difference. */
16305 if (list2proftime(&argvars[0], &start) == FAIL
16306 || list2proftime(&argvars[1], &res) == FAIL)
16307 return;
16308 profile_sub(&res, &start);
16309 }
16310
16311 if (rettv_list_alloc(rettv) == OK)
16312 {
16313 long n1, n2;
16314
16315# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016316 n1 = res.HighPart;
16317 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016318# else
16319 n1 = res.tv_sec;
16320 n2 = res.tv_usec;
16321# endif
16322 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16323 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16324 }
16325#endif
16326}
16327
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016328#ifdef FEAT_FLOAT
16329/*
16330 * "reltimefloat()" function
16331 */
16332 static void
16333f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16334{
16335# ifdef FEAT_RELTIME
16336 proftime_T tm;
16337# endif
16338
16339 rettv->v_type = VAR_FLOAT;
16340 rettv->vval.v_float = 0;
16341# ifdef FEAT_RELTIME
16342 if (list2proftime(&argvars[0], &tm) == OK)
16343 rettv->vval.v_float = profile_float(&tm);
16344# endif
16345}
16346#endif
16347
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016348/*
16349 * "reltimestr()" function
16350 */
16351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016352f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016353{
16354#ifdef FEAT_RELTIME
16355 proftime_T tm;
16356#endif
16357
16358 rettv->v_type = VAR_STRING;
16359 rettv->vval.v_string = NULL;
16360#ifdef FEAT_RELTIME
16361 if (list2proftime(&argvars[0], &tm) == OK)
16362 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16363#endif
16364}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016365
Bram Moolenaar0d660222005-01-07 21:51:51 +000016366#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016367static void make_connection(void);
16368static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016369
16370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016371make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016372{
16373 if (X_DISPLAY == NULL
16374# ifdef FEAT_GUI
16375 && !gui.in_use
16376# endif
16377 )
16378 {
16379 x_force_connect = TRUE;
16380 setup_term_clip();
16381 x_force_connect = FALSE;
16382 }
16383}
16384
16385 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016386check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016387{
16388 make_connection();
16389 if (X_DISPLAY == NULL)
16390 {
16391 EMSG(_("E240: No connection to Vim server"));
16392 return FAIL;
16393 }
16394 return OK;
16395}
16396#endif
16397
16398#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016399static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016400
16401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016402remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016403{
16404 char_u *server_name;
16405 char_u *keys;
16406 char_u *r = NULL;
16407 char_u buf[NUMBUFLEN];
16408# ifdef WIN32
16409 HWND w;
16410# else
16411 Window w;
16412# endif
16413
16414 if (check_restricted() || check_secure())
16415 return;
16416
16417# ifdef FEAT_X11
16418 if (check_connection() == FAIL)
16419 return;
16420# endif
16421
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016422 server_name = get_tv_string_chk(&argvars[0]);
16423 if (server_name == NULL)
16424 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016425 keys = get_tv_string_buf(&argvars[1], buf);
16426# ifdef WIN32
16427 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16428# else
16429 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16430 < 0)
16431# endif
16432 {
16433 if (r != NULL)
16434 EMSG(r); /* sending worked but evaluation failed */
16435 else
16436 EMSG2(_("E241: Unable to send to %s"), server_name);
16437 return;
16438 }
16439
16440 rettv->vval.v_string = r;
16441
16442 if (argvars[2].v_type != VAR_UNKNOWN)
16443 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016444 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016445 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016446 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016447
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016448 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016449 v.di_tv.v_type = VAR_STRING;
16450 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016451 idvar = get_tv_string_chk(&argvars[2]);
16452 if (idvar != NULL)
16453 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016454 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016455 }
16456}
16457#endif
16458
16459/*
16460 * "remote_expr()" function
16461 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016463f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016464{
16465 rettv->v_type = VAR_STRING;
16466 rettv->vval.v_string = NULL;
16467#ifdef FEAT_CLIENTSERVER
16468 remote_common(argvars, rettv, TRUE);
16469#endif
16470}
16471
16472/*
16473 * "remote_foreground()" function
16474 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016476f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016477{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016478#ifdef FEAT_CLIENTSERVER
16479# ifdef WIN32
16480 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016481 {
16482 char_u *server_name = get_tv_string_chk(&argvars[0]);
16483
16484 if (server_name != NULL)
16485 serverForeground(server_name);
16486 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016487# else
16488 /* Send a foreground() expression to the server. */
16489 argvars[1].v_type = VAR_STRING;
16490 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16491 argvars[2].v_type = VAR_UNKNOWN;
16492 remote_common(argvars, rettv, TRUE);
16493 vim_free(argvars[1].vval.v_string);
16494# endif
16495#endif
16496}
16497
Bram Moolenaar0d660222005-01-07 21:51:51 +000016498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016499f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016500{
16501#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016502 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016503 char_u *s = NULL;
16504# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016505 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016506# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016507 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016508
16509 if (check_restricted() || check_secure())
16510 {
16511 rettv->vval.v_number = -1;
16512 return;
16513 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016514 serverid = get_tv_string_chk(&argvars[0]);
16515 if (serverid == NULL)
16516 {
16517 rettv->vval.v_number = -1;
16518 return; /* type error; errmsg already given */
16519 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016520# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016521 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016522 if (n == 0)
16523 rettv->vval.v_number = -1;
16524 else
16525 {
16526 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16527 rettv->vval.v_number = (s != NULL);
16528 }
16529# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016530 if (check_connection() == FAIL)
16531 return;
16532
16533 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016534 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016535# endif
16536
16537 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16538 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016539 char_u *retvar;
16540
Bram Moolenaar33570922005-01-25 22:26:29 +000016541 v.di_tv.v_type = VAR_STRING;
16542 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016543 retvar = get_tv_string_chk(&argvars[1]);
16544 if (retvar != NULL)
16545 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016546 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016547 }
16548#else
16549 rettv->vval.v_number = -1;
16550#endif
16551}
16552
Bram Moolenaar0d660222005-01-07 21:51:51 +000016553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016554f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016555{
16556 char_u *r = NULL;
16557
16558#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016559 char_u *serverid = get_tv_string_chk(&argvars[0]);
16560
16561 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016562 {
16563# ifdef WIN32
16564 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016565 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016566
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016567 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016568 if (n != 0)
16569 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16570 if (r == NULL)
16571# else
16572 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016573 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016574# endif
16575 EMSG(_("E277: Unable to read a server reply"));
16576 }
16577#endif
16578 rettv->v_type = VAR_STRING;
16579 rettv->vval.v_string = r;
16580}
16581
16582/*
16583 * "remote_send()" function
16584 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016586f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016587{
16588 rettv->v_type = VAR_STRING;
16589 rettv->vval.v_string = NULL;
16590#ifdef FEAT_CLIENTSERVER
16591 remote_common(argvars, rettv, FALSE);
16592#endif
16593}
16594
16595/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016596 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016597 */
16598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016599f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016600{
Bram Moolenaar33570922005-01-25 22:26:29 +000016601 list_T *l;
16602 listitem_T *item, *item2;
16603 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016604 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016605 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016606 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016607 dict_T *d;
16608 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016609 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016610
Bram Moolenaar8c711452005-01-14 21:53:12 +000016611 if (argvars[0].v_type == VAR_DICT)
16612 {
16613 if (argvars[2].v_type != VAR_UNKNOWN)
16614 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016615 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016616 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016617 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016618 key = get_tv_string_chk(&argvars[1]);
16619 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016621 di = dict_find(d, key, -1);
16622 if (di == NULL)
16623 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016624 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16625 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016626 {
16627 *rettv = di->di_tv;
16628 init_tv(&di->di_tv);
16629 dictitem_remove(d, di);
16630 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016631 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016632 }
16633 }
16634 else if (argvars[0].v_type != VAR_LIST)
16635 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016636 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016637 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016638 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016639 int error = FALSE;
16640
16641 idx = get_tv_number_chk(&argvars[1], &error);
16642 if (error)
16643 ; /* type error: do nothing, errmsg already given */
16644 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016645 EMSGN(_(e_listidx), idx);
16646 else
16647 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016648 if (argvars[2].v_type == VAR_UNKNOWN)
16649 {
16650 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016651 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016652 *rettv = item->li_tv;
16653 vim_free(item);
16654 }
16655 else
16656 {
16657 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016658 end = get_tv_number_chk(&argvars[2], &error);
16659 if (error)
16660 ; /* type error: do nothing */
16661 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016662 EMSGN(_(e_listidx), end);
16663 else
16664 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016665 int cnt = 0;
16666
16667 for (li = item; li != NULL; li = li->li_next)
16668 {
16669 ++cnt;
16670 if (li == item2)
16671 break;
16672 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016673 if (li == NULL) /* didn't find "item2" after "item" */
16674 EMSG(_(e_invrange));
16675 else
16676 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016677 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016678 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016679 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016680 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016681 l->lv_first = item;
16682 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016683 item->li_prev = NULL;
16684 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016685 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016686 }
16687 }
16688 }
16689 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016690 }
16691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016692}
16693
16694/*
16695 * "rename({from}, {to})" function
16696 */
16697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016698f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016699{
16700 char_u buf[NUMBUFLEN];
16701
16702 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016703 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016704 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016705 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16706 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016707}
16708
16709/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016710 * "repeat()" function
16711 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016713f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016714{
16715 char_u *p;
16716 int n;
16717 int slen;
16718 int len;
16719 char_u *r;
16720 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016721
16722 n = get_tv_number(&argvars[1]);
16723 if (argvars[0].v_type == VAR_LIST)
16724 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016725 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016726 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016727 if (list_extend(rettv->vval.v_list,
16728 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016729 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016730 }
16731 else
16732 {
16733 p = get_tv_string(&argvars[0]);
16734 rettv->v_type = VAR_STRING;
16735 rettv->vval.v_string = NULL;
16736
16737 slen = (int)STRLEN(p);
16738 len = slen * n;
16739 if (len <= 0)
16740 return;
16741
16742 r = alloc(len + 1);
16743 if (r != NULL)
16744 {
16745 for (i = 0; i < n; i++)
16746 mch_memmove(r + i * slen, p, (size_t)slen);
16747 r[len] = NUL;
16748 }
16749
16750 rettv->vval.v_string = r;
16751 }
16752}
16753
16754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016755 * "resolve()" function
16756 */
16757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016758f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759{
16760 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016761#ifdef HAVE_READLINK
16762 char_u *buf = NULL;
16763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016765 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766#ifdef FEAT_SHORTCUT
16767 {
16768 char_u *v = NULL;
16769
16770 v = mch_resolve_shortcut(p);
16771 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016772 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016774 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016775 }
16776#else
16777# ifdef HAVE_READLINK
16778 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016779 char_u *cpy;
16780 int len;
16781 char_u *remain = NULL;
16782 char_u *q;
16783 int is_relative_to_current = FALSE;
16784 int has_trailing_pathsep = FALSE;
16785 int limit = 100;
16786
16787 p = vim_strsave(p);
16788
16789 if (p[0] == '.' && (vim_ispathsep(p[1])
16790 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16791 is_relative_to_current = TRUE;
16792
16793 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016794 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016795 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016796 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016797 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016799
16800 q = getnextcomp(p);
16801 if (*q != NUL)
16802 {
16803 /* Separate the first path component in "p", and keep the
16804 * remainder (beginning with the path separator). */
16805 remain = vim_strsave(q - 1);
16806 q[-1] = NUL;
16807 }
16808
Bram Moolenaard9462e32011-04-11 21:35:11 +020016809 buf = alloc(MAXPATHL + 1);
16810 if (buf == NULL)
16811 goto fail;
16812
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813 for (;;)
16814 {
16815 for (;;)
16816 {
16817 len = readlink((char *)p, (char *)buf, MAXPATHL);
16818 if (len <= 0)
16819 break;
16820 buf[len] = NUL;
16821
16822 if (limit-- == 0)
16823 {
16824 vim_free(p);
16825 vim_free(remain);
16826 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016827 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016828 goto fail;
16829 }
16830
16831 /* Ensure that the result will have a trailing path separator
16832 * if the argument has one. */
16833 if (remain == NULL && has_trailing_pathsep)
16834 add_pathsep(buf);
16835
16836 /* Separate the first path component in the link value and
16837 * concatenate the remainders. */
16838 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16839 if (*q != NUL)
16840 {
16841 if (remain == NULL)
16842 remain = vim_strsave(q - 1);
16843 else
16844 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016845 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016846 if (cpy != NULL)
16847 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848 vim_free(remain);
16849 remain = cpy;
16850 }
16851 }
16852 q[-1] = NUL;
16853 }
16854
16855 q = gettail(p);
16856 if (q > p && *q == NUL)
16857 {
16858 /* Ignore trailing path separator. */
16859 q[-1] = NUL;
16860 q = gettail(p);
16861 }
16862 if (q > p && !mch_isFullName(buf))
16863 {
16864 /* symlink is relative to directory of argument */
16865 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16866 if (cpy != NULL)
16867 {
16868 STRCPY(cpy, p);
16869 STRCPY(gettail(cpy), buf);
16870 vim_free(p);
16871 p = cpy;
16872 }
16873 }
16874 else
16875 {
16876 vim_free(p);
16877 p = vim_strsave(buf);
16878 }
16879 }
16880
16881 if (remain == NULL)
16882 break;
16883
16884 /* Append the first path component of "remain" to "p". */
16885 q = getnextcomp(remain + 1);
16886 len = q - remain - (*q != NUL);
16887 cpy = vim_strnsave(p, STRLEN(p) + len);
16888 if (cpy != NULL)
16889 {
16890 STRNCAT(cpy, remain, len);
16891 vim_free(p);
16892 p = cpy;
16893 }
16894 /* Shorten "remain". */
16895 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016896 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016897 else
16898 {
16899 vim_free(remain);
16900 remain = NULL;
16901 }
16902 }
16903
16904 /* If the result is a relative path name, make it explicitly relative to
16905 * the current directory if and only if the argument had this form. */
16906 if (!vim_ispathsep(*p))
16907 {
16908 if (is_relative_to_current
16909 && *p != NUL
16910 && !(p[0] == '.'
16911 && (p[1] == NUL
16912 || vim_ispathsep(p[1])
16913 || (p[1] == '.'
16914 && (p[2] == NUL
16915 || vim_ispathsep(p[2]))))))
16916 {
16917 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016918 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016919 if (cpy != NULL)
16920 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921 vim_free(p);
16922 p = cpy;
16923 }
16924 }
16925 else if (!is_relative_to_current)
16926 {
16927 /* Strip leading "./". */
16928 q = p;
16929 while (q[0] == '.' && vim_ispathsep(q[1]))
16930 q += 2;
16931 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016932 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016933 }
16934 }
16935
16936 /* Ensure that the result will have no trailing path separator
16937 * if the argument had none. But keep "/" or "//". */
16938 if (!has_trailing_pathsep)
16939 {
16940 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016941 if (after_pathsep(p, q))
16942 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016943 }
16944
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016945 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016946 }
16947# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016948 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016949# endif
16950#endif
16951
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016952 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016953
16954#ifdef HAVE_READLINK
16955fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016956 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016958 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016959}
16960
16961/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016962 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963 */
16964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016965f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016966{
Bram Moolenaar33570922005-01-25 22:26:29 +000016967 list_T *l;
16968 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016969
Bram Moolenaar0d660222005-01-07 21:51:51 +000016970 if (argvars[0].v_type != VAR_LIST)
16971 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016972 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016973 && !tv_check_lock(l->lv_lock,
16974 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016975 {
16976 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016977 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016978 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016979 while (li != NULL)
16980 {
16981 ni = li->li_prev;
16982 list_append(l, li);
16983 li = ni;
16984 }
16985 rettv->vval.v_list = l;
16986 rettv->v_type = VAR_LIST;
16987 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016988 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990}
16991
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016992#define SP_NOMOVE 0x01 /* don't move cursor */
16993#define SP_REPEAT 0x02 /* repeat to find outer pair */
16994#define SP_RETCOUNT 0x04 /* return matchcount */
16995#define SP_SETPCMARK 0x08 /* set previous context mark */
16996#define SP_START 0x10 /* accept match at start position */
16997#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16998#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016999#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017000
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017001static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017002
17003/*
17004 * Get flags for a search function.
17005 * Possibly sets "p_ws".
17006 * Returns BACKWARD, FORWARD or zero (for an error).
17007 */
17008 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017009get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017010{
17011 int dir = FORWARD;
17012 char_u *flags;
17013 char_u nbuf[NUMBUFLEN];
17014 int mask;
17015
17016 if (varp->v_type != VAR_UNKNOWN)
17017 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017018 flags = get_tv_string_buf_chk(varp, nbuf);
17019 if (flags == NULL)
17020 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017021 while (*flags != NUL)
17022 {
17023 switch (*flags)
17024 {
17025 case 'b': dir = BACKWARD; break;
17026 case 'w': p_ws = TRUE; break;
17027 case 'W': p_ws = FALSE; break;
17028 default: mask = 0;
17029 if (flagsp != NULL)
17030 switch (*flags)
17031 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017032 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017033 case 'e': mask = SP_END; break;
17034 case 'm': mask = SP_RETCOUNT; break;
17035 case 'n': mask = SP_NOMOVE; break;
17036 case 'p': mask = SP_SUBPAT; break;
17037 case 'r': mask = SP_REPEAT; break;
17038 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017039 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017040 }
17041 if (mask == 0)
17042 {
17043 EMSG2(_(e_invarg2), flags);
17044 dir = 0;
17045 }
17046 else
17047 *flagsp |= mask;
17048 }
17049 if (dir == 0)
17050 break;
17051 ++flags;
17052 }
17053 }
17054 return dir;
17055}
17056
Bram Moolenaar071d4272004-06-13 20:20:40 +000017057/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017058 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017060 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017061search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017063 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064 char_u *pat;
17065 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017066 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067 int save_p_ws = p_ws;
17068 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017069 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017070 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017071 proftime_T tm;
17072#ifdef FEAT_RELTIME
17073 long time_limit = 0;
17074#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017075 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017076 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017078 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017079 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017080 if (dir == 0)
17081 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017082 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017083 if (flags & SP_START)
17084 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017085 if (flags & SP_END)
17086 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017087 if (flags & SP_COLUMN)
17088 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017089
Bram Moolenaar76929292008-01-06 19:07:36 +000017090 /* Optional arguments: line number to stop searching and timeout. */
17091 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017092 {
17093 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17094 if (lnum_stop < 0)
17095 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017096#ifdef FEAT_RELTIME
17097 if (argvars[3].v_type != VAR_UNKNOWN)
17098 {
17099 time_limit = get_tv_number_chk(&argvars[3], NULL);
17100 if (time_limit < 0)
17101 goto theend;
17102 }
17103#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017104 }
17105
Bram Moolenaar76929292008-01-06 19:07:36 +000017106#ifdef FEAT_RELTIME
17107 /* Set the time limit, if there is one. */
17108 profile_setlimit(time_limit, &tm);
17109#endif
17110
Bram Moolenaar231334e2005-07-25 20:46:57 +000017111 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017112 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017113 * Check to make sure only those flags are set.
17114 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17115 * flags cannot be set. Check for that condition also.
17116 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017117 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017118 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017119 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017120 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017121 goto theend;
17122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017123
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017124 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017125 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017126 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017127 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017128 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017129 if (flags & SP_SUBPAT)
17130 retval = subpatnum;
17131 else
17132 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017133 if (flags & SP_SETPCMARK)
17134 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017135 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017136 if (match_pos != NULL)
17137 {
17138 /* Store the match cursor position */
17139 match_pos->lnum = pos.lnum;
17140 match_pos->col = pos.col + 1;
17141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017142 /* "/$" will put the cursor after the end of the line, may need to
17143 * correct that here */
17144 check_cursor();
17145 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017146
17147 /* If 'n' flag is used: restore cursor position. */
17148 if (flags & SP_NOMOVE)
17149 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017150 else
17151 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017152theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017153 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017154
17155 return retval;
17156}
17157
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017158#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017159
17160/*
17161 * round() is not in C90, use ceil() or floor() instead.
17162 */
17163 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017164vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017165{
17166 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17167}
17168
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017169/*
17170 * "round({float})" function
17171 */
17172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017173f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017174{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017175 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017176
17177 rettv->v_type = VAR_FLOAT;
17178 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017179 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017180 else
17181 rettv->vval.v_float = 0.0;
17182}
17183#endif
17184
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017185/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017186 * "screenattr()" function
17187 */
17188 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017189f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017190{
17191 int row;
17192 int col;
17193 int c;
17194
17195 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17196 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17197 if (row < 0 || row >= screen_Rows
17198 || col < 0 || col >= screen_Columns)
17199 c = -1;
17200 else
17201 c = ScreenAttrs[LineOffset[row] + col];
17202 rettv->vval.v_number = c;
17203}
17204
17205/*
17206 * "screenchar()" function
17207 */
17208 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017209f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017210{
17211 int row;
17212 int col;
17213 int off;
17214 int c;
17215
17216 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17217 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17218 if (row < 0 || row >= screen_Rows
17219 || col < 0 || col >= screen_Columns)
17220 c = -1;
17221 else
17222 {
17223 off = LineOffset[row] + col;
17224#ifdef FEAT_MBYTE
17225 if (enc_utf8 && ScreenLinesUC[off] != 0)
17226 c = ScreenLinesUC[off];
17227 else
17228#endif
17229 c = ScreenLines[off];
17230 }
17231 rettv->vval.v_number = c;
17232}
17233
17234/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017235 * "screencol()" function
17236 *
17237 * First column is 1 to be consistent with virtcol().
17238 */
17239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017240f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017241{
17242 rettv->vval.v_number = screen_screencol() + 1;
17243}
17244
17245/*
17246 * "screenrow()" function
17247 */
17248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017249f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017250{
17251 rettv->vval.v_number = screen_screenrow() + 1;
17252}
17253
17254/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017255 * "search()" function
17256 */
17257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017258f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017259{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017260 int flags = 0;
17261
17262 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017263}
17264
Bram Moolenaar071d4272004-06-13 20:20:40 +000017265/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017266 * "searchdecl()" function
17267 */
17268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017269f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017270{
17271 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017272 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017273 int error = FALSE;
17274 char_u *name;
17275
17276 rettv->vval.v_number = 1; /* default: FAIL */
17277
17278 name = get_tv_string_chk(&argvars[0]);
17279 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017280 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017281 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017282 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17283 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17284 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017285 if (!error && name != NULL)
17286 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017287 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017288}
17289
17290/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017291 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017292 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017293 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017294searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295{
17296 char_u *spat, *mpat, *epat;
17297 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299 int dir;
17300 int flags = 0;
17301 char_u nbuf1[NUMBUFLEN];
17302 char_u nbuf2[NUMBUFLEN];
17303 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017304 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017305 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017306 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307
Bram Moolenaar071d4272004-06-13 20:20:40 +000017308 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017309 spat = get_tv_string_chk(&argvars[0]);
17310 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17311 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17312 if (spat == NULL || mpat == NULL || epat == NULL)
17313 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315 /* Handle the optional fourth argument: flags */
17316 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017317 if (dir == 0)
17318 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017319
17320 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017321 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17322 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017323 if ((flags & (SP_END | SP_SUBPAT)) != 0
17324 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017325 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017326 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017327 goto theend;
17328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017329
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017330 /* Using 'r' implies 'W', otherwise it doesn't work. */
17331 if (flags & SP_REPEAT)
17332 p_ws = FALSE;
17333
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017334 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017335 if (argvars[3].v_type == VAR_UNKNOWN
17336 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017337 skip = (char_u *)"";
17338 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017340 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017341 if (argvars[5].v_type != VAR_UNKNOWN)
17342 {
17343 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17344 if (lnum_stop < 0)
17345 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017346#ifdef FEAT_RELTIME
17347 if (argvars[6].v_type != VAR_UNKNOWN)
17348 {
17349 time_limit = get_tv_number_chk(&argvars[6], NULL);
17350 if (time_limit < 0)
17351 goto theend;
17352 }
17353#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017354 }
17355 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017356 if (skip == NULL)
17357 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017358
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017359 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017360 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017361
17362theend:
17363 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017364
17365 return retval;
17366}
17367
17368/*
17369 * "searchpair()" function
17370 */
17371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017372f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017373{
17374 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17375}
17376
17377/*
17378 * "searchpairpos()" function
17379 */
17380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017381f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017382{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017383 pos_T match_pos;
17384 int lnum = 0;
17385 int col = 0;
17386
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017387 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017388 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017389
17390 if (searchpair_cmn(argvars, &match_pos) > 0)
17391 {
17392 lnum = match_pos.lnum;
17393 col = match_pos.col;
17394 }
17395
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017396 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17397 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017398}
17399
17400/*
17401 * Search for a start/middle/end thing.
17402 * Used by searchpair(), see its documentation for the details.
17403 * Returns 0 or -1 for no match,
17404 */
17405 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017406do_searchpair(
17407 char_u *spat, /* start pattern */
17408 char_u *mpat, /* middle pattern */
17409 char_u *epat, /* end pattern */
17410 int dir, /* BACKWARD or FORWARD */
17411 char_u *skip, /* skip expression */
17412 int flags, /* SP_SETPCMARK and other SP_ values */
17413 pos_T *match_pos,
17414 linenr_T lnum_stop, /* stop at this line if not zero */
17415 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017416{
17417 char_u *save_cpo;
17418 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17419 long retval = 0;
17420 pos_T pos;
17421 pos_T firstpos;
17422 pos_T foundpos;
17423 pos_T save_cursor;
17424 pos_T save_pos;
17425 int n;
17426 int r;
17427 int nest = 1;
17428 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017429 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017430 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017431
17432 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17433 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017434 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017435
Bram Moolenaar76929292008-01-06 19:07:36 +000017436#ifdef FEAT_RELTIME
17437 /* Set the time limit, if there is one. */
17438 profile_setlimit(time_limit, &tm);
17439#endif
17440
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017441 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17442 * start/middle/end (pat3, for the top pair). */
17443 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17444 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17445 if (pat2 == NULL || pat3 == NULL)
17446 goto theend;
17447 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17448 if (*mpat == NUL)
17449 STRCPY(pat3, pat2);
17450 else
17451 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17452 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017453 if (flags & SP_START)
17454 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017455
Bram Moolenaar071d4272004-06-13 20:20:40 +000017456 save_cursor = curwin->w_cursor;
17457 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017458 clearpos(&firstpos);
17459 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017460 pat = pat3;
17461 for (;;)
17462 {
17463 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017464 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017465 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17466 /* didn't find it or found the first match again: FAIL */
17467 break;
17468
17469 if (firstpos.lnum == 0)
17470 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017471 if (equalpos(pos, foundpos))
17472 {
17473 /* Found the same position again. Can happen with a pattern that
17474 * has "\zs" at the end and searching backwards. Advance one
17475 * character and try again. */
17476 if (dir == BACKWARD)
17477 decl(&pos);
17478 else
17479 incl(&pos);
17480 }
17481 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017483 /* clear the start flag to avoid getting stuck here */
17484 options &= ~SEARCH_START;
17485
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486 /* If the skip pattern matches, ignore this match. */
17487 if (*skip != NUL)
17488 {
17489 save_pos = curwin->w_cursor;
17490 curwin->w_cursor = pos;
17491 r = eval_to_bool(skip, &err, NULL, FALSE);
17492 curwin->w_cursor = save_pos;
17493 if (err)
17494 {
17495 /* Evaluating {skip} caused an error, break here. */
17496 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017497 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498 break;
17499 }
17500 if (r)
17501 continue;
17502 }
17503
17504 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17505 {
17506 /* Found end when searching backwards or start when searching
17507 * forward: nested pair. */
17508 ++nest;
17509 pat = pat2; /* nested, don't search for middle */
17510 }
17511 else
17512 {
17513 /* Found end when searching forward or start when searching
17514 * backward: end of (nested) pair; or found middle in outer pair. */
17515 if (--nest == 1)
17516 pat = pat3; /* outer level, search for middle */
17517 }
17518
17519 if (nest == 0)
17520 {
17521 /* Found the match: return matchcount or line number. */
17522 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017523 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017524 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017525 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017526 if (flags & SP_SETPCMARK)
17527 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528 curwin->w_cursor = pos;
17529 if (!(flags & SP_REPEAT))
17530 break;
17531 nest = 1; /* search for next unmatched */
17532 }
17533 }
17534
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017535 if (match_pos != NULL)
17536 {
17537 /* Store the match cursor position */
17538 match_pos->lnum = curwin->w_cursor.lnum;
17539 match_pos->col = curwin->w_cursor.col + 1;
17540 }
17541
Bram Moolenaar071d4272004-06-13 20:20:40 +000017542 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017543 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017544 curwin->w_cursor = save_cursor;
17545
17546theend:
17547 vim_free(pat2);
17548 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017549 if (p_cpo == empty_option)
17550 p_cpo = save_cpo;
17551 else
17552 /* Darn, evaluating the {skip} expression changed the value. */
17553 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017554
17555 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556}
17557
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017558/*
17559 * "searchpos()" function
17560 */
17561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017562f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017563{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017564 pos_T match_pos;
17565 int lnum = 0;
17566 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017567 int n;
17568 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017569
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017570 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017571 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017572
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017573 n = search_cmn(argvars, &match_pos, &flags);
17574 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017575 {
17576 lnum = match_pos.lnum;
17577 col = match_pos.col;
17578 }
17579
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017580 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17581 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017582 if (flags & SP_SUBPAT)
17583 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017584}
17585
Bram Moolenaar0d660222005-01-07 21:51:51 +000017586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017587f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017588{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017589#ifdef FEAT_CLIENTSERVER
17590 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017591 char_u *server = get_tv_string_chk(&argvars[0]);
17592 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017593
Bram Moolenaar0d660222005-01-07 21:51:51 +000017594 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017595 if (server == NULL || reply == NULL)
17596 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017597 if (check_restricted() || check_secure())
17598 return;
17599# ifdef FEAT_X11
17600 if (check_connection() == FAIL)
17601 return;
17602# endif
17603
17604 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017606 EMSG(_("E258: Unable to send to client"));
17607 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017608 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017609 rettv->vval.v_number = 0;
17610#else
17611 rettv->vval.v_number = -1;
17612#endif
17613}
17614
Bram Moolenaar0d660222005-01-07 21:51:51 +000017615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017616f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017617{
17618 char_u *r = NULL;
17619
17620#ifdef FEAT_CLIENTSERVER
17621# ifdef WIN32
17622 r = serverGetVimNames();
17623# else
17624 make_connection();
17625 if (X_DISPLAY != NULL)
17626 r = serverGetVimNames(X_DISPLAY);
17627# endif
17628#endif
17629 rettv->v_type = VAR_STRING;
17630 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631}
17632
17633/*
17634 * "setbufvar()" function
17635 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017637f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638{
17639 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017641 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017642 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643 char_u nbuf[NUMBUFLEN];
17644
17645 if (check_restricted() || check_secure())
17646 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017647 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17648 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017649 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017650 varp = &argvars[2];
17651
17652 if (buf != NULL && varname != NULL && varp != NULL)
17653 {
17654 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656
17657 if (*varname == '&')
17658 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017659 long numval;
17660 char_u *strval;
17661 int error = FALSE;
17662
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017664 numval = get_tv_number_chk(varp, &error);
17665 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017666 if (!error && strval != NULL)
17667 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668 }
17669 else
17670 {
17671 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17672 if (bufvarname != NULL)
17673 {
17674 STRCPY(bufvarname, "b:");
17675 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017676 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677 vim_free(bufvarname);
17678 }
17679 }
17680
17681 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017684}
17685
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017687f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017688{
17689 dict_T *d;
17690 dictitem_T *di;
17691 char_u *csearch;
17692
17693 if (argvars[0].v_type != VAR_DICT)
17694 {
17695 EMSG(_(e_dictreq));
17696 return;
17697 }
17698
17699 if ((d = argvars[0].vval.v_dict) != NULL)
17700 {
17701 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17702 if (csearch != NULL)
17703 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017704#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017705 if (enc_utf8)
17706 {
17707 int pcc[MAX_MCO];
17708 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017709
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017710 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17711 }
17712 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017713#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017714 set_last_csearch(PTR2CHAR(csearch),
17715 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017716 }
17717
17718 di = dict_find(d, (char_u *)"forward", -1);
17719 if (di != NULL)
17720 set_csearch_direction(get_tv_number(&di->di_tv)
17721 ? FORWARD : BACKWARD);
17722
17723 di = dict_find(d, (char_u *)"until", -1);
17724 if (di != NULL)
17725 set_csearch_until(!!get_tv_number(&di->di_tv));
17726 }
17727}
17728
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729/*
17730 * "setcmdpos()" function
17731 */
17732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017733f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017735 int pos = (int)get_tv_number(&argvars[0]) - 1;
17736
17737 if (pos >= 0)
17738 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739}
17740
17741/*
Bram Moolenaar80492532016-03-08 17:08:53 +010017742 * "setfperm({fname}, {mode})" function
17743 */
17744 static void
17745f_setfperm(typval_T *argvars, typval_T *rettv)
17746{
17747 char_u *fname;
17748 char_u modebuf[NUMBUFLEN];
17749 char_u *mode_str;
17750 int i;
17751 int mask;
17752 int mode = 0;
17753
17754 rettv->vval.v_number = 0;
17755 fname = get_tv_string_chk(&argvars[0]);
17756 if (fname == NULL)
17757 return;
17758 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
17759 if (mode_str == NULL)
17760 return;
17761 if (STRLEN(mode_str) != 9)
17762 {
17763 EMSG2(_(e_invarg2), mode_str);
17764 return;
17765 }
17766
17767 mask = 1;
17768 for (i = 8; i >= 0; --i)
17769 {
17770 if (mode_str[i] != '-')
17771 mode |= mask;
17772 mask = mask << 1;
17773 }
17774 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
17775}
17776
17777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778 * "setline()" function
17779 */
17780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017781f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782{
17783 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017784 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017785 list_T *l = NULL;
17786 listitem_T *li = NULL;
17787 long added = 0;
17788 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017790 lnum = get_tv_lnum(&argvars[0]);
17791 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017792 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017793 l = argvars[1].vval.v_list;
17794 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017795 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017796 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017797 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017798
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017799 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017800 for (;;)
17801 {
17802 if (l != NULL)
17803 {
17804 /* list argument, get next string */
17805 if (li == NULL)
17806 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017807 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017808 li = li->li_next;
17809 }
17810
17811 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017812 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017813 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017814
17815 /* When coming here from Insert mode, sync undo, so that this can be
17816 * undone separately from what was previously inserted. */
17817 if (u_sync_once == 2)
17818 {
17819 u_sync_once = 1; /* notify that u_sync() was called */
17820 u_sync(TRUE);
17821 }
17822
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017823 if (lnum <= curbuf->b_ml.ml_line_count)
17824 {
17825 /* existing line, replace it */
17826 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17827 {
17828 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017829 if (lnum == curwin->w_cursor.lnum)
17830 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017831 rettv->vval.v_number = 0; /* OK */
17832 }
17833 }
17834 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17835 {
17836 /* lnum is one past the last line, append the line */
17837 ++added;
17838 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17839 rettv->vval.v_number = 0; /* OK */
17840 }
17841
17842 if (l == NULL) /* only one string argument */
17843 break;
17844 ++lnum;
17845 }
17846
17847 if (added > 0)
17848 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017849}
17850
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017851static 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 +000017852
Bram Moolenaar071d4272004-06-13 20:20:40 +000017853/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017854 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017855 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017857set_qf_ll_list(
17858 win_T *wp UNUSED,
17859 typval_T *list_arg UNUSED,
17860 typval_T *action_arg UNUSED,
17861 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017862{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017863#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017864 char_u *act;
17865 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017866#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017867
Bram Moolenaar2641f772005-03-25 21:58:17 +000017868 rettv->vval.v_number = -1;
17869
17870#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017871 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017872 EMSG(_(e_listreq));
17873 else
17874 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017875 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017876
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017877 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017878 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017879 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017880 if (act == NULL)
17881 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017882 if (*act == 'a' || *act == 'r')
17883 action = *act;
17884 }
17885
Bram Moolenaar81484f42012-12-05 15:16:47 +010017886 if (l != NULL && set_errorlist(wp, l, action,
17887 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017888 rettv->vval.v_number = 0;
17889 }
17890#endif
17891}
17892
17893/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017894 * "setloclist()" function
17895 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017897f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017898{
17899 win_T *win;
17900
17901 rettv->vval.v_number = -1;
17902
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017903 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017904 if (win != NULL)
17905 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17906}
17907
17908/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017909 * "setmatches()" function
17910 */
17911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017912f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017913{
17914#ifdef FEAT_SEARCH_EXTRA
17915 list_T *l;
17916 listitem_T *li;
17917 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017918 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017919
17920 rettv->vval.v_number = -1;
17921 if (argvars[0].v_type != VAR_LIST)
17922 {
17923 EMSG(_(e_listreq));
17924 return;
17925 }
17926 if ((l = argvars[0].vval.v_list) != NULL)
17927 {
17928
17929 /* To some extent make sure that we are dealing with a list from
17930 * "getmatches()". */
17931 li = l->lv_first;
17932 while (li != NULL)
17933 {
17934 if (li->li_tv.v_type != VAR_DICT
17935 || (d = li->li_tv.vval.v_dict) == NULL)
17936 {
17937 EMSG(_(e_invarg));
17938 return;
17939 }
17940 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017941 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17942 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017943 && dict_find(d, (char_u *)"priority", -1) != NULL
17944 && dict_find(d, (char_u *)"id", -1) != NULL))
17945 {
17946 EMSG(_(e_invarg));
17947 return;
17948 }
17949 li = li->li_next;
17950 }
17951
17952 clear_matches(curwin);
17953 li = l->lv_first;
17954 while (li != NULL)
17955 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017956 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017957 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017958 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017959 char_u *group;
17960 int priority;
17961 int id;
17962 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017963
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017964 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017965 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17966 {
17967 if (s == NULL)
17968 {
17969 s = list_alloc();
17970 if (s == NULL)
17971 return;
17972 }
17973
17974 /* match from matchaddpos() */
17975 for (i = 1; i < 9; i++)
17976 {
17977 sprintf((char *)buf, (char *)"pos%d", i);
17978 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17979 {
17980 if (di->di_tv.v_type != VAR_LIST)
17981 return;
17982
17983 list_append_tv(s, &di->di_tv);
17984 s->lv_refcount++;
17985 }
17986 else
17987 break;
17988 }
17989 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017990
17991 group = get_dict_string(d, (char_u *)"group", FALSE);
17992 priority = (int)get_dict_number(d, (char_u *)"priority");
17993 id = (int)get_dict_number(d, (char_u *)"id");
17994 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17995 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17996 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017997 if (i == 0)
17998 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017999 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018000 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018001 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018002 }
18003 else
18004 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018005 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018006 list_unref(s);
18007 s = NULL;
18008 }
18009
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018010 li = li->li_next;
18011 }
18012 rettv->vval.v_number = 0;
18013 }
18014#endif
18015}
18016
18017/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018018 * "setpos()" function
18019 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018021f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018022{
18023 pos_T pos;
18024 int fnum;
18025 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018026 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018027
Bram Moolenaar08250432008-02-13 11:42:46 +000018028 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018029 name = get_tv_string_chk(argvars);
18030 if (name != NULL)
18031 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018032 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018033 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018034 if (--pos.col < 0)
18035 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018036 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018037 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018038 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018039 if (fnum == curbuf->b_fnum)
18040 {
18041 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018042 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018043 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018044 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018045 curwin->w_set_curswant = FALSE;
18046 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018047 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018048 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018049 }
18050 else
18051 EMSG(_(e_invarg));
18052 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018053 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18054 {
18055 /* set mark */
18056 if (setmark_pos(name[1], &pos, fnum) == OK)
18057 rettv->vval.v_number = 0;
18058 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018059 else
18060 EMSG(_(e_invarg));
18061 }
18062 }
18063}
18064
18065/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018066 * "setqflist()" function
18067 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018069f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018070{
18071 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18072}
18073
18074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075 * "setreg()" function
18076 */
18077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018078f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079{
18080 int regname;
18081 char_u *strregname;
18082 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018083 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084 int append;
18085 char_u yank_type;
18086 long block_len;
18087
18088 block_len = -1;
18089 yank_type = MAUTO;
18090 append = FALSE;
18091
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018092 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018093 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018095 if (strregname == NULL)
18096 return; /* type error; errmsg already given */
18097 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098 if (regname == 0 || regname == '@')
18099 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018100
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018101 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018103 stropt = get_tv_string_chk(&argvars[2]);
18104 if (stropt == NULL)
18105 return; /* type error */
18106 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018107 switch (*stropt)
18108 {
18109 case 'a': case 'A': /* append */
18110 append = TRUE;
18111 break;
18112 case 'v': case 'c': /* character-wise selection */
18113 yank_type = MCHAR;
18114 break;
18115 case 'V': case 'l': /* line-wise selection */
18116 yank_type = MLINE;
18117 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118 case 'b': case Ctrl_V: /* block-wise selection */
18119 yank_type = MBLOCK;
18120 if (VIM_ISDIGIT(stropt[1]))
18121 {
18122 ++stropt;
18123 block_len = getdigits(&stropt) - 1;
18124 --stropt;
18125 }
18126 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018127 }
18128 }
18129
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018130 if (argvars[1].v_type == VAR_LIST)
18131 {
18132 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018133 char_u **allocval;
18134 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018135 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018136 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018137 int len = argvars[1].vval.v_list->lv_len;
18138 listitem_T *li;
18139
Bram Moolenaar7d647822014-04-05 21:28:56 +020018140 /* First half: use for pointers to result lines; second half: use for
18141 * pointers to allocated copies. */
18142 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018143 if (lstval == NULL)
18144 return;
18145 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018146 allocval = lstval + len + 2;
18147 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018148
18149 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18150 li = li->li_next)
18151 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018152 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018153 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018154 goto free_lstval;
18155 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018156 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018157 /* Need to make a copy, next get_tv_string_buf_chk() will
18158 * overwrite the string. */
18159 strval = vim_strsave(buf);
18160 if (strval == NULL)
18161 goto free_lstval;
18162 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018163 }
18164 *curval++ = strval;
18165 }
18166 *curval++ = NULL;
18167
18168 write_reg_contents_lst(regname, lstval, -1,
18169 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018170free_lstval:
18171 while (curallocval > allocval)
18172 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018173 vim_free(lstval);
18174 }
18175 else
18176 {
18177 strval = get_tv_string_chk(&argvars[1]);
18178 if (strval == NULL)
18179 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018180 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018182 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018183 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018184}
18185
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018186/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018187 * "settabvar()" function
18188 */
18189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018190f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018191{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018192#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018193 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018194 tabpage_T *tp;
18195#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018196 char_u *varname, *tabvarname;
18197 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018198
18199 rettv->vval.v_number = 0;
18200
18201 if (check_restricted() || check_secure())
18202 return;
18203
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018204#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018205 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018206#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018207 varname = get_tv_string_chk(&argvars[1]);
18208 varp = &argvars[2];
18209
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018210 if (varname != NULL && varp != NULL
18211#ifdef FEAT_WINDOWS
18212 && tp != NULL
18213#endif
18214 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018215 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018216#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018217 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018218 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018219#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018220
18221 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18222 if (tabvarname != NULL)
18223 {
18224 STRCPY(tabvarname, "t:");
18225 STRCPY(tabvarname + 2, varname);
18226 set_var(tabvarname, varp, TRUE);
18227 vim_free(tabvarname);
18228 }
18229
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018230#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018231 /* Restore current tabpage */
18232 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018233 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018234#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018235 }
18236}
18237
18238/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018239 * "settabwinvar()" function
18240 */
18241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018242f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018243{
18244 setwinvar(argvars, rettv, 1);
18245}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018246
18247/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018248 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018251f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018252{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018253 setwinvar(argvars, rettv, 0);
18254}
18255
18256/*
18257 * "setwinvar()" and "settabwinvar()" functions
18258 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018259
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018261setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018262{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263 win_T *win;
18264#ifdef FEAT_WINDOWS
18265 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018266 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018267 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018268#endif
18269 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018270 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018271 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018272 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018273
18274 if (check_restricted() || check_secure())
18275 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018276
18277#ifdef FEAT_WINDOWS
18278 if (off == 1)
18279 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18280 else
18281 tp = curtab;
18282#endif
18283 win = find_win_by_nr(&argvars[off], tp);
18284 varname = get_tv_string_chk(&argvars[off + 1]);
18285 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018286
18287 if (win != NULL && varname != NULL && varp != NULL)
18288 {
18289#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018290 need_switch_win = !(tp == curtab && win == curwin);
18291 if (!need_switch_win
18292 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018294 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018295 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018296 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018297 long numval;
18298 char_u *strval;
18299 int error = FALSE;
18300
18301 ++varname;
18302 numval = get_tv_number_chk(varp, &error);
18303 strval = get_tv_string_buf_chk(varp, nbuf);
18304 if (!error && strval != NULL)
18305 set_option_value(varname, numval, strval, OPT_LOCAL);
18306 }
18307 else
18308 {
18309 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18310 if (winvarname != NULL)
18311 {
18312 STRCPY(winvarname, "w:");
18313 STRCPY(winvarname + 2, varname);
18314 set_var(winvarname, varp, TRUE);
18315 vim_free(winvarname);
18316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018317 }
18318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018319#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018320 if (need_switch_win)
18321 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322#endif
18323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018324}
18325
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018326#ifdef FEAT_CRYPT
18327/*
18328 * "sha256({string})" function
18329 */
18330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018331f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018332{
18333 char_u *p;
18334
18335 p = get_tv_string(&argvars[0]);
18336 rettv->vval.v_string = vim_strsave(
18337 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18338 rettv->v_type = VAR_STRING;
18339}
18340#endif /* FEAT_CRYPT */
18341
Bram Moolenaar071d4272004-06-13 20:20:40 +000018342/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018343 * "shellescape({string})" function
18344 */
18345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018346f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018347{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018348 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018349 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018350 rettv->v_type = VAR_STRING;
18351}
18352
18353/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018354 * shiftwidth() function
18355 */
18356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018357f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018358{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018359 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018360}
18361
18362/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018363 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018364 */
18365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018366f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018367{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018368 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018369
Bram Moolenaar0d660222005-01-07 21:51:51 +000018370 p = get_tv_string(&argvars[0]);
18371 rettv->vval.v_string = vim_strsave(p);
18372 simplify_filename(rettv->vval.v_string); /* simplify in place */
18373 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018374}
18375
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018376#ifdef FEAT_FLOAT
18377/*
18378 * "sin()" function
18379 */
18380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018381f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018382{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018383 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018384
18385 rettv->v_type = VAR_FLOAT;
18386 if (get_float_arg(argvars, &f) == OK)
18387 rettv->vval.v_float = sin(f);
18388 else
18389 rettv->vval.v_float = 0.0;
18390}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018391
18392/*
18393 * "sinh()" function
18394 */
18395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018396f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018397{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018398 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018399
18400 rettv->v_type = VAR_FLOAT;
18401 if (get_float_arg(argvars, &f) == OK)
18402 rettv->vval.v_float = sinh(f);
18403 else
18404 rettv->vval.v_float = 0.0;
18405}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018406#endif
18407
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_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018413static int
18414#ifdef __BORLANDC__
18415 _RTLENTRYF
18416#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018417 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018418
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018419/* struct used in the array that's given to qsort() */
18420typedef struct
18421{
18422 listitem_T *item;
18423 int idx;
18424} sortItem_T;
18425
Bram Moolenaar0b962472016-02-22 22:51:33 +010018426/* struct storing information about current sort */
18427typedef struct
18428{
18429 int item_compare_ic;
18430 int item_compare_numeric;
18431 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018432#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018433 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018434#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018435 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018436 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018437 dict_T *item_compare_selfdict;
18438 int item_compare_func_err;
18439 int item_compare_keep_zero;
18440} sortinfo_T;
18441static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018442static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018443#define ITEM_COMPARE_FAIL 999
18444
Bram Moolenaar071d4272004-06-13 20:20:40 +000018445/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018446 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018447 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018448 static int
18449#ifdef __BORLANDC__
18450_RTLENTRYF
18451#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018452item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018453{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018454 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018455 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018456 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018457 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018458 int res;
18459 char_u numbuf1[NUMBUFLEN];
18460 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018462 si1 = (sortItem_T *)s1;
18463 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018464 tv1 = &si1->item->li_tv;
18465 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018466
Bram Moolenaar0b962472016-02-22 22:51:33 +010018467 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018468 {
18469 long v1 = get_tv_number(tv1);
18470 long v2 = get_tv_number(tv2);
18471
18472 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18473 }
18474
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018475#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018476 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018477 {
18478 float_T v1 = get_tv_float(tv1);
18479 float_T v2 = get_tv_float(tv2);
18480
18481 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18482 }
18483#endif
18484
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018485 /* tv2string() puts quotes around a string and allocates memory. Don't do
18486 * that for string variables. Use a single quote when comparing with a
18487 * non-string to do what the docs promise. */
18488 if (tv1->v_type == VAR_STRING)
18489 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018490 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018491 p1 = (char_u *)"'";
18492 else
18493 p1 = tv1->vval.v_string;
18494 }
18495 else
18496 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18497 if (tv2->v_type == VAR_STRING)
18498 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018499 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018500 p2 = (char_u *)"'";
18501 else
18502 p2 = tv2->vval.v_string;
18503 }
18504 else
18505 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018506 if (p1 == NULL)
18507 p1 = (char_u *)"";
18508 if (p2 == NULL)
18509 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018510 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018511 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018512 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018513 res = STRICMP(p1, p2);
18514 else
18515 res = STRCMP(p1, p2);
18516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018517 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018518 {
18519 double n1, n2;
18520 n1 = strtod((char *)p1, (char **)&p1);
18521 n2 = strtod((char *)p2, (char **)&p2);
18522 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18523 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018524
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018525 /* When the result would be zero, compare the item indexes. Makes the
18526 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018527 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018528 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018529
Bram Moolenaar0d660222005-01-07 21:51:51 +000018530 vim_free(tofree1);
18531 vim_free(tofree2);
18532 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018533}
18534
18535 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018536#ifdef __BORLANDC__
18537_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018538#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018539item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018540{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018541 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018542 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018543 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018544 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018545 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018546 char_u *func_name;
18547 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018548
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018549 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018550 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018551 return 0;
18552
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018553 si1 = (sortItem_T *)s1;
18554 si2 = (sortItem_T *)s2;
18555
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018556 if (partial == NULL)
18557 func_name = sortinfo->item_compare_func;
18558 else
18559 func_name = partial->pt_name;
18560
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018561 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018562 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018563 copy_tv(&si1->item->li_tv, &argv[0]);
18564 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018565
18566 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018567 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018568 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018569 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018570 clear_tv(&argv[0]);
18571 clear_tv(&argv[1]);
18572
18573 if (res == FAIL)
18574 res = ITEM_COMPARE_FAIL;
18575 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018576 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18577 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018578 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018579 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018580
18581 /* When the result would be zero, compare the pointers themselves. Makes
18582 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018583 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018584 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018585
Bram Moolenaar0d660222005-01-07 21:51:51 +000018586 return res;
18587}
18588
18589/*
18590 * "sort({list})" function
18591 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018593do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018594{
Bram Moolenaar33570922005-01-25 22:26:29 +000018595 list_T *l;
18596 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018597 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018598 sortinfo_T *old_sortinfo;
18599 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018600 long len;
18601 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602
Bram Moolenaar0b962472016-02-22 22:51:33 +010018603 /* Pointer to current info struct used in compare function. Save and
18604 * restore the current one for nested calls. */
18605 old_sortinfo = sortinfo;
18606 sortinfo = &info;
18607
Bram Moolenaar0d660222005-01-07 21:51:51 +000018608 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018609 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018610 else
18611 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018612 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018613 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018614 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18615 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018616 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018617 rettv->vval.v_list = l;
18618 rettv->v_type = VAR_LIST;
18619 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620
Bram Moolenaar0d660222005-01-07 21:51:51 +000018621 len = list_len(l);
18622 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018623 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624
Bram Moolenaar0b962472016-02-22 22:51:33 +010018625 info.item_compare_ic = FALSE;
18626 info.item_compare_numeric = FALSE;
18627 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018628#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018629 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018630#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018631 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018632 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018633 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018634 if (argvars[1].v_type != VAR_UNKNOWN)
18635 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018636 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018637 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018638 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018639 else if (argvars[1].v_type == VAR_PARTIAL)
18640 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018641 else
18642 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018643 int error = FALSE;
18644
18645 i = get_tv_number_chk(&argvars[1], &error);
18646 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018647 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018648 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018649 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010018650 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018651 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010018652 else if (i != 0)
18653 {
18654 EMSG(_(e_invarg));
18655 goto theend;
18656 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018657 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018658 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010018659 if (*info.item_compare_func == NUL)
18660 {
18661 /* empty string means default sort */
18662 info.item_compare_func = NULL;
18663 }
18664 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018665 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018666 info.item_compare_func = NULL;
18667 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018668 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018669 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018670 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018671 info.item_compare_func = NULL;
18672 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018673 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018674#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018675 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018676 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018677 info.item_compare_func = NULL;
18678 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018679 }
18680#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018681 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018682 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018683 info.item_compare_func = NULL;
18684 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018685 }
18686 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018687 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018688
18689 if (argvars[2].v_type != VAR_UNKNOWN)
18690 {
18691 /* optional third argument: {dict} */
18692 if (argvars[2].v_type != VAR_DICT)
18693 {
18694 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010018695 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018696 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018697 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018698 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018700
Bram Moolenaar0d660222005-01-07 21:51:51 +000018701 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018702 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018703 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018704 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705
Bram Moolenaar327aa022014-03-25 18:24:23 +010018706 i = 0;
18707 if (sort)
18708 {
18709 /* sort(): ptrs will be the list to sort */
18710 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018711 {
18712 ptrs[i].item = li;
18713 ptrs[i].idx = i;
18714 ++i;
18715 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018716
Bram Moolenaar0b962472016-02-22 22:51:33 +010018717 info.item_compare_func_err = FALSE;
18718 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018719 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018720 if ((info.item_compare_func != NULL
18721 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018722 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018723 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018724 EMSG(_("E702: Sort compare function failed"));
18725 else
18726 {
18727 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018728 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010018729 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018730 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010018731 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018732
Bram Moolenaar0b962472016-02-22 22:51:33 +010018733 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018734 {
18735 /* Clear the List and append the items in sorted order. */
18736 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18737 l->lv_len = 0;
18738 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018739 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018740 }
18741 }
18742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018744 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018745 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018746
18747 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018748 info.item_compare_func_err = FALSE;
18749 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018750 item_compare_func_ptr = info.item_compare_func != NULL
18751 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010018752 ? item_compare2 : item_compare;
18753
18754 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18755 li = li->li_next)
18756 {
18757 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18758 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018759 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018760 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018761 {
18762 EMSG(_("E882: Uniq compare function failed"));
18763 break;
18764 }
18765 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018766
Bram Moolenaar0b962472016-02-22 22:51:33 +010018767 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018768 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018769 while (--i >= 0)
18770 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018771 li = ptrs[i].item->li_next;
18772 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018773 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018774 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018775 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018776 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018777 list_fix_watch(l, li);
18778 listitem_free(li);
18779 l->lv_len--;
18780 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018781 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018782 }
18783
18784 vim_free(ptrs);
18785 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018786theend:
18787 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018788}
18789
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018790/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018791 * "sort({list})" function
18792 */
18793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018794f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018795{
18796 do_sort_uniq(argvars, rettv, TRUE);
18797}
18798
18799/*
18800 * "uniq({list})" function
18801 */
18802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018803f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018804{
18805 do_sort_uniq(argvars, rettv, FALSE);
18806}
18807
18808/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018809 * "soundfold({word})" function
18810 */
18811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018812f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018813{
18814 char_u *s;
18815
18816 rettv->v_type = VAR_STRING;
18817 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018818#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018819 rettv->vval.v_string = eval_soundfold(s);
18820#else
18821 rettv->vval.v_string = vim_strsave(s);
18822#endif
18823}
18824
18825/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018826 * "spellbadword()" function
18827 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018829f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018830{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018831 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018832 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018833 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018834
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018835 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018836 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018837
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018838#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018839 if (argvars[0].v_type == VAR_UNKNOWN)
18840 {
18841 /* Find the start and length of the badly spelled word. */
18842 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18843 if (len != 0)
18844 word = ml_get_cursor();
18845 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018846 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018847 {
18848 char_u *str = get_tv_string_chk(&argvars[0]);
18849 int capcol = -1;
18850
18851 if (str != NULL)
18852 {
18853 /* Check the argument for spelling. */
18854 while (*str != NUL)
18855 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018856 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018857 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018858 {
18859 word = str;
18860 break;
18861 }
18862 str += len;
18863 }
18864 }
18865 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018866#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018867
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018868 list_append_string(rettv->vval.v_list, word, len);
18869 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018870 attr == HLF_SPB ? "bad" :
18871 attr == HLF_SPR ? "rare" :
18872 attr == HLF_SPL ? "local" :
18873 attr == HLF_SPC ? "caps" :
18874 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018875}
18876
18877/*
18878 * "spellsuggest()" function
18879 */
18880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018881f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018882{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018883#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018884 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018885 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018886 int maxcount;
18887 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018888 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018889 listitem_T *li;
18890 int need_capital = FALSE;
18891#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018892
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018893 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018894 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018895
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018896#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018897 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018898 {
18899 str = get_tv_string(&argvars[0]);
18900 if (argvars[1].v_type != VAR_UNKNOWN)
18901 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018902 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018903 if (maxcount <= 0)
18904 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018905 if (argvars[2].v_type != VAR_UNKNOWN)
18906 {
18907 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18908 if (typeerr)
18909 return;
18910 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018911 }
18912 else
18913 maxcount = 25;
18914
Bram Moolenaar4770d092006-01-12 23:22:24 +000018915 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018916
18917 for (i = 0; i < ga.ga_len; ++i)
18918 {
18919 str = ((char_u **)ga.ga_data)[i];
18920
18921 li = listitem_alloc();
18922 if (li == NULL)
18923 vim_free(str);
18924 else
18925 {
18926 li->li_tv.v_type = VAR_STRING;
18927 li->li_tv.v_lock = 0;
18928 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018929 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018930 }
18931 }
18932 ga_clear(&ga);
18933 }
18934#endif
18935}
18936
Bram Moolenaar0d660222005-01-07 21:51:51 +000018937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018938f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018939{
18940 char_u *str;
18941 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018942 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018943 regmatch_T regmatch;
18944 char_u patbuf[NUMBUFLEN];
18945 char_u *save_cpo;
18946 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018947 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018948 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018949 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018950
18951 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18952 save_cpo = p_cpo;
18953 p_cpo = (char_u *)"";
18954
18955 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018956 if (argvars[1].v_type != VAR_UNKNOWN)
18957 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018958 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18959 if (pat == NULL)
18960 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018961 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018962 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018963 }
18964 if (pat == NULL || *pat == NUL)
18965 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018966
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018967 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018969 if (typeerr)
18970 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018971
Bram Moolenaar0d660222005-01-07 21:51:51 +000018972 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18973 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018974 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018975 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018976 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018977 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018978 if (*str == NUL)
18979 match = FALSE; /* empty item at the end */
18980 else
18981 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018982 if (match)
18983 end = regmatch.startp[0];
18984 else
18985 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018986 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18987 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018988 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018989 if (list_append_string(rettv->vval.v_list, str,
18990 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018991 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018992 }
18993 if (!match)
18994 break;
18995 /* Advance to just after the match. */
18996 if (regmatch.endp[0] > str)
18997 col = 0;
18998 else
18999 {
19000 /* Don't get stuck at the same match. */
19001#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019002 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019003#else
19004 col = 1;
19005#endif
19006 }
19007 str = regmatch.endp[0];
19008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019009
Bram Moolenaar473de612013-06-08 18:19:48 +020019010 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019012
Bram Moolenaar0d660222005-01-07 21:51:51 +000019013 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019014}
19015
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019016#ifdef FEAT_FLOAT
19017/*
19018 * "sqrt()" function
19019 */
19020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019021f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019022{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019023 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019024
19025 rettv->v_type = VAR_FLOAT;
19026 if (get_float_arg(argvars, &f) == OK)
19027 rettv->vval.v_float = sqrt(f);
19028 else
19029 rettv->vval.v_float = 0.0;
19030}
19031
19032/*
19033 * "str2float()" function
19034 */
19035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019036f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019037{
19038 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19039
19040 if (*p == '+')
19041 p = skipwhite(p + 1);
19042 (void)string2float(p, &rettv->vval.v_float);
19043 rettv->v_type = VAR_FLOAT;
19044}
19045#endif
19046
Bram Moolenaar2c932302006-03-18 21:42:09 +000019047/*
19048 * "str2nr()" function
19049 */
19050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019051f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019052{
19053 int base = 10;
19054 char_u *p;
19055 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019056 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019057
19058 if (argvars[1].v_type != VAR_UNKNOWN)
19059 {
19060 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019061 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019062 {
19063 EMSG(_(e_invarg));
19064 return;
19065 }
19066 }
19067
19068 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019069 if (*p == '+')
19070 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019071 switch (base)
19072 {
19073 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19074 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19075 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19076 default: what = 0;
19077 }
19078 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019079 rettv->vval.v_number = n;
19080}
19081
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082#ifdef HAVE_STRFTIME
19083/*
19084 * "strftime({format}[, {time}])" function
19085 */
19086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019087f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019088{
19089 char_u result_buf[256];
19090 struct tm *curtime;
19091 time_t seconds;
19092 char_u *p;
19093
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019094 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019096 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019097 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098 seconds = time(NULL);
19099 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019100 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101 curtime = localtime(&seconds);
19102 /* MSVC returns NULL for an invalid value of seconds. */
19103 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019104 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019105 else
19106 {
19107# ifdef FEAT_MBYTE
19108 vimconv_T conv;
19109 char_u *enc;
19110
19111 conv.vc_type = CONV_NONE;
19112 enc = enc_locale();
19113 convert_setup(&conv, p_enc, enc);
19114 if (conv.vc_type != CONV_NONE)
19115 p = string_convert(&conv, p, NULL);
19116# endif
19117 if (p != NULL)
19118 (void)strftime((char *)result_buf, sizeof(result_buf),
19119 (char *)p, curtime);
19120 else
19121 result_buf[0] = NUL;
19122
19123# ifdef FEAT_MBYTE
19124 if (conv.vc_type != CONV_NONE)
19125 vim_free(p);
19126 convert_setup(&conv, enc, p_enc);
19127 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019128 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019129 else
19130# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019131 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132
19133# ifdef FEAT_MBYTE
19134 /* Release conversion descriptors */
19135 convert_setup(&conv, NULL, NULL);
19136 vim_free(enc);
19137# endif
19138 }
19139}
19140#endif
19141
19142/*
19143 * "stridx()" function
19144 */
19145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019146f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019147{
19148 char_u buf[NUMBUFLEN];
19149 char_u *needle;
19150 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019151 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019152 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019153 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019154
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019155 needle = get_tv_string_chk(&argvars[1]);
19156 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019157 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019158 if (needle == NULL || haystack == NULL)
19159 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160
Bram Moolenaar33570922005-01-25 22:26:29 +000019161 if (argvars[2].v_type != VAR_UNKNOWN)
19162 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019163 int error = FALSE;
19164
19165 start_idx = get_tv_number_chk(&argvars[2], &error);
19166 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019167 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019168 if (start_idx >= 0)
19169 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019170 }
19171
19172 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19173 if (pos != NULL)
19174 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019175}
19176
19177/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019178 * "string()" function
19179 */
19180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019181f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019182{
19183 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019184 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019186 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019187 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019188 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019189 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019190 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019191}
19192
19193/*
19194 * "strlen()" function
19195 */
19196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019197f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019198{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019199 rettv->vval.v_number = (varnumber_T)(STRLEN(
19200 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019201}
19202
19203/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019204 * "strchars()" function
19205 */
19206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019207f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019208{
19209 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019210 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019211#ifdef FEAT_MBYTE
19212 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019213 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019214#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019215
19216 if (argvars[1].v_type != VAR_UNKNOWN)
19217 skipcc = get_tv_number_chk(&argvars[1], NULL);
19218 if (skipcc < 0 || skipcc > 1)
19219 EMSG(_(e_invarg));
19220 else
19221 {
19222#ifdef FEAT_MBYTE
19223 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19224 while (*s != NUL)
19225 {
19226 func_mb_ptr2char_adv(&s);
19227 ++len;
19228 }
19229 rettv->vval.v_number = len;
19230#else
19231 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19232#endif
19233 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019234}
19235
19236/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019237 * "strdisplaywidth()" function
19238 */
19239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019240f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019241{
19242 char_u *s = get_tv_string(&argvars[0]);
19243 int col = 0;
19244
19245 if (argvars[1].v_type != VAR_UNKNOWN)
19246 col = get_tv_number(&argvars[1]);
19247
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019248 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019249}
19250
19251/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019252 * "strwidth()" function
19253 */
19254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019255f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019256{
19257 char_u *s = get_tv_string(&argvars[0]);
19258
19259 rettv->vval.v_number = (varnumber_T)(
19260#ifdef FEAT_MBYTE
19261 mb_string2cells(s, -1)
19262#else
19263 STRLEN(s)
19264#endif
19265 );
19266}
19267
19268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019269 * "strpart()" function
19270 */
19271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019272f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273{
19274 char_u *p;
19275 int n;
19276 int len;
19277 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019278 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019279
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019280 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019281 slen = (int)STRLEN(p);
19282
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019283 n = get_tv_number_chk(&argvars[1], &error);
19284 if (error)
19285 len = 0;
19286 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019287 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019288 else
19289 len = slen - n; /* default len: all bytes that are available. */
19290
19291 /*
19292 * Only return the overlap between the specified part and the actual
19293 * string.
19294 */
19295 if (n < 0)
19296 {
19297 len += n;
19298 n = 0;
19299 }
19300 else if (n > slen)
19301 n = slen;
19302 if (len < 0)
19303 len = 0;
19304 else if (n + len > slen)
19305 len = slen - n;
19306
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019307 rettv->v_type = VAR_STRING;
19308 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019309}
19310
19311/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019312 * "strridx()" function
19313 */
19314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019315f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019316{
19317 char_u buf[NUMBUFLEN];
19318 char_u *needle;
19319 char_u *haystack;
19320 char_u *rest;
19321 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019322 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019323
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019324 needle = get_tv_string_chk(&argvars[1]);
19325 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019326
19327 rettv->vval.v_number = -1;
19328 if (needle == NULL || haystack == NULL)
19329 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019330
19331 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019332 if (argvars[2].v_type != VAR_UNKNOWN)
19333 {
19334 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019335 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019336 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019337 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019338 }
19339 else
19340 end_idx = haystack_len;
19341
Bram Moolenaar0d660222005-01-07 21:51:51 +000019342 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019343 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019344 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019345 lastmatch = haystack + end_idx;
19346 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019347 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019348 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019349 for (rest = haystack; *rest != '\0'; ++rest)
19350 {
19351 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019352 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019353 break;
19354 lastmatch = rest;
19355 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019356 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019357
19358 if (lastmatch == NULL)
19359 rettv->vval.v_number = -1;
19360 else
19361 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19362}
19363
19364/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365 * "strtrans()" function
19366 */
19367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019368f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019370 rettv->v_type = VAR_STRING;
19371 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019372}
19373
19374/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019375 * "submatch()" function
19376 */
19377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019378f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019379{
Bram Moolenaar41571762014-04-02 19:00:58 +020019380 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019381 int no;
19382 int retList = 0;
19383
19384 no = (int)get_tv_number_chk(&argvars[0], &error);
19385 if (error)
19386 return;
19387 error = FALSE;
19388 if (argvars[1].v_type != VAR_UNKNOWN)
19389 retList = get_tv_number_chk(&argvars[1], &error);
19390 if (error)
19391 return;
19392
19393 if (retList == 0)
19394 {
19395 rettv->v_type = VAR_STRING;
19396 rettv->vval.v_string = reg_submatch(no);
19397 }
19398 else
19399 {
19400 rettv->v_type = VAR_LIST;
19401 rettv->vval.v_list = reg_submatch_list(no);
19402 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019403}
19404
19405/*
19406 * "substitute()" function
19407 */
19408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019409f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019410{
19411 char_u patbuf[NUMBUFLEN];
19412 char_u subbuf[NUMBUFLEN];
19413 char_u flagsbuf[NUMBUFLEN];
19414
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019415 char_u *str = get_tv_string_chk(&argvars[0]);
19416 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19417 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19418 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19419
Bram Moolenaar0d660222005-01-07 21:51:51 +000019420 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019421 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19422 rettv->vval.v_string = NULL;
19423 else
19424 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019425}
19426
19427/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019428 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019431f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019432{
19433 int id = 0;
19434#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019435 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019436 long col;
19437 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019438 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019440 lnum = get_tv_lnum(argvars); /* -1 on type error */
19441 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19442 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019444 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019445 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019446 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019447#endif
19448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019449 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019450}
19451
19452/*
19453 * "synIDattr(id, what [, mode])" function
19454 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019456f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457{
19458 char_u *p = NULL;
19459#ifdef FEAT_SYN_HL
19460 int id;
19461 char_u *what;
19462 char_u *mode;
19463 char_u modebuf[NUMBUFLEN];
19464 int modec;
19465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019466 id = get_tv_number(&argvars[0]);
19467 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019468 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019470 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019471 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019472 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019473 modec = 0; /* replace invalid with current */
19474 }
19475 else
19476 {
19477#ifdef FEAT_GUI
19478 if (gui.in_use)
19479 modec = 'g';
19480 else
19481#endif
19482 if (t_colors > 1)
19483 modec = 'c';
19484 else
19485 modec = 't';
19486 }
19487
19488
19489 switch (TOLOWER_ASC(what[0]))
19490 {
19491 case 'b':
19492 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19493 p = highlight_color(id, what, modec);
19494 else /* bold */
19495 p = highlight_has_attr(id, HL_BOLD, modec);
19496 break;
19497
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019498 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019499 p = highlight_color(id, what, modec);
19500 break;
19501
19502 case 'i':
19503 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19504 p = highlight_has_attr(id, HL_INVERSE, modec);
19505 else /* italic */
19506 p = highlight_has_attr(id, HL_ITALIC, modec);
19507 break;
19508
19509 case 'n': /* name */
19510 p = get_highlight_name(NULL, id - 1);
19511 break;
19512
19513 case 'r': /* reverse */
19514 p = highlight_has_attr(id, HL_INVERSE, modec);
19515 break;
19516
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019517 case 's':
19518 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19519 p = highlight_color(id, what, modec);
19520 else /* standout */
19521 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522 break;
19523
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019524 case 'u':
19525 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19526 /* underline */
19527 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19528 else
19529 /* undercurl */
19530 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019531 break;
19532 }
19533
19534 if (p != NULL)
19535 p = vim_strsave(p);
19536#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019537 rettv->v_type = VAR_STRING;
19538 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019539}
19540
19541/*
19542 * "synIDtrans(id)" function
19543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019545f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019546{
19547 int id;
19548
19549#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019550 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019551
19552 if (id > 0)
19553 id = syn_get_final_id(id);
19554 else
19555#endif
19556 id = 0;
19557
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019558 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019559}
19560
19561/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019562 * "synconcealed(lnum, col)" function
19563 */
19564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019565f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019566{
19567#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19568 long lnum;
19569 long col;
19570 int syntax_flags = 0;
19571 int cchar;
19572 int matchid = 0;
19573 char_u str[NUMBUFLEN];
19574#endif
19575
19576 rettv->v_type = VAR_LIST;
19577 rettv->vval.v_list = NULL;
19578
19579#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19580 lnum = get_tv_lnum(argvars); /* -1 on type error */
19581 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19582
19583 vim_memset(str, NUL, sizeof(str));
19584
19585 if (rettv_list_alloc(rettv) != FAIL)
19586 {
19587 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19588 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19589 && curwin->w_p_cole > 0)
19590 {
19591 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19592 syntax_flags = get_syntax_info(&matchid);
19593
19594 /* get the conceal character */
19595 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19596 {
19597 cchar = syn_get_sub_char();
19598 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19599 cchar = lcs_conceal;
19600 if (cchar != NUL)
19601 {
19602# ifdef FEAT_MBYTE
19603 if (has_mbyte)
19604 (*mb_char2bytes)(cchar, str);
19605 else
19606# endif
19607 str[0] = cchar;
19608 }
19609 }
19610 }
19611
19612 list_append_number(rettv->vval.v_list,
19613 (syntax_flags & HL_CONCEAL) != 0);
19614 /* -1 to auto-determine strlen */
19615 list_append_string(rettv->vval.v_list, str, -1);
19616 list_append_number(rettv->vval.v_list, matchid);
19617 }
19618#endif
19619}
19620
19621/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019622 * "synstack(lnum, col)" function
19623 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019625f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019626{
19627#ifdef FEAT_SYN_HL
19628 long lnum;
19629 long col;
19630 int i;
19631 int id;
19632#endif
19633
19634 rettv->v_type = VAR_LIST;
19635 rettv->vval.v_list = NULL;
19636
19637#ifdef FEAT_SYN_HL
19638 lnum = get_tv_lnum(argvars); /* -1 on type error */
19639 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19640
19641 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019642 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019643 && rettv_list_alloc(rettv) != FAIL)
19644 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019645 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019646 for (i = 0; ; ++i)
19647 {
19648 id = syn_get_stack_item(i);
19649 if (id < 0)
19650 break;
19651 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19652 break;
19653 }
19654 }
19655#endif
19656}
19657
Bram Moolenaar071d4272004-06-13 20:20:40 +000019658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019659get_cmd_output_as_rettv(
19660 typval_T *argvars,
19661 typval_T *rettv,
19662 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019663{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019664 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019666 char_u *infile = NULL;
19667 char_u buf[NUMBUFLEN];
19668 int err = FALSE;
19669 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019670 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019671 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019672
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019673 rettv->v_type = VAR_STRING;
19674 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019675 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019676 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019677
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019678 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019679 {
19680 /*
19681 * Write the string to a temp file, to be used for input of the shell
19682 * command.
19683 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019684 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019685 {
19686 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019687 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019688 }
19689
19690 fd = mch_fopen((char *)infile, WRITEBIN);
19691 if (fd == NULL)
19692 {
19693 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019694 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019695 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019696 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019697 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019698 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19699 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019700 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019701 else
19702 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019703 size_t len;
19704
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019705 p = get_tv_string_buf_chk(&argvars[1], buf);
19706 if (p == NULL)
19707 {
19708 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019709 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019710 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019711 len = STRLEN(p);
19712 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019713 err = TRUE;
19714 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019715 if (fclose(fd) != 0)
19716 err = TRUE;
19717 if (err)
19718 {
19719 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019720 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019721 }
19722 }
19723
Bram Moolenaar52a72462014-08-29 15:53:52 +020019724 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19725 * echoes typeahead, that messes up the display. */
19726 if (!msg_silent)
19727 flags += SHELL_COOKED;
19728
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019729 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019730 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019731 int len;
19732 listitem_T *li;
19733 char_u *s = NULL;
19734 char_u *start;
19735 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019736 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019737
Bram Moolenaar52a72462014-08-29 15:53:52 +020019738 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019739 if (res == NULL)
19740 goto errret;
19741
19742 list = list_alloc();
19743 if (list == NULL)
19744 goto errret;
19745
19746 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019748 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019749 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019750 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019751 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019752
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019753 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019754 if (s == NULL)
19755 goto errret;
19756
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019757 for (p = s; start < end; ++p, ++start)
19758 *p = *start == NUL ? NL : *start;
19759 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019760
19761 li = listitem_alloc();
19762 if (li == NULL)
19763 {
19764 vim_free(s);
19765 goto errret;
19766 }
19767 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019768 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019769 li->li_tv.vval.v_string = s;
19770 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019772
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019773 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019774 rettv->v_type = VAR_LIST;
19775 rettv->vval.v_list = list;
19776 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019778 else
19779 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019780 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019781#ifdef USE_CR
19782 /* translate <CR> into <NL> */
19783 if (res != NULL)
19784 {
19785 char_u *s;
19786
19787 for (s = res; *s; ++s)
19788 {
19789 if (*s == CAR)
19790 *s = NL;
19791 }
19792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793#else
19794# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019795 /* translate <CR><NL> into <NL> */
19796 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019798 char_u *s, *d;
19799
19800 d = res;
19801 for (s = res; *s; ++s)
19802 {
19803 if (s[0] == CAR && s[1] == NL)
19804 ++s;
19805 *d++ = *s;
19806 }
19807 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809# endif
19810#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019811 rettv->vval.v_string = res;
19812 res = NULL;
19813 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019814
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019815errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019816 if (infile != NULL)
19817 {
19818 mch_remove(infile);
19819 vim_free(infile);
19820 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019821 if (res != NULL)
19822 vim_free(res);
19823 if (list != NULL)
19824 list_free(list, TRUE);
19825}
19826
19827/*
19828 * "system()" function
19829 */
19830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019831f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019832{
19833 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19834}
19835
19836/*
19837 * "systemlist()" function
19838 */
19839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019840f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019841{
19842 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843}
19844
19845/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019846 * "tabpagebuflist()" function
19847 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019849f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019850{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019851#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019852 tabpage_T *tp;
19853 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019854
19855 if (argvars[0].v_type == VAR_UNKNOWN)
19856 wp = firstwin;
19857 else
19858 {
19859 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19860 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019861 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019862 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019863 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019864 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019865 for (; wp != NULL; wp = wp->w_next)
19866 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019867 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019868 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019869 }
19870#endif
19871}
19872
19873
19874/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019875 * "tabpagenr()" function
19876 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019878f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019879{
19880 int nr = 1;
19881#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019882 char_u *arg;
19883
19884 if (argvars[0].v_type != VAR_UNKNOWN)
19885 {
19886 arg = get_tv_string_chk(&argvars[0]);
19887 nr = 0;
19888 if (arg != NULL)
19889 {
19890 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019891 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019892 else
19893 EMSG2(_(e_invexpr2), arg);
19894 }
19895 }
19896 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019897 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019898#endif
19899 rettv->vval.v_number = nr;
19900}
19901
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019902
19903#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019904static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019905
19906/*
19907 * Common code for tabpagewinnr() and winnr().
19908 */
19909 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019910get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019911{
19912 win_T *twin;
19913 int nr = 1;
19914 win_T *wp;
19915 char_u *arg;
19916
19917 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19918 if (argvar->v_type != VAR_UNKNOWN)
19919 {
19920 arg = get_tv_string_chk(argvar);
19921 if (arg == NULL)
19922 nr = 0; /* type error; errmsg already given */
19923 else if (STRCMP(arg, "$") == 0)
19924 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19925 else if (STRCMP(arg, "#") == 0)
19926 {
19927 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19928 if (twin == NULL)
19929 nr = 0;
19930 }
19931 else
19932 {
19933 EMSG2(_(e_invexpr2), arg);
19934 nr = 0;
19935 }
19936 }
19937
19938 if (nr > 0)
19939 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19940 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019941 {
19942 if (wp == NULL)
19943 {
19944 /* didn't find it in this tabpage */
19945 nr = 0;
19946 break;
19947 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019948 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019949 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019950 return nr;
19951}
19952#endif
19953
19954/*
19955 * "tabpagewinnr()" function
19956 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019958f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019959{
19960 int nr = 1;
19961#ifdef FEAT_WINDOWS
19962 tabpage_T *tp;
19963
19964 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19965 if (tp == NULL)
19966 nr = 0;
19967 else
19968 nr = get_winnr(tp, &argvars[1]);
19969#endif
19970 rettv->vval.v_number = nr;
19971}
19972
19973
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019974/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019975 * "tagfiles()" function
19976 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019978f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019979{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019980 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019981 tagname_T tn;
19982 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019983
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019984 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019985 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019986 fname = alloc(MAXPATHL);
19987 if (fname == NULL)
19988 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019989
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019990 for (first = TRUE; ; first = FALSE)
19991 if (get_tagfname(&tn, first, fname) == FAIL
19992 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019993 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019994 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019995 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019996}
19997
19998/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019999 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020000 */
20001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020002f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020003{
20004 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020005
20006 tag_pattern = get_tv_string(&argvars[0]);
20007
20008 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020009 if (*tag_pattern == NUL)
20010 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020011
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020012 if (rettv_list_alloc(rettv) == OK)
20013 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020014}
20015
20016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020017 * "tempname()" function
20018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020020f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020021{
20022 static int x = 'A';
20023
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020024 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020025 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020026
20027 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20028 * names. Skip 'I' and 'O', they are used for shell redirection. */
20029 do
20030 {
20031 if (x == 'Z')
20032 x = '0';
20033 else if (x == '9')
20034 x = 'A';
20035 else
20036 {
20037#ifdef EBCDIC
20038 if (x == 'I')
20039 x = 'J';
20040 else if (x == 'R')
20041 x = 'S';
20042 else
20043#endif
20044 ++x;
20045 }
20046 } while (x == 'I' || x == 'O');
20047}
20048
20049/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020050 * "test(list)" function: Just checking the walls...
20051 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020053f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020054{
20055 /* Used for unit testing. Change the code below to your liking. */
20056#if 0
20057 listitem_T *li;
20058 list_T *l;
20059 char_u *bad, *good;
20060
20061 if (argvars[0].v_type != VAR_LIST)
20062 return;
20063 l = argvars[0].vval.v_list;
20064 if (l == NULL)
20065 return;
20066 li = l->lv_first;
20067 if (li == NULL)
20068 return;
20069 bad = get_tv_string(&li->li_tv);
20070 li = li->li_next;
20071 if (li == NULL)
20072 return;
20073 good = get_tv_string(&li->li_tv);
20074 rettv->vval.v_number = test_edit_score(bad, good);
20075#endif
20076}
20077
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020078#ifdef FEAT_FLOAT
20079/*
20080 * "tan()" function
20081 */
20082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020083f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020084{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020085 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020086
20087 rettv->v_type = VAR_FLOAT;
20088 if (get_float_arg(argvars, &f) == OK)
20089 rettv->vval.v_float = tan(f);
20090 else
20091 rettv->vval.v_float = 0.0;
20092}
20093
20094/*
20095 * "tanh()" function
20096 */
20097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020098f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020099{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020100 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020101
20102 rettv->v_type = VAR_FLOAT;
20103 if (get_float_arg(argvars, &f) == OK)
20104 rettv->vval.v_float = tanh(f);
20105 else
20106 rettv->vval.v_float = 0.0;
20107}
20108#endif
20109
Bram Moolenaar975b5272016-03-15 23:10:59 +010020110#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20111/*
20112 * Get a callback from "arg". It can be a Funcref or a function name.
20113 * When "arg" is zero return an empty string.
20114 * Return NULL for an invalid argument.
20115 */
20116 char_u *
20117get_callback(typval_T *arg, partial_T **pp)
20118{
20119 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20120 {
20121 *pp = arg->vval.v_partial;
20122 return (*pp)->pt_name;
20123 }
20124 *pp = NULL;
20125 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20126 return arg->vval.v_string;
20127 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20128 return (char_u *)"";
20129 EMSG(_("E921: Invalid callback argument"));
20130 return NULL;
20131}
20132#endif
20133
20134#ifdef FEAT_TIMERS
20135/*
20136 * "timer_start(time, callback [, options])" function
20137 */
20138 static void
20139f_timer_start(typval_T *argvars, typval_T *rettv)
20140{
20141 long msec = get_tv_number(&argvars[0]);
20142 timer_T *timer;
20143 int repeat = 0;
20144 char_u *callback;
20145 dict_T *dict;
20146
20147 if (argvars[2].v_type != VAR_UNKNOWN)
20148 {
20149 if (argvars[2].v_type != VAR_DICT
20150 || (dict = argvars[2].vval.v_dict) == NULL)
20151 {
20152 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20153 return;
20154 }
20155 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20156 repeat = get_dict_number(dict, (char_u *)"repeat");
20157 }
20158
20159 timer = create_timer(msec, repeat);
20160 callback = get_callback(&argvars[1], &timer->tr_partial);
20161 if (callback == NULL)
20162 {
20163 stop_timer(timer);
20164 rettv->vval.v_number = -1;
20165 }
20166 else
20167 {
20168 timer->tr_callback = vim_strsave(callback);
20169 rettv->vval.v_number = timer->tr_id;
20170 }
20171}
20172
20173/*
20174 * "timer_stop(timer)" function
20175 */
20176 static void
20177f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20178{
20179 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20180
20181 if (timer != NULL)
20182 stop_timer(timer);
20183}
20184#endif
20185
Bram Moolenaard52d9742005-08-21 22:20:28 +000020186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187 * "tolower(string)" function
20188 */
20189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020190f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191{
20192 char_u *p;
20193
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020194 p = vim_strsave(get_tv_string(&argvars[0]));
20195 rettv->v_type = VAR_STRING;
20196 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020197
20198 if (p != NULL)
20199 while (*p != NUL)
20200 {
20201#ifdef FEAT_MBYTE
20202 int l;
20203
20204 if (enc_utf8)
20205 {
20206 int c, lc;
20207
20208 c = utf_ptr2char(p);
20209 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020210 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211 /* TODO: reallocate string when byte count changes. */
20212 if (utf_char2len(lc) == l)
20213 utf_char2bytes(lc, p);
20214 p += l;
20215 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020216 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217 p += l; /* skip multi-byte character */
20218 else
20219#endif
20220 {
20221 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20222 ++p;
20223 }
20224 }
20225}
20226
20227/*
20228 * "toupper(string)" function
20229 */
20230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020231f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020232{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020233 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020234 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020235}
20236
20237/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020238 * "tr(string, fromstr, tostr)" function
20239 */
20240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020241f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020242{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020243 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020244 char_u *fromstr;
20245 char_u *tostr;
20246 char_u *p;
20247#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020248 int inlen;
20249 int fromlen;
20250 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020251 int idx;
20252 char_u *cpstr;
20253 int cplen;
20254 int first = TRUE;
20255#endif
20256 char_u buf[NUMBUFLEN];
20257 char_u buf2[NUMBUFLEN];
20258 garray_T ga;
20259
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020260 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020261 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20262 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020263
20264 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020265 rettv->v_type = VAR_STRING;
20266 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020267 if (fromstr == NULL || tostr == NULL)
20268 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020269 ga_init2(&ga, (int)sizeof(char), 80);
20270
20271#ifdef FEAT_MBYTE
20272 if (!has_mbyte)
20273#endif
20274 /* not multi-byte: fromstr and tostr must be the same length */
20275 if (STRLEN(fromstr) != STRLEN(tostr))
20276 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020277#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020278error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020279#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020280 EMSG2(_(e_invarg2), fromstr);
20281 ga_clear(&ga);
20282 return;
20283 }
20284
20285 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020286 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020287 {
20288#ifdef FEAT_MBYTE
20289 if (has_mbyte)
20290 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020291 inlen = (*mb_ptr2len)(in_str);
20292 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020293 cplen = inlen;
20294 idx = 0;
20295 for (p = fromstr; *p != NUL; p += fromlen)
20296 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020297 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020298 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020299 {
20300 for (p = tostr; *p != NUL; p += tolen)
20301 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020302 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020303 if (idx-- == 0)
20304 {
20305 cplen = tolen;
20306 cpstr = p;
20307 break;
20308 }
20309 }
20310 if (*p == NUL) /* tostr is shorter than fromstr */
20311 goto error;
20312 break;
20313 }
20314 ++idx;
20315 }
20316
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020317 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020318 {
20319 /* Check that fromstr and tostr have the same number of
20320 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020321 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020322 first = FALSE;
20323 for (p = tostr; *p != NUL; p += tolen)
20324 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020325 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020326 --idx;
20327 }
20328 if (idx != 0)
20329 goto error;
20330 }
20331
Bram Moolenaarcde88542015-08-11 19:14:00 +020020332 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020333 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020334 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020335
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020336 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020337 }
20338 else
20339#endif
20340 {
20341 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020342 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020343 if (p != NULL)
20344 ga_append(&ga, tostr[p - fromstr]);
20345 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020346 ga_append(&ga, *in_str);
20347 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020348 }
20349 }
20350
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020351 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020352 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020353 ga_append(&ga, NUL);
20354
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020355 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020356}
20357
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020358#ifdef FEAT_FLOAT
20359/*
20360 * "trunc({float})" function
20361 */
20362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020363f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020364{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020365 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020366
20367 rettv->v_type = VAR_FLOAT;
20368 if (get_float_arg(argvars, &f) == OK)
20369 /* trunc() is not in C90, use floor() or ceil() instead. */
20370 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20371 else
20372 rettv->vval.v_float = 0.0;
20373}
20374#endif
20375
Bram Moolenaar8299df92004-07-10 09:47:34 +000020376/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377 * "type(expr)" function
20378 */
20379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020380f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020381{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020382 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020383
20384 switch (argvars[0].v_type)
20385 {
20386 case VAR_NUMBER: n = 0; break;
20387 case VAR_STRING: n = 1; break;
20388 case VAR_FUNC: n = 2; break;
20389 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020390 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020391 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020392 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020393 if (argvars[0].vval.v_number == VVAL_FALSE
20394 || argvars[0].vval.v_number == VVAL_TRUE)
20395 n = 6;
20396 else
20397 n = 7;
20398 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020399 case VAR_JOB: n = 8; break;
20400 case VAR_CHANNEL: n = 9; break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010020401 case VAR_PARTIAL: n = 10; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020402 case VAR_UNKNOWN:
20403 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20404 n = -1;
20405 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020406 }
20407 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408}
20409
20410/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020411 * "undofile(name)" function
20412 */
20413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020414f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020415{
20416 rettv->v_type = VAR_STRING;
20417#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020418 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020419 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020420
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020421 if (*fname == NUL)
20422 {
20423 /* If there is no file name there will be no undo file. */
20424 rettv->vval.v_string = NULL;
20425 }
20426 else
20427 {
20428 char_u *ffname = FullName_save(fname, FALSE);
20429
20430 if (ffname != NULL)
20431 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20432 vim_free(ffname);
20433 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020434 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020435#else
20436 rettv->vval.v_string = NULL;
20437#endif
20438}
20439
20440/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020441 * "undotree()" function
20442 */
20443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020444f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020445{
20446 if (rettv_dict_alloc(rettv) == OK)
20447 {
20448 dict_T *dict = rettv->vval.v_dict;
20449 list_T *list;
20450
Bram Moolenaar730cde92010-06-27 05:18:54 +020020451 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020452 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020453 dict_add_nr_str(dict, "save_last",
20454 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020455 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20456 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020457 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020458
20459 list = list_alloc();
20460 if (list != NULL)
20461 {
20462 u_eval_tree(curbuf->b_u_oldhead, list);
20463 dict_add_list(dict, "entries", list);
20464 }
20465 }
20466}
20467
20468/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020469 * "values(dict)" function
20470 */
20471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020472f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020473{
20474 dict_list(argvars, rettv, 1);
20475}
20476
20477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020478 * "virtcol(string)" function
20479 */
20480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020481f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020482{
20483 colnr_T vcol = 0;
20484 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020485 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020486
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020487 fp = var2fpos(&argvars[0], FALSE, &fnum);
20488 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20489 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020490 {
20491 getvvcol(curwin, fp, NULL, NULL, &vcol);
20492 ++vcol;
20493 }
20494
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020495 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020496}
20497
20498/*
20499 * "visualmode()" function
20500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020501 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010020502f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020503{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020504 char_u str[2];
20505
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020506 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020507 str[0] = curbuf->b_visual_mode_eval;
20508 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020509 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020510
20511 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020512 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020513 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514}
20515
20516/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020517 * "wildmenumode()" function
20518 */
20519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020520f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020521{
20522#ifdef FEAT_WILDMENU
20523 if (wild_menu_showing)
20524 rettv->vval.v_number = 1;
20525#endif
20526}
20527
20528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020529 * "winbufnr(nr)" function
20530 */
20531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020532f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020533{
20534 win_T *wp;
20535
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020536 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020538 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020540 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020541}
20542
20543/*
20544 * "wincol()" function
20545 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020547f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020548{
20549 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020550 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551}
20552
20553/*
20554 * "winheight(nr)" function
20555 */
20556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020557f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020558{
20559 win_T *wp;
20560
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020561 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020562 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020563 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020565 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020566}
20567
20568/*
20569 * "winline()" function
20570 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020572f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020573{
20574 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020575 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020576}
20577
20578/*
20579 * "winnr()" function
20580 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020582f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020583{
20584 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020585
Bram Moolenaar071d4272004-06-13 20:20:40 +000020586#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020587 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020588#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020589 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590}
20591
20592/*
20593 * "winrestcmd()" function
20594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020596f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020597{
20598#ifdef FEAT_WINDOWS
20599 win_T *wp;
20600 int winnr = 1;
20601 garray_T ga;
20602 char_u buf[50];
20603
20604 ga_init2(&ga, (int)sizeof(char), 70);
20605 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20606 {
20607 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20608 ga_concat(&ga, buf);
20609# ifdef FEAT_VERTSPLIT
20610 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20611 ga_concat(&ga, buf);
20612# endif
20613 ++winnr;
20614 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020615 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020616
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020617 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020618#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020619 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020620#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020621 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020622}
20623
20624/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020625 * "winrestview()" function
20626 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020628f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020629{
20630 dict_T *dict;
20631
20632 if (argvars[0].v_type != VAR_DICT
20633 || (dict = argvars[0].vval.v_dict) == NULL)
20634 EMSG(_(e_invarg));
20635 else
20636 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020637 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20638 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20639 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20640 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020641#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020642 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20643 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020644#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020645 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20646 {
20647 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20648 curwin->w_set_curswant = FALSE;
20649 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020650
Bram Moolenaar82c25852014-05-28 16:47:16 +020020651 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20652 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020653#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020654 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20655 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020656#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020657 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20658 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20659 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20660 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020661
20662 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020663 win_new_height(curwin, curwin->w_height);
20664# ifdef FEAT_VERTSPLIT
20665 win_new_width(curwin, W_WIDTH(curwin));
20666# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020667 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020668
Bram Moolenaarb851a962014-10-31 15:45:52 +010020669 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020670 curwin->w_topline = 1;
20671 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20672 curwin->w_topline = curbuf->b_ml.ml_line_count;
20673#ifdef FEAT_DIFF
20674 check_topfill(curwin, TRUE);
20675#endif
20676 }
20677}
20678
20679/*
20680 * "winsaveview()" function
20681 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020683f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020684{
20685 dict_T *dict;
20686
Bram Moolenaara800b422010-06-27 01:15:55 +020020687 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020688 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020689 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020690
20691 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20692 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20693#ifdef FEAT_VIRTUALEDIT
20694 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20695#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020696 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020697 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20698
20699 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20700#ifdef FEAT_DIFF
20701 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20702#endif
20703 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20704 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20705}
20706
20707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708 * "winwidth(nr)" function
20709 */
20710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020711f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020712{
20713 win_T *wp;
20714
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020715 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020716 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020717 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020718 else
20719#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020720 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020721#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020722 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723#endif
20724}
20725
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020727 * "wordcount()" function
20728 */
20729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020730f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020731{
20732 if (rettv_dict_alloc(rettv) == FAIL)
20733 return;
20734 cursor_pos_info(rettv->vval.v_dict);
20735}
20736
20737/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020738 * Write list of strings to file
20739 */
20740 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020741write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020742{
20743 listitem_T *li;
20744 int c;
20745 int ret = OK;
20746 char_u *s;
20747
20748 for (li = list->lv_first; li != NULL; li = li->li_next)
20749 {
20750 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20751 {
20752 if (*s == '\n')
20753 c = putc(NUL, fd);
20754 else
20755 c = putc(*s, fd);
20756 if (c == EOF)
20757 {
20758 ret = FAIL;
20759 break;
20760 }
20761 }
20762 if (!binary || li->li_next != NULL)
20763 if (putc('\n', fd) == EOF)
20764 {
20765 ret = FAIL;
20766 break;
20767 }
20768 if (ret == FAIL)
20769 {
20770 EMSG(_(e_write));
20771 break;
20772 }
20773 }
20774 return ret;
20775}
20776
20777/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020778 * "writefile()" function
20779 */
20780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020781f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020782{
20783 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020784 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020785 char_u *fname;
20786 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020787 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020788
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020789 if (check_restricted() || check_secure())
20790 return;
20791
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020792 if (argvars[0].v_type != VAR_LIST)
20793 {
20794 EMSG2(_(e_listarg), "writefile()");
20795 return;
20796 }
20797 if (argvars[0].vval.v_list == NULL)
20798 return;
20799
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020800 if (argvars[2].v_type != VAR_UNKNOWN)
20801 {
20802 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20803 binary = TRUE;
20804 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20805 append = TRUE;
20806 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020807
20808 /* Always open the file in binary mode, library functions have a mind of
20809 * their own about CR-LF conversion. */
20810 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020811 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20812 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020813 {
20814 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20815 ret = -1;
20816 }
20817 else
20818 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020819 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20820 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020821 fclose(fd);
20822 }
20823
20824 rettv->vval.v_number = ret;
20825}
20826
20827/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020828 * "xor(expr, expr)" function
20829 */
20830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020831f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020832{
20833 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20834 ^ get_tv_number_chk(&argvars[1], NULL);
20835}
20836
20837
20838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020839 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020840 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020841 */
20842 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020843var2fpos(
20844 typval_T *varp,
20845 int dollar_lnum, /* TRUE when $ is last line */
20846 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020847{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020848 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020849 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020850 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020851
Bram Moolenaara5525202006-03-02 22:52:09 +000020852 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020853 if (varp->v_type == VAR_LIST)
20854 {
20855 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020856 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020857 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020858 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020859
20860 l = varp->vval.v_list;
20861 if (l == NULL)
20862 return NULL;
20863
20864 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020865 pos.lnum = list_find_nr(l, 0L, &error);
20866 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020867 return NULL; /* invalid line number */
20868
20869 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020870 pos.col = list_find_nr(l, 1L, &error);
20871 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020872 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020873 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020874
20875 /* We accept "$" for the column number: last column. */
20876 li = list_find(l, 1L);
20877 if (li != NULL && li->li_tv.v_type == VAR_STRING
20878 && li->li_tv.vval.v_string != NULL
20879 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20880 pos.col = len + 1;
20881
Bram Moolenaara5525202006-03-02 22:52:09 +000020882 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020883 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020884 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020885 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020886
Bram Moolenaara5525202006-03-02 22:52:09 +000020887#ifdef FEAT_VIRTUALEDIT
20888 /* Get the virtual offset. Defaults to zero. */
20889 pos.coladd = list_find_nr(l, 2L, &error);
20890 if (error)
20891 pos.coladd = 0;
20892#endif
20893
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020894 return &pos;
20895 }
20896
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020897 name = get_tv_string_chk(varp);
20898 if (name == NULL)
20899 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020900 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020901 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020902 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20903 {
20904 if (VIsual_active)
20905 return &VIsual;
20906 return &curwin->w_cursor;
20907 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020908 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020910 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020911 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20912 return NULL;
20913 return pp;
20914 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020915
20916#ifdef FEAT_VIRTUALEDIT
20917 pos.coladd = 0;
20918#endif
20919
Bram Moolenaar477933c2007-07-17 14:32:23 +000020920 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020921 {
20922 pos.col = 0;
20923 if (name[1] == '0') /* "w0": first visible line */
20924 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020925 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020926 pos.lnum = curwin->w_topline;
20927 return &pos;
20928 }
20929 else if (name[1] == '$') /* "w$": last visible line */
20930 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020931 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020932 pos.lnum = curwin->w_botline - 1;
20933 return &pos;
20934 }
20935 }
20936 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020937 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020938 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020939 {
20940 pos.lnum = curbuf->b_ml.ml_line_count;
20941 pos.col = 0;
20942 }
20943 else
20944 {
20945 pos.lnum = curwin->w_cursor.lnum;
20946 pos.col = (colnr_T)STRLEN(ml_get_curline());
20947 }
20948 return &pos;
20949 }
20950 return NULL;
20951}
20952
20953/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020954 * Convert list in "arg" into a position and optional file number.
20955 * When "fnump" is NULL there is no file number, only 3 items.
20956 * Note that the column is passed on as-is, the caller may want to decrement
20957 * it to use 1 for the first column.
20958 * Return FAIL when conversion is not possible, doesn't check the position for
20959 * validity.
20960 */
20961 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020962list2fpos(
20963 typval_T *arg,
20964 pos_T *posp,
20965 int *fnump,
20966 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020967{
20968 list_T *l = arg->vval.v_list;
20969 long i = 0;
20970 long n;
20971
Bram Moolenaar493c1782014-05-28 14:34:46 +020020972 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20973 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020974 if (arg->v_type != VAR_LIST
20975 || l == NULL
20976 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020977 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020978 return FAIL;
20979
20980 if (fnump != NULL)
20981 {
20982 n = list_find_nr(l, i++, NULL); /* fnum */
20983 if (n < 0)
20984 return FAIL;
20985 if (n == 0)
20986 n = curbuf->b_fnum; /* current buffer */
20987 *fnump = n;
20988 }
20989
20990 n = list_find_nr(l, i++, NULL); /* lnum */
20991 if (n < 0)
20992 return FAIL;
20993 posp->lnum = n;
20994
20995 n = list_find_nr(l, i++, NULL); /* col */
20996 if (n < 0)
20997 return FAIL;
20998 posp->col = n;
20999
21000#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021001 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021002 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021003 posp->coladd = 0;
21004 else
21005 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021006#endif
21007
Bram Moolenaar493c1782014-05-28 14:34:46 +020021008 if (curswantp != NULL)
21009 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21010
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021011 return OK;
21012}
21013
21014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015 * Get the length of an environment variable name.
21016 * Advance "arg" to the first character after the name.
21017 * Return 0 for error.
21018 */
21019 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021020get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021021{
21022 char_u *p;
21023 int len;
21024
21025 for (p = *arg; vim_isIDc(*p); ++p)
21026 ;
21027 if (p == *arg) /* no name found */
21028 return 0;
21029
21030 len = (int)(p - *arg);
21031 *arg = p;
21032 return len;
21033}
21034
21035/*
21036 * Get the length of the name of a function or internal variable.
21037 * "arg" is advanced to the first non-white character after the name.
21038 * Return 0 if something is wrong.
21039 */
21040 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021041get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021042{
21043 char_u *p;
21044 int len;
21045
21046 /* Find the end of the name. */
21047 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021048 {
21049 if (*p == ':')
21050 {
21051 /* "s:" is start of "s:var", but "n:" is not and can be used in
21052 * slice "[n:]". Also "xx:" is not a namespace. */
21053 len = (int)(p - *arg);
21054 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21055 || len > 1)
21056 break;
21057 }
21058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021059 if (p == *arg) /* no name found */
21060 return 0;
21061
21062 len = (int)(p - *arg);
21063 *arg = skipwhite(p);
21064
21065 return len;
21066}
21067
21068/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021069 * Get the length of the name of a variable or function.
21070 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021071 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021072 * Return -1 if curly braces expansion failed.
21073 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021074 * If the name contains 'magic' {}'s, expand them and return the
21075 * expanded name in an allocated string via 'alias' - caller must free.
21076 */
21077 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021078get_name_len(
21079 char_u **arg,
21080 char_u **alias,
21081 int evaluate,
21082 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021083{
21084 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021085 char_u *p;
21086 char_u *expr_start;
21087 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088
21089 *alias = NULL; /* default to no alias */
21090
21091 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21092 && (*arg)[2] == (int)KE_SNR)
21093 {
21094 /* hard coded <SNR>, already translated */
21095 *arg += 3;
21096 return get_id_len(arg) + 3;
21097 }
21098 len = eval_fname_script(*arg);
21099 if (len > 0)
21100 {
21101 /* literal "<SID>", "s:" or "<SNR>" */
21102 *arg += len;
21103 }
21104
Bram Moolenaar071d4272004-06-13 20:20:40 +000021105 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021106 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021108 p = find_name_end(*arg, &expr_start, &expr_end,
21109 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110 if (expr_start != NULL)
21111 {
21112 char_u *temp_string;
21113
21114 if (!evaluate)
21115 {
21116 len += (int)(p - *arg);
21117 *arg = skipwhite(p);
21118 return len;
21119 }
21120
21121 /*
21122 * Include any <SID> etc in the expanded string:
21123 * Thus the -len here.
21124 */
21125 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21126 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021127 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021128 *alias = temp_string;
21129 *arg = skipwhite(p);
21130 return (int)STRLEN(temp_string);
21131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021132
21133 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021134 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021135 EMSG2(_(e_invexpr2), *arg);
21136
21137 return len;
21138}
21139
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021140/*
21141 * Find the end of a variable or function name, taking care of magic braces.
21142 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21143 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021144 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021145 * Return a pointer to just after the name. Equal to "arg" if there is no
21146 * valid name.
21147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021148 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021149find_name_end(
21150 char_u *arg,
21151 char_u **expr_start,
21152 char_u **expr_end,
21153 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021154{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021155 int mb_nest = 0;
21156 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021157 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021158 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021160 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021162 *expr_start = NULL;
21163 *expr_end = NULL;
21164 }
21165
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021166 /* Quick check for valid starting character. */
21167 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21168 return arg;
21169
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021170 for (p = arg; *p != NUL
21171 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021172 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021173 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021174 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021175 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021176 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021177 if (*p == '\'')
21178 {
21179 /* skip over 'string' to avoid counting [ and ] inside it. */
21180 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21181 ;
21182 if (*p == NUL)
21183 break;
21184 }
21185 else if (*p == '"')
21186 {
21187 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21188 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21189 if (*p == '\\' && p[1] != NUL)
21190 ++p;
21191 if (*p == NUL)
21192 break;
21193 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021194 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21195 {
21196 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021197 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021198 len = (int)(p - arg);
21199 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021200 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021201 break;
21202 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021203
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021204 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021206 if (*p == '[')
21207 ++br_nest;
21208 else if (*p == ']')
21209 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021210 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021211
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021212 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021213 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021214 if (*p == '{')
21215 {
21216 mb_nest++;
21217 if (expr_start != NULL && *expr_start == NULL)
21218 *expr_start = p;
21219 }
21220 else if (*p == '}')
21221 {
21222 mb_nest--;
21223 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21224 *expr_end = p;
21225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021227 }
21228
21229 return p;
21230}
21231
21232/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021233 * Expands out the 'magic' {}'s in a variable/function name.
21234 * Note that this can call itself recursively, to deal with
21235 * constructs like foo{bar}{baz}{bam}
21236 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21237 * "in_start" ^
21238 * "expr_start" ^
21239 * "expr_end" ^
21240 * "in_end" ^
21241 *
21242 * Returns a new allocated string, which the caller must free.
21243 * Returns NULL for failure.
21244 */
21245 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021246make_expanded_name(
21247 char_u *in_start,
21248 char_u *expr_start,
21249 char_u *expr_end,
21250 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021251{
21252 char_u c1;
21253 char_u *retval = NULL;
21254 char_u *temp_result;
21255 char_u *nextcmd = NULL;
21256
21257 if (expr_end == NULL || in_end == NULL)
21258 return NULL;
21259 *expr_start = NUL;
21260 *expr_end = NUL;
21261 c1 = *in_end;
21262 *in_end = NUL;
21263
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021264 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021265 if (temp_result != NULL && nextcmd == NULL)
21266 {
21267 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21268 + (in_end - expr_end) + 1));
21269 if (retval != NULL)
21270 {
21271 STRCPY(retval, in_start);
21272 STRCAT(retval, temp_result);
21273 STRCAT(retval, expr_end + 1);
21274 }
21275 }
21276 vim_free(temp_result);
21277
21278 *in_end = c1; /* put char back for error messages */
21279 *expr_start = '{';
21280 *expr_end = '}';
21281
21282 if (retval != NULL)
21283 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021284 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021285 if (expr_start != NULL)
21286 {
21287 /* Further expansion! */
21288 temp_result = make_expanded_name(retval, expr_start,
21289 expr_end, temp_result);
21290 vim_free(retval);
21291 retval = temp_result;
21292 }
21293 }
21294
21295 return retval;
21296}
21297
21298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021299 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021300 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021301 */
21302 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021303eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021305 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21306}
21307
21308/*
21309 * Return TRUE if character "c" can be used as the first character in a
21310 * variable or function name (excluding '{' and '}').
21311 */
21312 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021313eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021314{
21315 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021316}
21317
21318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319 * Set number v: variable to "val".
21320 */
21321 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021322set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021323{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021324 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021325}
21326
21327/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021328 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021329 */
21330 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021331get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021332{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021333 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021334}
21335
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021336/*
21337 * Get string v: variable value. Uses a static buffer, can only be used once.
21338 */
21339 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021340get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021341{
21342 return get_tv_string(&vimvars[idx].vv_tv);
21343}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021344
Bram Moolenaar071d4272004-06-13 20:20:40 +000021345/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021346 * Get List v: variable value. Caller must take care of reference count when
21347 * needed.
21348 */
21349 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021350get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021351{
21352 return vimvars[idx].vv_list;
21353}
21354
21355/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021356 * Set v:char to character "c".
21357 */
21358 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021359set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021360{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021361 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021362
21363#ifdef FEAT_MBYTE
21364 if (has_mbyte)
21365 buf[(*mb_char2bytes)(c, buf)] = NUL;
21366 else
21367#endif
21368 {
21369 buf[0] = c;
21370 buf[1] = NUL;
21371 }
21372 set_vim_var_string(VV_CHAR, buf, -1);
21373}
21374
21375/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021376 * Set v:count to "count" and v:count1 to "count1".
21377 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021378 */
21379 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021380set_vcount(
21381 long count,
21382 long count1,
21383 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021384{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021385 if (set_prevcount)
21386 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021387 vimvars[VV_COUNT].vv_nr = count;
21388 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021389}
21390
21391/*
21392 * Set string v: variable to a copy of "val".
21393 */
21394 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021395set_vim_var_string(
21396 int idx,
21397 char_u *val,
21398 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021399{
Bram Moolenaara542c682016-01-31 16:28:04 +010021400 clear_tv(&vimvars[idx].vv_di.di_tv);
21401 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021402 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021403 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021404 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021405 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021406 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021407 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021408}
21409
21410/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021411 * Set List v: variable to "val".
21412 */
21413 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021414set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021415{
Bram Moolenaara542c682016-01-31 16:28:04 +010021416 clear_tv(&vimvars[idx].vv_di.di_tv);
21417 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021418 vimvars[idx].vv_list = val;
21419 if (val != NULL)
21420 ++val->lv_refcount;
21421}
21422
21423/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021424 * Set Dictionary v: variable to "val".
21425 */
21426 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021427set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021428{
21429 int todo;
21430 hashitem_T *hi;
21431
Bram Moolenaara542c682016-01-31 16:28:04 +010021432 clear_tv(&vimvars[idx].vv_di.di_tv);
21433 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021434 vimvars[idx].vv_dict = val;
21435 if (val != NULL)
21436 {
21437 ++val->dv_refcount;
21438
21439 /* Set readonly */
21440 todo = (int)val->dv_hashtab.ht_used;
21441 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21442 {
21443 if (HASHITEM_EMPTY(hi))
21444 continue;
21445 --todo;
21446 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21447 }
21448 }
21449}
21450
21451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021452 * Set v:register if needed.
21453 */
21454 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021455set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021456{
21457 char_u regname;
21458
21459 if (c == 0 || c == ' ')
21460 regname = '"';
21461 else
21462 regname = c;
21463 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021464 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021465 set_vim_var_string(VV_REG, &regname, 1);
21466}
21467
21468/*
21469 * Get or set v:exception. If "oldval" == NULL, return the current value.
21470 * Otherwise, restore the value to "oldval" and return NULL.
21471 * Must always be called in pairs to save and restore v:exception! Does not
21472 * take care of memory allocations.
21473 */
21474 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021475v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476{
21477 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021478 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021479
Bram Moolenaare9a41262005-01-15 22:18:47 +000021480 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481 return NULL;
21482}
21483
21484/*
21485 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21486 * Otherwise, restore the value to "oldval" and return NULL.
21487 * Must always be called in pairs to save and restore v:throwpoint! Does not
21488 * take care of memory allocations.
21489 */
21490 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021491v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492{
21493 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021494 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495
Bram Moolenaare9a41262005-01-15 22:18:47 +000021496 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021497 return NULL;
21498}
21499
21500#if defined(FEAT_AUTOCMD) || defined(PROTO)
21501/*
21502 * Set v:cmdarg.
21503 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21504 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21505 * Must always be called in pairs!
21506 */
21507 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021508set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509{
21510 char_u *oldval;
21511 char_u *newval;
21512 unsigned len;
21513
Bram Moolenaare9a41262005-01-15 22:18:47 +000021514 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021515 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021517 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021518 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021519 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021520 }
21521
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021522 if (eap->force_bin == FORCE_BIN)
21523 len = 6;
21524 else if (eap->force_bin == FORCE_NOBIN)
21525 len = 8;
21526 else
21527 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021528
21529 if (eap->read_edit)
21530 len += 7;
21531
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021532 if (eap->force_ff != 0)
21533 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21534# ifdef FEAT_MBYTE
21535 if (eap->force_enc != 0)
21536 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021537 if (eap->bad_char != 0)
21538 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021539# endif
21540
21541 newval = alloc(len + 1);
21542 if (newval == NULL)
21543 return NULL;
21544
21545 if (eap->force_bin == FORCE_BIN)
21546 sprintf((char *)newval, " ++bin");
21547 else if (eap->force_bin == FORCE_NOBIN)
21548 sprintf((char *)newval, " ++nobin");
21549 else
21550 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021551
21552 if (eap->read_edit)
21553 STRCAT(newval, " ++edit");
21554
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021555 if (eap->force_ff != 0)
21556 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21557 eap->cmd + eap->force_ff);
21558# ifdef FEAT_MBYTE
21559 if (eap->force_enc != 0)
21560 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21561 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021562 if (eap->bad_char == BAD_KEEP)
21563 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21564 else if (eap->bad_char == BAD_DROP)
21565 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21566 else if (eap->bad_char != 0)
21567 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021568# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021569 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021570 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021571}
21572#endif
21573
21574/*
21575 * Get the value of internal variable "name".
21576 * Return OK or FAIL.
21577 */
21578 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021579get_var_tv(
21580 char_u *name,
21581 int len, /* length of "name" */
21582 typval_T *rettv, /* NULL when only checking existence */
21583 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21584 int verbose, /* may give error message */
21585 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021586{
21587 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021588 typval_T *tv = NULL;
21589 typval_T atv;
21590 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021592
21593 /* truncate the name, so that we can use strcmp() */
21594 cc = name[len];
21595 name[len] = NUL;
21596
21597 /*
21598 * Check for "b:changedtick".
21599 */
21600 if (STRCMP(name, "b:changedtick") == 0)
21601 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021602 atv.v_type = VAR_NUMBER;
21603 atv.vval.v_number = curbuf->b_changedtick;
21604 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605 }
21606
21607 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608 * Check for user-defined variables.
21609 */
21610 else
21611 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021612 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021614 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021615 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021616 if (dip != NULL)
21617 *dip = v;
21618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021619 }
21620
Bram Moolenaare9a41262005-01-15 22:18:47 +000021621 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021622 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021623 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021624 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021625 ret = FAIL;
21626 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021627 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021628 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629
21630 name[len] = cc;
21631
21632 return ret;
21633}
21634
21635/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021636 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21637 * Also handle function call with Funcref variable: func(expr)
21638 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21639 */
21640 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021641handle_subscript(
21642 char_u **arg,
21643 typval_T *rettv,
21644 int evaluate, /* do more than finding the end */
21645 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021646{
21647 int ret = OK;
21648 dict_T *selfdict = NULL;
21649 char_u *s;
21650 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021651 typval_T functv;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021652 partial_T *pt = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021653
21654 while (ret == OK
21655 && (**arg == '['
21656 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021657 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
21658 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021659 && !vim_iswhite(*(*arg - 1)))
21660 {
21661 if (**arg == '(')
21662 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021663 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021664 if (evaluate)
21665 {
21666 functv = *rettv;
21667 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021668
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021669 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021670 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021671 {
21672 pt = functv.vval.v_partial;
21673 s = pt->pt_name;
21674 }
21675 else
21676 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021677 }
21678 else
21679 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021680 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021681 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021682 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000021683
21684 /* Clear the funcref afterwards, so that deleting it while
21685 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021686 if (evaluate)
21687 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021688
21689 /* Stop the expression evaluation when immediately aborting on
21690 * error, or when an interrupt occurred or an exception was thrown
21691 * but not caught. */
21692 if (aborting())
21693 {
21694 if (ret == OK)
21695 clear_tv(rettv);
21696 ret = FAIL;
21697 }
21698 dict_unref(selfdict);
21699 selfdict = NULL;
21700 }
21701 else /* **arg == '[' || **arg == '.' */
21702 {
21703 dict_unref(selfdict);
21704 if (rettv->v_type == VAR_DICT)
21705 {
21706 selfdict = rettv->vval.v_dict;
21707 if (selfdict != NULL)
21708 ++selfdict->dv_refcount;
21709 }
21710 else
21711 selfdict = NULL;
21712 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21713 {
21714 clear_tv(rettv);
21715 ret = FAIL;
21716 }
21717 }
21718 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021719
21720 if (rettv->v_type == VAR_FUNC && selfdict != NULL)
21721 {
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021722 char_u *fname;
21723 char_u *tofree = NULL;
21724 ufunc_T *fp;
21725 char_u fname_buf[FLEN_FIXED + 1];
21726 int error;
21727
21728 /* Translate "s:func" to the stored function name. */
21729 fname = fname_trans_sid(rettv->vval.v_string, fname_buf,
21730 &tofree, &error);
21731 fp = find_func(fname);
21732 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021733
21734 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010021735 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021736 {
Bram Moolenaar65639032016-03-16 21:40:30 +010021737 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
21738
21739 if (pt != NULL)
21740 {
21741 pt->pt_refcount = 1;
21742 pt->pt_dict = selfdict;
21743 selfdict = NULL;
21744 pt->pt_name = rettv->vval.v_string;
21745 func_ref(pt->pt_name);
21746 rettv->v_type = VAR_PARTIAL;
21747 rettv->vval.v_partial = pt;
21748 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021749 }
21750 }
21751
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021752 dict_unref(selfdict);
21753 return ret;
21754}
21755
21756/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021757 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021758 * value).
21759 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021760 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021761alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021762{
Bram Moolenaar33570922005-01-25 22:26:29 +000021763 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021764}
21765
21766/*
21767 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021768 * The string "s" must have been allocated, it is consumed.
21769 * Return NULL for out of memory, the variable otherwise.
21770 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021771 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021772alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021773{
Bram Moolenaar33570922005-01-25 22:26:29 +000021774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021775
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021776 rettv = alloc_tv();
21777 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021778 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021779 rettv->v_type = VAR_STRING;
21780 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021781 }
21782 else
21783 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021784 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021785}
21786
21787/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021788 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021789 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021790 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021791free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021792{
21793 if (varp != NULL)
21794 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021795 switch (varp->v_type)
21796 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021797 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021798 func_unref(varp->vval.v_string);
21799 /*FALLTHROUGH*/
21800 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021801 vim_free(varp->vval.v_string);
21802 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021803 case VAR_PARTIAL:
21804 partial_unref(varp->vval.v_partial);
21805 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021806 case VAR_LIST:
21807 list_unref(varp->vval.v_list);
21808 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021809 case VAR_DICT:
21810 dict_unref(varp->vval.v_dict);
21811 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021812 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021813#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021814 job_unref(varp->vval.v_job);
21815 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021816#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021817 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021818#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021819 channel_unref(varp->vval.v_channel);
21820 break;
21821#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021822 case VAR_NUMBER:
21823 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021824 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021825 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021826 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021828 vim_free(varp);
21829 }
21830}
21831
21832/*
21833 * Free the memory for a variable value and set the value to NULL or 0.
21834 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021835 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021836clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021837{
21838 if (varp != NULL)
21839 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021840 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021841 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021842 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021843 func_unref(varp->vval.v_string);
21844 /*FALLTHROUGH*/
21845 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021846 vim_free(varp->vval.v_string);
21847 varp->vval.v_string = NULL;
21848 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021849 case VAR_PARTIAL:
21850 partial_unref(varp->vval.v_partial);
21851 varp->vval.v_partial = NULL;
21852 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021853 case VAR_LIST:
21854 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021855 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021856 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021857 case VAR_DICT:
21858 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021859 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021860 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021861 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021862 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021863 varp->vval.v_number = 0;
21864 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021865 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021866#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021867 varp->vval.v_float = 0.0;
21868 break;
21869#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021870 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021871#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021872 job_unref(varp->vval.v_job);
21873 varp->vval.v_job = NULL;
21874#endif
21875 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021876 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021877#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021878 channel_unref(varp->vval.v_channel);
21879 varp->vval.v_channel = NULL;
21880#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021881 case VAR_UNKNOWN:
21882 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021884 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021885 }
21886}
21887
21888/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021889 * Set the value of a variable to NULL without freeing items.
21890 */
21891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021892init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021893{
21894 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021895 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021896}
21897
21898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021899 * Get the number value of a variable.
21900 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021901 * For incompatible types, return 0.
21902 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21903 * caller of incompatible types: it sets *denote to TRUE if "denote"
21904 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021905 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010021906 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021907get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021909 int error = FALSE;
21910
21911 return get_tv_number_chk(varp, &error); /* return 0L on error */
21912}
21913
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021914 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021915get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021916{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021917 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021918
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021919 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021921 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021922 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021923 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021924#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021925 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021926 break;
21927#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021928 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021929 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021930 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021931 break;
21932 case VAR_STRING:
21933 if (varp->vval.v_string != NULL)
21934 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021935 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021936 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021937 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021938 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021939 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021940 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021941 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021942 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021943 case VAR_SPECIAL:
21944 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21945 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021946 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021947#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021948 EMSG(_("E910: Using a Job as a Number"));
21949 break;
21950#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021951 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021952#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021953 EMSG(_("E913: Using a Channel as a Number"));
21954 break;
21955#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021956 case VAR_UNKNOWN:
21957 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021958 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021959 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021960 if (denote == NULL) /* useful for values that must be unsigned */
21961 n = -1;
21962 else
21963 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021964 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021965}
21966
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021967#ifdef FEAT_FLOAT
21968 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021969get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021970{
21971 switch (varp->v_type)
21972 {
21973 case VAR_NUMBER:
21974 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021975 case VAR_FLOAT:
21976 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021977 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021978 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021979 EMSG(_("E891: Using a Funcref as a Float"));
21980 break;
21981 case VAR_STRING:
21982 EMSG(_("E892: Using a String as a Float"));
21983 break;
21984 case VAR_LIST:
21985 EMSG(_("E893: Using a List as a Float"));
21986 break;
21987 case VAR_DICT:
21988 EMSG(_("E894: Using a Dictionary as a Float"));
21989 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021990 case VAR_SPECIAL:
21991 EMSG(_("E907: Using a special value as a Float"));
21992 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021993 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021994# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021995 EMSG(_("E911: Using a Job as a Float"));
21996 break;
21997# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021998 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021999# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022000 EMSG(_("E914: Using a Channel as a Float"));
22001 break;
22002# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022003 case VAR_UNKNOWN:
22004 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022005 break;
22006 }
22007 return 0;
22008}
22009#endif
22010
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022012 * Get the lnum from the first argument.
22013 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022014 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022015 */
22016 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022017get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022018{
Bram Moolenaar33570922005-01-25 22:26:29 +000022019 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022020 linenr_T lnum;
22021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022022 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022023 if (lnum == 0) /* no valid number, try using line() */
22024 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022025 rettv.v_type = VAR_NUMBER;
22026 f_line(argvars, &rettv);
22027 lnum = rettv.vval.v_number;
22028 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029 }
22030 return lnum;
22031}
22032
22033/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022034 * Get the lnum from the first argument.
22035 * Also accepts "$", then "buf" is used.
22036 * Returns 0 on error.
22037 */
22038 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022039get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022040{
22041 if (argvars[0].v_type == VAR_STRING
22042 && argvars[0].vval.v_string != NULL
22043 && argvars[0].vval.v_string[0] == '$'
22044 && buf != NULL)
22045 return buf->b_ml.ml_line_count;
22046 return get_tv_number_chk(&argvars[0], NULL);
22047}
22048
22049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050 * Get the string value of a variable.
22051 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022052 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22053 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054 * If the String variable has never been set, return an empty string.
22055 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022056 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22057 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022058 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022059 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022060get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061{
22062 static char_u mybuf[NUMBUFLEN];
22063
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022064 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022065}
22066
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022067 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022068get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022069{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022070 char_u *res = get_tv_string_buf_chk(varp, buf);
22071
22072 return res != NULL ? res : (char_u *)"";
22073}
22074
Bram Moolenaar7d647822014-04-05 21:28:56 +020022075/*
22076 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22077 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022078 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022079get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022080{
22081 static char_u mybuf[NUMBUFLEN];
22082
22083 return get_tv_string_buf_chk(varp, mybuf);
22084}
22085
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022086 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022087get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022088{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022089 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022090 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022091 case VAR_NUMBER:
22092 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22093 return buf;
22094 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022095 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022096 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022097 break;
22098 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022099 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022100 break;
22101 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022102 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022103 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022104 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022105#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022106 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022107 break;
22108#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022109 case VAR_STRING:
22110 if (varp->vval.v_string != NULL)
22111 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022112 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022113 case VAR_SPECIAL:
22114 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22115 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022116 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022117#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022118 {
22119 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022120 char *status;
22121
22122 if (job == NULL)
22123 return (char_u *)"no process";
22124 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022125 : job->jv_status == JOB_ENDED ? "dead"
22126 : "run";
22127# ifdef UNIX
22128 vim_snprintf((char *)buf, NUMBUFLEN,
22129 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022130# elif defined(WIN32)
22131 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022132 "process %ld %s",
22133 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022134 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022135# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022136 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022137 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22138# endif
22139 return buf;
22140 }
22141#endif
22142 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022143 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022144#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022145 {
22146 channel_T *channel = varp->vval.v_channel;
22147 char *status = channel_status(channel);
22148
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022149 if (channel == NULL)
22150 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22151 else
22152 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022153 "channel %d %s", channel->ch_id, status);
22154 return buf;
22155 }
22156#endif
22157 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022158 case VAR_UNKNOWN:
22159 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022160 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022162 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022163}
22164
22165/*
22166 * Find variable "name" in the list of variables.
22167 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022168 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022169 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022170 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022172 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022173find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022174{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022176 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022177
Bram Moolenaara7043832005-01-21 11:56:39 +000022178 ht = find_var_ht(name, &varname);
22179 if (htp != NULL)
22180 *htp = ht;
22181 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022182 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022183 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184}
22185
22186/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022187 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022188 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022189 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022190 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022191find_var_in_ht(
22192 hashtab_T *ht,
22193 int htname,
22194 char_u *varname,
22195 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022196{
Bram Moolenaar33570922005-01-25 22:26:29 +000022197 hashitem_T *hi;
22198
22199 if (*varname == NUL)
22200 {
22201 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022202 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022203 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022204 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022205 case 'g': return &globvars_var;
22206 case 'v': return &vimvars_var;
22207 case 'b': return &curbuf->b_bufvar;
22208 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022209#ifdef FEAT_WINDOWS
22210 case 't': return &curtab->tp_winvar;
22211#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022212 case 'l': return current_funccal == NULL
22213 ? NULL : &current_funccal->l_vars_var;
22214 case 'a': return current_funccal == NULL
22215 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022216 }
22217 return NULL;
22218 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022219
22220 hi = hash_find(ht, varname);
22221 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022222 {
22223 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022224 * worked find the variable again. Don't auto-load a script if it was
22225 * loaded already, otherwise it would be loaded every time when
22226 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022227 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022228 {
22229 /* Note: script_autoload() may make "hi" invalid. It must either
22230 * be obtained again or not used. */
22231 if (!script_autoload(varname, FALSE) || aborting())
22232 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022233 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022234 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022235 if (HASHITEM_EMPTY(hi))
22236 return NULL;
22237 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022238 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022239}
22240
22241/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022242 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022243 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022244 * Set "varname" to the start of name without ':'.
22245 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022246 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022247find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022248{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022249 hashitem_T *hi;
22250
Bram Moolenaar73627d02015-08-11 15:46:09 +020022251 if (name[0] == NUL)
22252 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022253 if (name[1] != ':')
22254 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022255 /* The name must not start with a colon or #. */
22256 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022257 return NULL;
22258 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022259
22260 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022261 hi = hash_find(&compat_hashtab, name);
22262 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022263 return &compat_hashtab;
22264
Bram Moolenaar071d4272004-06-13 20:20:40 +000022265 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022266 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022267 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022268 }
22269 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022270 if (*name == 'g') /* global variable */
22271 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022272 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22273 */
22274 if (vim_strchr(name + 2, ':') != NULL
22275 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022276 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022277 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022278 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022279 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022280 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022281#ifdef FEAT_WINDOWS
22282 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022283 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022284#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022285 if (*name == 'v') /* v: variable */
22286 return &vimvarht;
22287 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022288 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022289 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022290 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022291 if (*name == 's' /* script variable */
22292 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22293 return &SCRIPT_VARS(current_SID);
22294 return NULL;
22295}
22296
22297/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022298 * Get function call environment based on bactrace debug level
22299 */
22300 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022301get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022302{
22303 int i;
22304 funccall_T *funccal;
22305 funccall_T *temp_funccal;
22306
22307 funccal = current_funccal;
22308 if (debug_backtrace_level > 0)
22309 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022310 for (i = 0; i < debug_backtrace_level; i++)
22311 {
22312 temp_funccal = funccal->caller;
22313 if (temp_funccal)
22314 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022315 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022316 /* backtrace level overflow. reset to max */
22317 debug_backtrace_level = i;
22318 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022319 }
22320 return funccal;
22321}
22322
22323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022324 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022325 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022326 * Returns NULL when it doesn't exist.
22327 */
22328 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022329get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330{
Bram Moolenaar33570922005-01-25 22:26:29 +000022331 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022332
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022333 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022334 if (v == NULL)
22335 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022336 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022337}
22338
22339/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022340 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022341 * sourcing this script and when executing functions defined in the script.
22342 */
22343 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022344new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022345{
Bram Moolenaara7043832005-01-21 11:56:39 +000022346 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022347 hashtab_T *ht;
22348 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022349
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22351 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022352 /* Re-allocating ga_data means that an ht_array pointing to
22353 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022354 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022355 for (i = 1; i <= ga_scripts.ga_len; ++i)
22356 {
22357 ht = &SCRIPT_VARS(i);
22358 if (ht->ht_mask == HT_INIT_SIZE - 1)
22359 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022360 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022361 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022362 }
22363
Bram Moolenaar071d4272004-06-13 20:20:40 +000022364 while (ga_scripts.ga_len < id)
22365 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022366 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022367 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022368 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022370 }
22371 }
22372}
22373
22374/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022375 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22376 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022377 */
22378 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022379init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380{
Bram Moolenaar33570922005-01-25 22:26:29 +000022381 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022382 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022383 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022384 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022385 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022386 dict_var->di_tv.vval.v_dict = dict;
22387 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022388 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022389 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22390 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022391}
22392
22393/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022394 * Unreference a dictionary initialized by init_var_dict().
22395 */
22396 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022397unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022398{
22399 /* Now the dict needs to be freed if no one else is using it, go back to
22400 * normal reference counting. */
22401 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22402 dict_unref(dict);
22403}
22404
22405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022407 * Frees all allocated variables and the value they contain.
22408 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022409 */
22410 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022411vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022412{
22413 vars_clear_ext(ht, TRUE);
22414}
22415
22416/*
22417 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22418 */
22419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022420vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022421{
Bram Moolenaara7043832005-01-21 11:56:39 +000022422 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022423 hashitem_T *hi;
22424 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022425
Bram Moolenaar33570922005-01-25 22:26:29 +000022426 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022427 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022428 for (hi = ht->ht_array; todo > 0; ++hi)
22429 {
22430 if (!HASHITEM_EMPTY(hi))
22431 {
22432 --todo;
22433
Bram Moolenaar33570922005-01-25 22:26:29 +000022434 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022435 * ht_array might change then. hash_clear() takes care of it
22436 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022437 v = HI2DI(hi);
22438 if (free_val)
22439 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022440 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022441 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022442 }
22443 }
22444 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022445 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022446}
22447
Bram Moolenaara7043832005-01-21 11:56:39 +000022448/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022449 * Delete a variable from hashtab "ht" at item "hi".
22450 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022453delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022454{
Bram Moolenaar33570922005-01-25 22:26:29 +000022455 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022456
22457 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022458 clear_tv(&di->di_tv);
22459 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022460}
22461
22462/*
22463 * List the value of one internal variable.
22464 */
22465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022466list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022467{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022468 char_u *tofree;
22469 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022470 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022471
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022472 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022473 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022474 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022475 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022476}
22477
Bram Moolenaar071d4272004-06-13 20:20:40 +000022478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022479list_one_var_a(
22480 char_u *prefix,
22481 char_u *name,
22482 int type,
22483 char_u *string,
22484 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485{
Bram Moolenaar31859182007-08-14 20:41:13 +000022486 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22487 msg_start();
22488 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022489 if (name != NULL) /* "a:" vars don't have a name stored */
22490 msg_puts(name);
22491 msg_putchar(' ');
22492 msg_advance(22);
22493 if (type == VAR_NUMBER)
22494 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022495 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022496 msg_putchar('*');
22497 else if (type == VAR_LIST)
22498 {
22499 msg_putchar('[');
22500 if (*string == '[')
22501 ++string;
22502 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022503 else if (type == VAR_DICT)
22504 {
22505 msg_putchar('{');
22506 if (*string == '{')
22507 ++string;
22508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022509 else
22510 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022511
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022513
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022514 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022515 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022516 if (*first)
22517 {
22518 msg_clr_eos();
22519 *first = FALSE;
22520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022521}
22522
22523/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022524 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022525 * If the variable already exists, the value is updated.
22526 * Otherwise the variable is created.
22527 */
22528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022529set_var(
22530 char_u *name,
22531 typval_T *tv,
22532 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022533{
Bram Moolenaar33570922005-01-25 22:26:29 +000022534 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022535 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022536 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022537
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022538 ht = find_var_ht(name, &varname);
22539 if (ht == NULL || *varname == NUL)
22540 {
22541 EMSG2(_(e_illvar), name);
22542 return;
22543 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022544 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022545
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022546 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
22547 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022548 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022549
Bram Moolenaar33570922005-01-25 22:26:29 +000022550 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022551 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022552 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022553 if (var_check_ro(v->di_flags, name, FALSE)
22554 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022555 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022556
22557 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022558 * Handle setting internal v: variables separately where needed to
22559 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022560 */
22561 if (ht == &vimvarht)
22562 {
22563 if (v->di_tv.v_type == VAR_STRING)
22564 {
22565 vim_free(v->di_tv.vval.v_string);
22566 if (copy || tv->v_type != VAR_STRING)
22567 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22568 else
22569 {
22570 /* Take over the string to avoid an extra alloc/free. */
22571 v->di_tv.vval.v_string = tv->vval.v_string;
22572 tv->vval.v_string = NULL;
22573 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022574 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022575 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022576 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022577 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022578 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022579 if (STRCMP(varname, "searchforward") == 0)
22580 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022581#ifdef FEAT_SEARCH_EXTRA
22582 else if (STRCMP(varname, "hlsearch") == 0)
22583 {
22584 no_hlsearch = !v->di_tv.vval.v_number;
22585 redraw_all_later(SOME_VALID);
22586 }
22587#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022588 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022589 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022590 else if (v->di_tv.v_type != tv->v_type)
22591 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022592 }
22593
22594 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022595 }
22596 else /* add a new variable */
22597 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022598 /* Can't add "v:" variable. */
22599 if (ht == &vimvarht)
22600 {
22601 EMSG2(_(e_illvar), name);
22602 return;
22603 }
22604
Bram Moolenaar92124a32005-06-17 22:03:40 +000022605 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022606 if (!valid_varname(varname))
22607 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022608
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022609 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22610 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022611 if (v == NULL)
22612 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022613 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022614 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022615 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022616 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022617 return;
22618 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022619 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022621
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022622 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022623 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022624 else
22625 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022626 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022627 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022628 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630}
22631
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022632/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022633 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022634 * Also give an error message.
22635 */
22636 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022637var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022638{
22639 if (flags & DI_FLAGS_RO)
22640 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022641 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022642 return TRUE;
22643 }
22644 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22645 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022646 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022647 return TRUE;
22648 }
22649 return FALSE;
22650}
22651
22652/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022653 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22654 * Also give an error message.
22655 */
22656 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022657var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022658{
22659 if (flags & DI_FLAGS_FIX)
22660 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022661 EMSG2(_("E795: Cannot delete variable %s"),
22662 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022663 return TRUE;
22664 }
22665 return FALSE;
22666}
22667
22668/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022669 * Check if a funcref is assigned to a valid variable name.
22670 * Return TRUE and give an error if not.
22671 */
22672 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022673var_check_func_name(
22674 char_u *name, /* points to start of variable name */
22675 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022676{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022677 /* Allow for w: b: s: and t:. */
22678 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022679 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22680 ? name[2] : name[0]))
22681 {
22682 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22683 name);
22684 return TRUE;
22685 }
22686 /* Don't allow hiding a function. When "v" is not NULL we might be
22687 * assigning another function to the same var, the type is checked
22688 * below. */
22689 if (new_var && function_exists(name))
22690 {
22691 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22692 name);
22693 return TRUE;
22694 }
22695 return FALSE;
22696}
22697
22698/*
22699 * Check if a variable name is valid.
22700 * Return FALSE and give an error if not.
22701 */
22702 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022703valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022704{
22705 char_u *p;
22706
22707 for (p = varname; *p != NUL; ++p)
22708 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22709 && *p != AUTOLOAD_CHAR)
22710 {
22711 EMSG2(_(e_illvar), varname);
22712 return FALSE;
22713 }
22714 return TRUE;
22715}
22716
22717/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022718 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022719 * Also give an error message, using "name" or _("name") when use_gettext is
22720 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022721 */
22722 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022723tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022724{
22725 if (lock & VAR_LOCKED)
22726 {
22727 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022728 name == NULL ? (char_u *)_("Unknown")
22729 : use_gettext ? (char_u *)_(name)
22730 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022731 return TRUE;
22732 }
22733 if (lock & VAR_FIXED)
22734 {
22735 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022736 name == NULL ? (char_u *)_("Unknown")
22737 : use_gettext ? (char_u *)_(name)
22738 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022739 return TRUE;
22740 }
22741 return FALSE;
22742}
22743
22744/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022745 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022746 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022747 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022748 * It is OK for "from" and "to" to point to the same item. This is used to
22749 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022750 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022751 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022752copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022754 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022755 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022756 switch (from->v_type)
22757 {
22758 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022759 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022760 to->vval.v_number = from->vval.v_number;
22761 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022762 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022763#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022764 to->vval.v_float = from->vval.v_float;
22765 break;
22766#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022767 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022768#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022769 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010022770 if (to->vval.v_job != NULL)
22771 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022772 break;
22773#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022774 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022775#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022776 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022777 if (to->vval.v_channel != NULL)
22778 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022779 break;
22780#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022781 case VAR_STRING:
22782 case VAR_FUNC:
22783 if (from->vval.v_string == NULL)
22784 to->vval.v_string = NULL;
22785 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022786 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022787 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022788 if (from->v_type == VAR_FUNC)
22789 func_ref(to->vval.v_string);
22790 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022791 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022792 case VAR_PARTIAL:
22793 if (from->vval.v_partial == NULL)
22794 to->vval.v_partial = NULL;
22795 else
22796 {
22797 to->vval.v_partial = from->vval.v_partial;
22798 ++to->vval.v_partial->pt_refcount;
22799 }
22800 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022801 case VAR_LIST:
22802 if (from->vval.v_list == NULL)
22803 to->vval.v_list = NULL;
22804 else
22805 {
22806 to->vval.v_list = from->vval.v_list;
22807 ++to->vval.v_list->lv_refcount;
22808 }
22809 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022810 case VAR_DICT:
22811 if (from->vval.v_dict == NULL)
22812 to->vval.v_dict = NULL;
22813 else
22814 {
22815 to->vval.v_dict = from->vval.v_dict;
22816 ++to->vval.v_dict->dv_refcount;
22817 }
22818 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022819 case VAR_UNKNOWN:
22820 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022821 break;
22822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022823}
22824
22825/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022826 * Make a copy of an item.
22827 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022828 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22829 * reference to an already copied list/dict can be used.
22830 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022831 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022832 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022833item_copy(
22834 typval_T *from,
22835 typval_T *to,
22836 int deep,
22837 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022838{
22839 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022840 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022841
Bram Moolenaar33570922005-01-25 22:26:29 +000022842 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022843 {
22844 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022845 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022846 }
22847 ++recurse;
22848
22849 switch (from->v_type)
22850 {
22851 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022852 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022853 case VAR_STRING:
22854 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022855 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010022856 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022857 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022858 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022859 copy_tv(from, to);
22860 break;
22861 case VAR_LIST:
22862 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022863 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022864 if (from->vval.v_list == NULL)
22865 to->vval.v_list = NULL;
22866 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22867 {
22868 /* use the copy made earlier */
22869 to->vval.v_list = from->vval.v_list->lv_copylist;
22870 ++to->vval.v_list->lv_refcount;
22871 }
22872 else
22873 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22874 if (to->vval.v_list == NULL)
22875 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022876 break;
22877 case VAR_DICT:
22878 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022879 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022880 if (from->vval.v_dict == NULL)
22881 to->vval.v_dict = NULL;
22882 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22883 {
22884 /* use the copy made earlier */
22885 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22886 ++to->vval.v_dict->dv_refcount;
22887 }
22888 else
22889 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22890 if (to->vval.v_dict == NULL)
22891 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022892 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022893 case VAR_UNKNOWN:
22894 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022895 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022896 }
22897 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022898 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022899}
22900
22901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022902 * ":echo expr1 ..." print each argument separated with a space, add a
22903 * newline at the end.
22904 * ":echon expr1 ..." print each argument plain.
22905 */
22906 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022907ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908{
22909 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022910 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022911 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912 char_u *p;
22913 int needclr = TRUE;
22914 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022915 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022916
22917 if (eap->skip)
22918 ++emsg_skip;
22919 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22920 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022921 /* If eval1() causes an error message the text from the command may
22922 * still need to be cleared. E.g., "echo 22,44". */
22923 need_clr_eos = needclr;
22924
Bram Moolenaar071d4272004-06-13 20:20:40 +000022925 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022926 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022927 {
22928 /*
22929 * Report the invalid expression unless the expression evaluation
22930 * has been cancelled due to an aborting error, an interrupt, or an
22931 * exception.
22932 */
22933 if (!aborting())
22934 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022935 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936 break;
22937 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022938 need_clr_eos = FALSE;
22939
Bram Moolenaar071d4272004-06-13 20:20:40 +000022940 if (!eap->skip)
22941 {
22942 if (atstart)
22943 {
22944 atstart = FALSE;
22945 /* Call msg_start() after eval1(), evaluating the expression
22946 * may cause a message to appear. */
22947 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022948 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022949 /* Mark the saved text as finishing the line, so that what
22950 * follows is displayed on a new line when scrolling back
22951 * at the more prompt. */
22952 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022953 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022955 }
22956 else if (eap->cmdidx == CMD_echo)
22957 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022958 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022959 if (p != NULL)
22960 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022962 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022963 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022964 if (*p != TAB && needclr)
22965 {
22966 /* remove any text still there from the command */
22967 msg_clr_eos();
22968 needclr = FALSE;
22969 }
22970 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022971 }
22972 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022973 {
22974#ifdef FEAT_MBYTE
22975 if (has_mbyte)
22976 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022977 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022978
22979 (void)msg_outtrans_len_attr(p, i, echo_attr);
22980 p += i - 1;
22981 }
22982 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022983#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022984 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022986 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022987 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022988 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022989 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022990 arg = skipwhite(arg);
22991 }
22992 eap->nextcmd = check_nextcmd(arg);
22993
22994 if (eap->skip)
22995 --emsg_skip;
22996 else
22997 {
22998 /* remove text that may still be there from the command */
22999 if (needclr)
23000 msg_clr_eos();
23001 if (eap->cmdidx == CMD_echo)
23002 msg_end();
23003 }
23004}
23005
23006/*
23007 * ":echohl {name}".
23008 */
23009 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023010ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023011{
23012 int id;
23013
23014 id = syn_name2id(eap->arg);
23015 if (id == 0)
23016 echo_attr = 0;
23017 else
23018 echo_attr = syn_id2attr(id);
23019}
23020
23021/*
23022 * ":execute expr1 ..." execute the result of an expression.
23023 * ":echomsg expr1 ..." Print a message
23024 * ":echoerr expr1 ..." Print an error
23025 * Each gets spaces around each argument and a newline at the end for
23026 * echo commands
23027 */
23028 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023029ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030{
23031 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023032 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023033 int ret = OK;
23034 char_u *p;
23035 garray_T ga;
23036 int len;
23037 int save_did_emsg;
23038
23039 ga_init2(&ga, 1, 80);
23040
23041 if (eap->skip)
23042 ++emsg_skip;
23043 while (*arg != NUL && *arg != '|' && *arg != '\n')
23044 {
23045 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023046 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023047 {
23048 /*
23049 * Report the invalid expression unless the expression evaluation
23050 * has been cancelled due to an aborting error, an interrupt, or an
23051 * exception.
23052 */
23053 if (!aborting())
23054 EMSG2(_(e_invexpr2), p);
23055 ret = FAIL;
23056 break;
23057 }
23058
23059 if (!eap->skip)
23060 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023061 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023062 len = (int)STRLEN(p);
23063 if (ga_grow(&ga, len + 2) == FAIL)
23064 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023065 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023066 ret = FAIL;
23067 break;
23068 }
23069 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023070 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023071 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023072 ga.ga_len += len;
23073 }
23074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023075 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023076 arg = skipwhite(arg);
23077 }
23078
23079 if (ret != FAIL && ga.ga_data != NULL)
23080 {
23081 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023082 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023083 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023084 out_flush();
23085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023086 else if (eap->cmdidx == CMD_echoerr)
23087 {
23088 /* We don't want to abort following commands, restore did_emsg. */
23089 save_did_emsg = did_emsg;
23090 EMSG((char_u *)ga.ga_data);
23091 if (!force_abort)
23092 did_emsg = save_did_emsg;
23093 }
23094 else if (eap->cmdidx == CMD_execute)
23095 do_cmdline((char_u *)ga.ga_data,
23096 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23097 }
23098
23099 ga_clear(&ga);
23100
23101 if (eap->skip)
23102 --emsg_skip;
23103
23104 eap->nextcmd = check_nextcmd(arg);
23105}
23106
23107/*
23108 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23109 * "arg" points to the "&" or '+' when called, to "option" when returning.
23110 * Returns NULL when no option name found. Otherwise pointer to the char
23111 * after the option name.
23112 */
23113 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023114find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023115{
23116 char_u *p = *arg;
23117
23118 ++p;
23119 if (*p == 'g' && p[1] == ':')
23120 {
23121 *opt_flags = OPT_GLOBAL;
23122 p += 2;
23123 }
23124 else if (*p == 'l' && p[1] == ':')
23125 {
23126 *opt_flags = OPT_LOCAL;
23127 p += 2;
23128 }
23129 else
23130 *opt_flags = 0;
23131
23132 if (!ASCII_ISALPHA(*p))
23133 return NULL;
23134 *arg = p;
23135
23136 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23137 p += 4; /* termcap option */
23138 else
23139 while (ASCII_ISALPHA(*p))
23140 ++p;
23141 return p;
23142}
23143
23144/*
23145 * ":function"
23146 */
23147 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023148ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023149{
23150 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023151 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023152 int j;
23153 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023154 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023155 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023156 char_u *name = NULL;
23157 char_u *p;
23158 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023159 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023160 garray_T newargs;
23161 garray_T newlines;
23162 int varargs = FALSE;
23163 int mustend = FALSE;
23164 int flags = 0;
23165 ufunc_T *fp;
23166 int indent;
23167 int nesting;
23168 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023169 dictitem_T *v;
23170 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023171 static int func_nr = 0; /* number for nameless function */
23172 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023173 hashtab_T *ht;
23174 int todo;
23175 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023176 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023177
23178 /*
23179 * ":function" without argument: list functions.
23180 */
23181 if (ends_excmd(*eap->arg))
23182 {
23183 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023184 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023185 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023186 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023187 {
23188 if (!HASHITEM_EMPTY(hi))
23189 {
23190 --todo;
23191 fp = HI2UF(hi);
23192 if (!isdigit(*fp->uf_name))
23193 list_func_head(fp, FALSE);
23194 }
23195 }
23196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023197 eap->nextcmd = check_nextcmd(eap->arg);
23198 return;
23199 }
23200
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023201 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023202 * ":function /pat": list functions matching pattern.
23203 */
23204 if (*eap->arg == '/')
23205 {
23206 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23207 if (!eap->skip)
23208 {
23209 regmatch_T regmatch;
23210
23211 c = *p;
23212 *p = NUL;
23213 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23214 *p = c;
23215 if (regmatch.regprog != NULL)
23216 {
23217 regmatch.rm_ic = p_ic;
23218
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023219 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023220 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23221 {
23222 if (!HASHITEM_EMPTY(hi))
23223 {
23224 --todo;
23225 fp = HI2UF(hi);
23226 if (!isdigit(*fp->uf_name)
23227 && vim_regexec(&regmatch, fp->uf_name, 0))
23228 list_func_head(fp, FALSE);
23229 }
23230 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023231 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023232 }
23233 }
23234 if (*p == '/')
23235 ++p;
23236 eap->nextcmd = check_nextcmd(p);
23237 return;
23238 }
23239
23240 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023241 * Get the function name. There are these situations:
23242 * func normal function name
23243 * "name" == func, "fudi.fd_dict" == NULL
23244 * dict.func new dictionary entry
23245 * "name" == NULL, "fudi.fd_dict" set,
23246 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23247 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023248 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023249 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23250 * dict.func existing dict entry that's not a Funcref
23251 * "name" == NULL, "fudi.fd_dict" set,
23252 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023253 * s:func script-local function name
23254 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023255 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023256 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023257 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023258 paren = (vim_strchr(p, '(') != NULL);
23259 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023260 {
23261 /*
23262 * Return on an invalid expression in braces, unless the expression
23263 * evaluation has been cancelled due to an aborting error, an
23264 * interrupt, or an exception.
23265 */
23266 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023267 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023268 if (!eap->skip && fudi.fd_newkey != NULL)
23269 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023270 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023271 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023273 else
23274 eap->skip = TRUE;
23275 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023276
Bram Moolenaar071d4272004-06-13 20:20:40 +000023277 /* An error in a function call during evaluation of an expression in magic
23278 * braces should not cause the function not to be defined. */
23279 saved_did_emsg = did_emsg;
23280 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023281
23282 /*
23283 * ":function func" with only function name: list function.
23284 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023285 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023286 {
23287 if (!ends_excmd(*skipwhite(p)))
23288 {
23289 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023290 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023291 }
23292 eap->nextcmd = check_nextcmd(p);
23293 if (eap->nextcmd != NULL)
23294 *p = NUL;
23295 if (!eap->skip && !got_int)
23296 {
23297 fp = find_func(name);
23298 if (fp != NULL)
23299 {
23300 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023301 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023303 if (FUNCLINE(fp, j) == NULL)
23304 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023305 msg_putchar('\n');
23306 msg_outnum((long)(j + 1));
23307 if (j < 9)
23308 msg_putchar(' ');
23309 if (j < 99)
23310 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023311 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023312 out_flush(); /* show a line at a time */
23313 ui_breakcheck();
23314 }
23315 if (!got_int)
23316 {
23317 msg_putchar('\n');
23318 msg_puts((char_u *)" endfunction");
23319 }
23320 }
23321 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023322 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023323 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023324 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023325 }
23326
23327 /*
23328 * ":function name(arg1, arg2)" Define function.
23329 */
23330 p = skipwhite(p);
23331 if (*p != '(')
23332 {
23333 if (!eap->skip)
23334 {
23335 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023336 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023337 }
23338 /* attempt to continue by skipping some text */
23339 if (vim_strchr(p, '(') != NULL)
23340 p = vim_strchr(p, '(');
23341 }
23342 p = skipwhite(p + 1);
23343
23344 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23345 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23346
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023347 if (!eap->skip)
23348 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023349 /* Check the name of the function. Unless it's a dictionary function
23350 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023351 if (name != NULL)
23352 arg = name;
23353 else
23354 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023355 if (arg != NULL && (fudi.fd_di == NULL
23356 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023357 {
23358 if (*arg == K_SPECIAL)
23359 j = 3;
23360 else
23361 j = 0;
23362 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23363 : eval_isnamec(arg[j])))
23364 ++j;
23365 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023366 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023367 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023368 /* Disallow using the g: dict. */
23369 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23370 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023371 }
23372
Bram Moolenaar071d4272004-06-13 20:20:40 +000023373 /*
23374 * Isolate the arguments: "arg1, arg2, ...)"
23375 */
23376 while (*p != ')')
23377 {
23378 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23379 {
23380 varargs = TRUE;
23381 p += 3;
23382 mustend = TRUE;
23383 }
23384 else
23385 {
23386 arg = p;
23387 while (ASCII_ISALNUM(*p) || *p == '_')
23388 ++p;
23389 if (arg == p || isdigit(*arg)
23390 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23391 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23392 {
23393 if (!eap->skip)
23394 EMSG2(_("E125: Illegal argument: %s"), arg);
23395 break;
23396 }
23397 if (ga_grow(&newargs, 1) == FAIL)
23398 goto erret;
23399 c = *p;
23400 *p = NUL;
23401 arg = vim_strsave(arg);
23402 if (arg == NULL)
23403 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023404
23405 /* Check for duplicate argument name. */
23406 for (i = 0; i < newargs.ga_len; ++i)
23407 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23408 {
23409 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023410 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023411 goto erret;
23412 }
23413
Bram Moolenaar071d4272004-06-13 20:20:40 +000023414 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23415 *p = c;
23416 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023417 if (*p == ',')
23418 ++p;
23419 else
23420 mustend = TRUE;
23421 }
23422 p = skipwhite(p);
23423 if (mustend && *p != ')')
23424 {
23425 if (!eap->skip)
23426 EMSG2(_(e_invarg2), eap->arg);
23427 break;
23428 }
23429 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023430 if (*p != ')')
23431 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023432 ++p; /* skip the ')' */
23433
Bram Moolenaare9a41262005-01-15 22:18:47 +000023434 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023435 for (;;)
23436 {
23437 p = skipwhite(p);
23438 if (STRNCMP(p, "range", 5) == 0)
23439 {
23440 flags |= FC_RANGE;
23441 p += 5;
23442 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023443 else if (STRNCMP(p, "dict", 4) == 0)
23444 {
23445 flags |= FC_DICT;
23446 p += 4;
23447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023448 else if (STRNCMP(p, "abort", 5) == 0)
23449 {
23450 flags |= FC_ABORT;
23451 p += 5;
23452 }
23453 else
23454 break;
23455 }
23456
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023457 /* When there is a line break use what follows for the function body.
23458 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23459 if (*p == '\n')
23460 line_arg = p + 1;
23461 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023462 EMSG(_(e_trailing));
23463
23464 /*
23465 * Read the body of the function, until ":endfunction" is found.
23466 */
23467 if (KeyTyped)
23468 {
23469 /* Check if the function already exists, don't let the user type the
23470 * whole function before telling him it doesn't work! For a script we
23471 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023472 if (!eap->skip && !eap->forceit)
23473 {
23474 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23475 EMSG(_(e_funcdict));
23476 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023477 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023479
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023480 if (!eap->skip && did_emsg)
23481 goto erret;
23482
Bram Moolenaar071d4272004-06-13 20:20:40 +000023483 msg_putchar('\n'); /* don't overwrite the function name */
23484 cmdline_row = msg_row;
23485 }
23486
23487 indent = 2;
23488 nesting = 0;
23489 for (;;)
23490 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023491 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023492 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023493 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023494 saved_wait_return = FALSE;
23495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023496 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023497 sourcing_lnum_off = sourcing_lnum;
23498
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023499 if (line_arg != NULL)
23500 {
23501 /* Use eap->arg, split up in parts by line breaks. */
23502 theline = line_arg;
23503 p = vim_strchr(theline, '\n');
23504 if (p == NULL)
23505 line_arg += STRLEN(line_arg);
23506 else
23507 {
23508 *p = NUL;
23509 line_arg = p + 1;
23510 }
23511 }
23512 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023513 theline = getcmdline(':', 0L, indent);
23514 else
23515 theline = eap->getline(':', eap->cookie, indent);
23516 if (KeyTyped)
23517 lines_left = Rows - 1;
23518 if (theline == NULL)
23519 {
23520 EMSG(_("E126: Missing :endfunction"));
23521 goto erret;
23522 }
23523
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023524 /* Detect line continuation: sourcing_lnum increased more than one. */
23525 if (sourcing_lnum > sourcing_lnum_off + 1)
23526 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23527 else
23528 sourcing_lnum_off = 0;
23529
Bram Moolenaar071d4272004-06-13 20:20:40 +000023530 if (skip_until != NULL)
23531 {
23532 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23533 * don't check for ":endfunc". */
23534 if (STRCMP(theline, skip_until) == 0)
23535 {
23536 vim_free(skip_until);
23537 skip_until = NULL;
23538 }
23539 }
23540 else
23541 {
23542 /* skip ':' and blanks*/
23543 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23544 ;
23545
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023546 /* Check for "endfunction". */
23547 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023548 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023549 if (line_arg == NULL)
23550 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023551 break;
23552 }
23553
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023554 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555 * at "end". */
23556 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23557 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023558 else if (STRNCMP(p, "if", 2) == 0
23559 || STRNCMP(p, "wh", 2) == 0
23560 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023561 || STRNCMP(p, "try", 3) == 0)
23562 indent += 2;
23563
23564 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023565 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023566 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023567 if (*p == '!')
23568 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023569 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010023570 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010023571 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023572 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023573 ++nesting;
23574 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023575 }
23576 }
23577
23578 /* Check for ":append" or ":insert". */
23579 p = skip_range(p, NULL);
23580 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23581 || (p[0] == 'i'
23582 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23583 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23584 skip_until = vim_strsave((char_u *)".");
23585
23586 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23587 arg = skipwhite(skiptowhite(p));
23588 if (arg[0] == '<' && arg[1] =='<'
23589 && ((p[0] == 'p' && p[1] == 'y'
23590 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23591 || (p[0] == 'p' && p[1] == 'e'
23592 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23593 || (p[0] == 't' && p[1] == 'c'
23594 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023595 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23596 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023597 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23598 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023599 || (p[0] == 'm' && p[1] == 'z'
23600 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023601 ))
23602 {
23603 /* ":python <<" continues until a dot, like ":append" */
23604 p = skipwhite(arg + 2);
23605 if (*p == NUL)
23606 skip_until = vim_strsave((char_u *)".");
23607 else
23608 skip_until = vim_strsave(p);
23609 }
23610 }
23611
23612 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023613 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023614 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023615 if (line_arg == NULL)
23616 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023617 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023618 }
23619
23620 /* Copy the line to newly allocated memory. get_one_sourceline()
23621 * allocates 250 bytes per line, this saves 80% on average. The cost
23622 * is an extra alloc/free. */
23623 p = vim_strsave(theline);
23624 if (p != NULL)
23625 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023626 if (line_arg == NULL)
23627 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023628 theline = p;
23629 }
23630
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023631 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23632
23633 /* Add NULL lines for continuation lines, so that the line count is
23634 * equal to the index in the growarray. */
23635 while (sourcing_lnum_off-- > 0)
23636 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023637
23638 /* Check for end of eap->arg. */
23639 if (line_arg != NULL && *line_arg == NUL)
23640 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023641 }
23642
23643 /* Don't define the function when skipping commands or when an error was
23644 * detected. */
23645 if (eap->skip || did_emsg)
23646 goto erret;
23647
23648 /*
23649 * If there are no errors, add the function
23650 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023651 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023652 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023653 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023654 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023655 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023656 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023657 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023658 goto erret;
23659 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023660
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023661 fp = find_func(name);
23662 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023663 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023664 if (!eap->forceit)
23665 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023666 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023667 goto erret;
23668 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023669 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023670 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023671 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023672 name);
23673 goto erret;
23674 }
23675 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023676 ga_clear_strings(&(fp->uf_args));
23677 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023678 vim_free(name);
23679 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023681 }
23682 else
23683 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023684 char numbuf[20];
23685
23686 fp = NULL;
23687 if (fudi.fd_newkey == NULL && !eap->forceit)
23688 {
23689 EMSG(_(e_funcdict));
23690 goto erret;
23691 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023692 if (fudi.fd_di == NULL)
23693 {
23694 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023695 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023696 goto erret;
23697 }
23698 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023699 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023700 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023701
23702 /* Give the function a sequential number. Can only be used with a
23703 * Funcref! */
23704 vim_free(name);
23705 sprintf(numbuf, "%d", ++func_nr);
23706 name = vim_strsave((char_u *)numbuf);
23707 if (name == NULL)
23708 goto erret;
23709 }
23710
23711 if (fp == NULL)
23712 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023713 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023714 {
23715 int slen, plen;
23716 char_u *scriptname;
23717
23718 /* Check that the autoload name matches the script name. */
23719 j = FAIL;
23720 if (sourcing_name != NULL)
23721 {
23722 scriptname = autoload_name(name);
23723 if (scriptname != NULL)
23724 {
23725 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023726 plen = (int)STRLEN(p);
23727 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023728 if (slen > plen && fnamecmp(p,
23729 sourcing_name + slen - plen) == 0)
23730 j = OK;
23731 vim_free(scriptname);
23732 }
23733 }
23734 if (j == FAIL)
23735 {
23736 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23737 goto erret;
23738 }
23739 }
23740
23741 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023742 if (fp == NULL)
23743 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023744
23745 if (fudi.fd_dict != NULL)
23746 {
23747 if (fudi.fd_di == NULL)
23748 {
23749 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023750 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023751 if (fudi.fd_di == NULL)
23752 {
23753 vim_free(fp);
23754 goto erret;
23755 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023756 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23757 {
23758 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023759 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023760 goto erret;
23761 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023762 }
23763 else
23764 /* overwrite existing dict entry */
23765 clear_tv(&fudi.fd_di->di_tv);
23766 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023767 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023768 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023769 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023770
23771 /* behave like "dict" was used */
23772 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023773 }
23774
Bram Moolenaar071d4272004-06-13 20:20:40 +000023775 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023776 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023777 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23778 {
23779 vim_free(fp);
23780 goto erret;
23781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023782 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023783 fp->uf_args = newargs;
23784 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023785#ifdef FEAT_PROFILE
23786 fp->uf_tml_count = NULL;
23787 fp->uf_tml_total = NULL;
23788 fp->uf_tml_self = NULL;
23789 fp->uf_profiling = FALSE;
23790 if (prof_def_func())
23791 func_do_profile(fp);
23792#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023793 fp->uf_varargs = varargs;
23794 fp->uf_flags = flags;
23795 fp->uf_calls = 0;
23796 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023797 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023798
23799erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023800 ga_clear_strings(&newargs);
23801 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023802ret_free:
23803 vim_free(skip_until);
23804 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023805 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023806 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023807 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023808}
23809
23810/*
23811 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023812 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023813 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023814 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023815 * TFN_INT: internal function name OK
23816 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023817 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023818 * Advances "pp" to just after the function name (if no error).
23819 */
23820 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023821trans_function_name(
23822 char_u **pp,
23823 int skip, /* only find the end, don't evaluate */
23824 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010023825 funcdict_T *fdp, /* return: info about dictionary used */
23826 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023827{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023828 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023829 char_u *start;
23830 char_u *end;
23831 int lead;
23832 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023833 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023834 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023835
23836 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023837 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023838 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023839
23840 /* Check for hard coded <SNR>: already translated function ID (from a user
23841 * command). */
23842 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23843 && (*pp)[2] == (int)KE_SNR)
23844 {
23845 *pp += 3;
23846 len = get_id_len(pp) + 3;
23847 return vim_strnsave(start, len);
23848 }
23849
23850 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23851 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023852 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023853 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023854 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023855
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023856 /* Note that TFN_ flags use the same values as GLV_ flags. */
23857 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023858 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023859 if (end == start)
23860 {
23861 if (!skip)
23862 EMSG(_("E129: Function name required"));
23863 goto theend;
23864 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023865 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023866 {
23867 /*
23868 * Report an invalid expression in braces, unless the expression
23869 * evaluation has been cancelled due to an aborting error, an
23870 * interrupt, or an exception.
23871 */
23872 if (!aborting())
23873 {
23874 if (end != NULL)
23875 EMSG2(_(e_invarg2), start);
23876 }
23877 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023878 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023879 goto theend;
23880 }
23881
23882 if (lv.ll_tv != NULL)
23883 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023884 if (fdp != NULL)
23885 {
23886 fdp->fd_dict = lv.ll_dict;
23887 fdp->fd_newkey = lv.ll_newkey;
23888 lv.ll_newkey = NULL;
23889 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023890 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023891 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23892 {
23893 name = vim_strsave(lv.ll_tv->vval.v_string);
23894 *pp = end;
23895 }
23896 else
23897 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023898 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23899 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023900 EMSG(_(e_funcref));
23901 else
23902 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023903 name = NULL;
23904 }
23905 goto theend;
23906 }
23907
23908 if (lv.ll_name == NULL)
23909 {
23910 /* Error found, but continue after the function name. */
23911 *pp = end;
23912 goto theend;
23913 }
23914
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023915 /* Check if the name is a Funcref. If so, use the value. */
23916 if (lv.ll_exp_name != NULL)
23917 {
23918 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010023919 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023920 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023921 if (name == lv.ll_exp_name)
23922 name = NULL;
23923 }
23924 else
23925 {
23926 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010023927 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023928 if (name == *pp)
23929 name = NULL;
23930 }
23931 if (name != NULL)
23932 {
23933 name = vim_strsave(name);
23934 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023935 if (STRNCMP(name, "<SNR>", 5) == 0)
23936 {
23937 /* Change "<SNR>" to the byte sequence. */
23938 name[0] = K_SPECIAL;
23939 name[1] = KS_EXTRA;
23940 name[2] = (int)KE_SNR;
23941 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23942 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023943 goto theend;
23944 }
23945
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023946 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023947 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023948 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023949 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23950 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23951 {
23952 /* When there was "s:" already or the name expanded to get a
23953 * leading "s:" then remove it. */
23954 lv.ll_name += 2;
23955 len -= 2;
23956 lead = 2;
23957 }
23958 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023959 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023960 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023961 /* skip over "s:" and "g:" */
23962 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023963 lv.ll_name += 2;
23964 len = (int)(end - lv.ll_name);
23965 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023966
23967 /*
23968 * Copy the function name to allocated memory.
23969 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23970 * Accept <SNR>123_name() outside a script.
23971 */
23972 if (skip)
23973 lead = 0; /* do nothing */
23974 else if (lead > 0)
23975 {
23976 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023977 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23978 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023979 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023980 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023981 if (current_SID <= 0)
23982 {
23983 EMSG(_(e_usingsid));
23984 goto theend;
23985 }
23986 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23987 lead += (int)STRLEN(sid_buf);
23988 }
23989 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023990 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023991 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023992 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023993 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023994 goto theend;
23995 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023996 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023997 {
23998 char_u *cp = vim_strchr(lv.ll_name, ':');
23999
24000 if (cp != NULL && cp < end)
24001 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024002 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024003 goto theend;
24004 }
24005 }
24006
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024007 name = alloc((unsigned)(len + lead + 1));
24008 if (name != NULL)
24009 {
24010 if (lead > 0)
24011 {
24012 name[0] = K_SPECIAL;
24013 name[1] = KS_EXTRA;
24014 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024015 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024016 STRCPY(name + 3, sid_buf);
24017 }
24018 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024019 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024020 }
24021 *pp = end;
24022
24023theend:
24024 clear_lval(&lv);
24025 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024026}
24027
24028/*
24029 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24030 * Return 2 if "p" starts with "s:".
24031 * Return 0 otherwise.
24032 */
24033 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024034eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024035{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024036 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24037 * the standard library function. */
24038 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24039 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024040 return 5;
24041 if (p[0] == 's' && p[1] == ':')
24042 return 2;
24043 return 0;
24044}
24045
24046/*
24047 * Return TRUE if "p" starts with "<SID>" or "s:".
24048 * Only works if eval_fname_script() returned non-zero for "p"!
24049 */
24050 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024051eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024052{
24053 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24054}
24055
24056/*
24057 * List the head of the function: "name(arg1, arg2)".
24058 */
24059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024060list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024061{
24062 int j;
24063
24064 msg_start();
24065 if (indent)
24066 MSG_PUTS(" ");
24067 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024068 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024069 {
24070 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024071 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024072 }
24073 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024074 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024075 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024076 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024077 {
24078 if (j)
24079 MSG_PUTS(", ");
24080 msg_puts(FUNCARG(fp, j));
24081 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024082 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024083 {
24084 if (j)
24085 MSG_PUTS(", ");
24086 MSG_PUTS("...");
24087 }
24088 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024089 if (fp->uf_flags & FC_ABORT)
24090 MSG_PUTS(" abort");
24091 if (fp->uf_flags & FC_RANGE)
24092 MSG_PUTS(" range");
24093 if (fp->uf_flags & FC_DICT)
24094 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024095 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024096 if (p_verbose > 0)
24097 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024098}
24099
24100/*
24101 * Find a function by name, return pointer to it in ufuncs.
24102 * Return NULL for unknown function.
24103 */
24104 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024105find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024106{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024107 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024108
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024109 hi = hash_find(&func_hashtab, name);
24110 if (!HASHITEM_EMPTY(hi))
24111 return HI2UF(hi);
24112 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024113}
24114
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024115#if defined(EXITFREE) || defined(PROTO)
24116 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024117free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024118{
24119 hashitem_T *hi;
24120
24121 /* Need to start all over every time, because func_free() may change the
24122 * hash table. */
24123 while (func_hashtab.ht_used > 0)
24124 for (hi = func_hashtab.ht_array; ; ++hi)
24125 if (!HASHITEM_EMPTY(hi))
24126 {
24127 func_free(HI2UF(hi));
24128 break;
24129 }
24130}
24131#endif
24132
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024133 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024134translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024135{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024136 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024137 return find_internal_func(name) >= 0;
24138 return find_func(name) != NULL;
24139}
24140
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024141/*
24142 * Return TRUE if a function "name" exists.
24143 */
24144 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024145function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024146{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024147 char_u *nm = name;
24148 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024149 int n = FALSE;
24150
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024151 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024152 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024153 nm = skipwhite(nm);
24154
24155 /* Only accept "funcname", "funcname ", "funcname (..." and
24156 * "funcname(...", not "funcname!...". */
24157 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024158 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024159 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024160 return n;
24161}
24162
Bram Moolenaara1544c02013-05-30 12:35:52 +020024163 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024164get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024165{
24166 char_u *nm = name;
24167 char_u *p;
24168
Bram Moolenaar65639032016-03-16 21:40:30 +010024169 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024170
24171 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024172 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024173 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024174
Bram Moolenaara1544c02013-05-30 12:35:52 +020024175 vim_free(p);
24176 return NULL;
24177}
24178
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024179/*
24180 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024181 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24182 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024183 */
24184 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024185builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024186{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024187 char_u *p;
24188
24189 if (!ASCII_ISLOWER(name[0]))
24190 return FALSE;
24191 p = vim_strchr(name, AUTOLOAD_CHAR);
24192 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024193}
24194
Bram Moolenaar05159a02005-02-26 23:04:13 +000024195#if defined(FEAT_PROFILE) || defined(PROTO)
24196/*
24197 * Start profiling function "fp".
24198 */
24199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024200func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024201{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024202 int len = fp->uf_lines.ga_len;
24203
24204 if (len == 0)
24205 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024206 fp->uf_tm_count = 0;
24207 profile_zero(&fp->uf_tm_self);
24208 profile_zero(&fp->uf_tm_total);
24209 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024210 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024211 if (fp->uf_tml_total == NULL)
24212 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024213 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024214 if (fp->uf_tml_self == NULL)
24215 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024216 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024217 fp->uf_tml_idx = -1;
24218 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24219 || fp->uf_tml_self == NULL)
24220 return; /* out of memory */
24221
24222 fp->uf_profiling = TRUE;
24223}
24224
24225/*
24226 * Dump the profiling results for all functions in file "fd".
24227 */
24228 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024229func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024230{
24231 hashitem_T *hi;
24232 int todo;
24233 ufunc_T *fp;
24234 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024235 ufunc_T **sorttab;
24236 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024237
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024238 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024239 if (todo == 0)
24240 return; /* nothing to dump */
24241
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024242 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024243
Bram Moolenaar05159a02005-02-26 23:04:13 +000024244 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24245 {
24246 if (!HASHITEM_EMPTY(hi))
24247 {
24248 --todo;
24249 fp = HI2UF(hi);
24250 if (fp->uf_profiling)
24251 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024252 if (sorttab != NULL)
24253 sorttab[st_len++] = fp;
24254
Bram Moolenaar05159a02005-02-26 23:04:13 +000024255 if (fp->uf_name[0] == K_SPECIAL)
24256 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24257 else
24258 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24259 if (fp->uf_tm_count == 1)
24260 fprintf(fd, "Called 1 time\n");
24261 else
24262 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24263 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24264 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24265 fprintf(fd, "\n");
24266 fprintf(fd, "count total (s) self (s)\n");
24267
24268 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24269 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024270 if (FUNCLINE(fp, i) == NULL)
24271 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024272 prof_func_line(fd, fp->uf_tml_count[i],
24273 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024274 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24275 }
24276 fprintf(fd, "\n");
24277 }
24278 }
24279 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024280
24281 if (sorttab != NULL && st_len > 0)
24282 {
24283 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24284 prof_total_cmp);
24285 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24286 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24287 prof_self_cmp);
24288 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24289 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024290
24291 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024292}
Bram Moolenaar73830342005-02-28 22:48:19 +000024293
24294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024295prof_sort_list(
24296 FILE *fd,
24297 ufunc_T **sorttab,
24298 int st_len,
24299 char *title,
24300 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024301{
24302 int i;
24303 ufunc_T *fp;
24304
24305 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24306 fprintf(fd, "count total (s) self (s) function\n");
24307 for (i = 0; i < 20 && i < st_len; ++i)
24308 {
24309 fp = sorttab[i];
24310 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24311 prefer_self);
24312 if (fp->uf_name[0] == K_SPECIAL)
24313 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24314 else
24315 fprintf(fd, " %s()\n", fp->uf_name);
24316 }
24317 fprintf(fd, "\n");
24318}
24319
24320/*
24321 * Print the count and times for one function or function line.
24322 */
24323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024324prof_func_line(
24325 FILE *fd,
24326 int count,
24327 proftime_T *total,
24328 proftime_T *self,
24329 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024330{
24331 if (count > 0)
24332 {
24333 fprintf(fd, "%5d ", count);
24334 if (prefer_self && profile_equal(total, self))
24335 fprintf(fd, " ");
24336 else
24337 fprintf(fd, "%s ", profile_msg(total));
24338 if (!prefer_self && profile_equal(total, self))
24339 fprintf(fd, " ");
24340 else
24341 fprintf(fd, "%s ", profile_msg(self));
24342 }
24343 else
24344 fprintf(fd, " ");
24345}
24346
24347/*
24348 * Compare function for total time sorting.
24349 */
24350 static int
24351#ifdef __BORLANDC__
24352_RTLENTRYF
24353#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024354prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024355{
24356 ufunc_T *p1, *p2;
24357
24358 p1 = *(ufunc_T **)s1;
24359 p2 = *(ufunc_T **)s2;
24360 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24361}
24362
24363/*
24364 * Compare function for self time sorting.
24365 */
24366 static int
24367#ifdef __BORLANDC__
24368_RTLENTRYF
24369#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024370prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024371{
24372 ufunc_T *p1, *p2;
24373
24374 p1 = *(ufunc_T **)s1;
24375 p2 = *(ufunc_T **)s2;
24376 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24377}
24378
Bram Moolenaar05159a02005-02-26 23:04:13 +000024379#endif
24380
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024381/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024382 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024383 * Return TRUE if a package was loaded.
24384 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024385 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024386script_autoload(
24387 char_u *name,
24388 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024389{
24390 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024391 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024392 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024393 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024394
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024395 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024396 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024397 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024398 return FALSE;
24399
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024400 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024401
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024402 /* Find the name in the list of previously loaded package names. Skip
24403 * "autoload/", it's always the same. */
24404 for (i = 0; i < ga_loaded.ga_len; ++i)
24405 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24406 break;
24407 if (!reload && i < ga_loaded.ga_len)
24408 ret = FALSE; /* was loaded already */
24409 else
24410 {
24411 /* Remember the name if it wasn't loaded already. */
24412 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24413 {
24414 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24415 tofree = NULL;
24416 }
24417
24418 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024419 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024420 ret = TRUE;
24421 }
24422
24423 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024424 return ret;
24425}
24426
24427/*
24428 * Return the autoload script name for a function or variable name.
24429 * Returns NULL when out of memory.
24430 */
24431 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024432autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024433{
24434 char_u *p;
24435 char_u *scriptname;
24436
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024437 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024438 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24439 if (scriptname == NULL)
24440 return FALSE;
24441 STRCPY(scriptname, "autoload/");
24442 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024443 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024444 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024445 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024446 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024447 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024448}
24449
Bram Moolenaar071d4272004-06-13 20:20:40 +000024450#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24451
24452/*
24453 * Function given to ExpandGeneric() to obtain the list of user defined
24454 * function names.
24455 */
24456 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024457get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024458{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024459 static long_u done;
24460 static hashitem_T *hi;
24461 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024462
24463 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024464 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024465 done = 0;
24466 hi = func_hashtab.ht_array;
24467 }
24468 if (done < func_hashtab.ht_used)
24469 {
24470 if (done++ > 0)
24471 ++hi;
24472 while (HASHITEM_EMPTY(hi))
24473 ++hi;
24474 fp = HI2UF(hi);
24475
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024476 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024477 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024478
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024479 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24480 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024481
24482 cat_func_name(IObuff, fp);
24483 if (xp->xp_context != EXPAND_USER_FUNC)
24484 {
24485 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024486 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024487 STRCAT(IObuff, ")");
24488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024489 return IObuff;
24490 }
24491 return NULL;
24492}
24493
24494#endif /* FEAT_CMDL_COMPL */
24495
24496/*
24497 * Copy the function name of "fp" to buffer "buf".
24498 * "buf" must be able to hold the function name plus three bytes.
24499 * Takes care of script-local function names.
24500 */
24501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024502cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024503{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024504 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024505 {
24506 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024507 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024508 }
24509 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024510 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024511}
24512
24513/*
24514 * ":delfunction {name}"
24515 */
24516 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024517ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024518{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024519 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024520 char_u *p;
24521 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024522 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024523
24524 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024525 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024526 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024527 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024528 {
24529 if (fudi.fd_dict != NULL && !eap->skip)
24530 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024531 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024533 if (!ends_excmd(*skipwhite(p)))
24534 {
24535 vim_free(name);
24536 EMSG(_(e_trailing));
24537 return;
24538 }
24539 eap->nextcmd = check_nextcmd(p);
24540 if (eap->nextcmd != NULL)
24541 *p = NUL;
24542
24543 if (!eap->skip)
24544 fp = find_func(name);
24545 vim_free(name);
24546
24547 if (!eap->skip)
24548 {
24549 if (fp == NULL)
24550 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024551 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024552 return;
24553 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024554 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024555 {
24556 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24557 return;
24558 }
24559
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024560 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024561 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024562 /* Delete the dict item that refers to the function, it will
24563 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024564 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024565 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024566 else
24567 func_free(fp);
24568 }
24569}
24570
24571/*
24572 * Free a function and remove it from the list of functions.
24573 */
24574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024575func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024576{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024577 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024578
24579 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024580 ga_clear_strings(&(fp->uf_args));
24581 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024582#ifdef FEAT_PROFILE
24583 vim_free(fp->uf_tml_count);
24584 vim_free(fp->uf_tml_total);
24585 vim_free(fp->uf_tml_self);
24586#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024587
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024588 /* remove the function from the function hashtable */
24589 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24590 if (HASHITEM_EMPTY(hi))
24591 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024592 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024593 hash_remove(&func_hashtab, hi);
24594
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024595 vim_free(fp);
24596}
24597
24598/*
24599 * Unreference a Function: decrement the reference count and free it when it
24600 * becomes zero. Only for numbered functions.
24601 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024602 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024603func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024604{
24605 ufunc_T *fp;
24606
24607 if (name != NULL && isdigit(*name))
24608 {
24609 fp = find_func(name);
24610 if (fp == NULL)
24611 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024612 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024613 {
24614 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024615 * when "uf_calls" becomes zero. */
24616 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024617 func_free(fp);
24618 }
24619 }
24620}
24621
24622/*
24623 * Count a reference to a Function.
24624 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024625 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024626func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024627{
24628 ufunc_T *fp;
24629
24630 if (name != NULL && isdigit(*name))
24631 {
24632 fp = find_func(name);
24633 if (fp == NULL)
24634 EMSG2(_(e_intern2), "func_ref()");
24635 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024636 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024637 }
24638}
24639
24640/*
24641 * Call a user function.
24642 */
24643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024644call_user_func(
24645 ufunc_T *fp, /* pointer to function */
24646 int argcount, /* nr of args */
24647 typval_T *argvars, /* arguments */
24648 typval_T *rettv, /* return value */
24649 linenr_T firstline, /* first line of range */
24650 linenr_T lastline, /* last line of range */
24651 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024652{
Bram Moolenaar33570922005-01-25 22:26:29 +000024653 char_u *save_sourcing_name;
24654 linenr_T save_sourcing_lnum;
24655 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024656 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024657 int save_did_emsg;
24658 static int depth = 0;
24659 dictitem_T *v;
24660 int fixvar_idx = 0; /* index in fixvar[] */
24661 int i;
24662 int ai;
24663 char_u numbuf[NUMBUFLEN];
24664 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024665 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024666#ifdef FEAT_PROFILE
24667 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024668 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024670
24671 /* If depth of calling is getting too high, don't execute the function */
24672 if (depth >= p_mfd)
24673 {
24674 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024675 rettv->v_type = VAR_NUMBER;
24676 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024677 return;
24678 }
24679 ++depth;
24680
24681 line_breakcheck(); /* check for CTRL-C hit */
24682
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024683 fc = (funccall_T *)alloc(sizeof(funccall_T));
24684 fc->caller = current_funccal;
24685 current_funccal = fc;
24686 fc->func = fp;
24687 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024688 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024689 fc->linenr = 0;
24690 fc->returned = FALSE;
24691 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024692 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024693 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24694 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024695
Bram Moolenaar33570922005-01-25 22:26:29 +000024696 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024697 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024698 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24699 * each argument variable and saves a lot of time.
24700 */
24701 /*
24702 * Init l: variables.
24703 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024704 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024705 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024706 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024707 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24708 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024709 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024710 name = v->di_key;
24711 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024712 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024713 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024714 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024715 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024716 v->di_tv.vval.v_dict = selfdict;
24717 ++selfdict->dv_refcount;
24718 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024719
Bram Moolenaar33570922005-01-25 22:26:29 +000024720 /*
24721 * Init a: variables.
24722 * Set a:0 to "argcount".
24723 * Set a:000 to a list with room for the "..." arguments.
24724 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024725 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024726 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024727 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024728 /* Use "name" to avoid a warning from some compiler that checks the
24729 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024730 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024731 name = v->di_key;
24732 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024733 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024734 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024735 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024736 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024737 v->di_tv.vval.v_list = &fc->l_varlist;
24738 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24739 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24740 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024741
24742 /*
24743 * Set a:firstline to "firstline" and a:lastline to "lastline".
24744 * Set a:name to named arguments.
24745 * Set a:N to the "..." arguments.
24746 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024747 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024748 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024749 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024750 (varnumber_T)lastline);
24751 for (i = 0; i < argcount; ++i)
24752 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024753 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024754 if (ai < 0)
24755 /* named argument a:name */
24756 name = FUNCARG(fp, i);
24757 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024758 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024759 /* "..." argument a:1, a:2, etc. */
24760 sprintf((char *)numbuf, "%d", ai + 1);
24761 name = numbuf;
24762 }
24763 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24764 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024765 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024766 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24767 }
24768 else
24769 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024770 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24771 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024772 if (v == NULL)
24773 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024774 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024775 }
24776 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024777 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024778
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024779 /* Note: the values are copied directly to avoid alloc/free.
24780 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024781 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024782 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024783
24784 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24785 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024786 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24787 fc->l_listitems[ai].li_tv = argvars[i];
24788 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024789 }
24790 }
24791
Bram Moolenaar071d4272004-06-13 20:20:40 +000024792 /* Don't redraw while executing the function. */
24793 ++RedrawingDisabled;
24794 save_sourcing_name = sourcing_name;
24795 save_sourcing_lnum = sourcing_lnum;
24796 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024797 /* need space for function name + ("function " + 3) or "[number]" */
24798 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24799 + STRLEN(fp->uf_name) + 20;
24800 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024801 if (sourcing_name != NULL)
24802 {
24803 if (save_sourcing_name != NULL
24804 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024805 sprintf((char *)sourcing_name, "%s[%d]..",
24806 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024807 else
24808 STRCPY(sourcing_name, "function ");
24809 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24810
24811 if (p_verbose >= 12)
24812 {
24813 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024814 verbose_enter_scroll();
24815
Bram Moolenaar555b2802005-05-19 21:08:39 +000024816 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024817 if (p_verbose >= 14)
24818 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024819 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024820 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024821 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024822 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024823
24824 msg_puts((char_u *)"(");
24825 for (i = 0; i < argcount; ++i)
24826 {
24827 if (i > 0)
24828 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024829 if (argvars[i].v_type == VAR_NUMBER)
24830 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024831 else
24832 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024833 /* Do not want errors such as E724 here. */
24834 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024835 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024836 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024837 if (s != NULL)
24838 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024839 if (vim_strsize(s) > MSG_BUF_CLEN)
24840 {
24841 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24842 s = buf;
24843 }
24844 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024845 vim_free(tofree);
24846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024847 }
24848 }
24849 msg_puts((char_u *)")");
24850 }
24851 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024852
24853 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024854 --no_wait_return;
24855 }
24856 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024857#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024858 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024859 {
24860 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24861 func_do_profile(fp);
24862 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024863 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024864 {
24865 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024866 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024867 profile_zero(&fp->uf_tm_children);
24868 }
24869 script_prof_save(&wait_start);
24870 }
24871#endif
24872
Bram Moolenaar071d4272004-06-13 20:20:40 +000024873 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024874 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024875 save_did_emsg = did_emsg;
24876 did_emsg = FALSE;
24877
24878 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024879 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024880 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24881
24882 --RedrawingDisabled;
24883
24884 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024885 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024886 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024887 clear_tv(rettv);
24888 rettv->v_type = VAR_NUMBER;
24889 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024890 }
24891
Bram Moolenaar05159a02005-02-26 23:04:13 +000024892#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024893 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024894 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024895 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024896 profile_end(&call_start);
24897 profile_sub_wait(&wait_start, &call_start);
24898 profile_add(&fp->uf_tm_total, &call_start);
24899 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024900 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024901 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024902 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24903 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024904 }
24905 }
24906#endif
24907
Bram Moolenaar071d4272004-06-13 20:20:40 +000024908 /* when being verbose, mention the return value */
24909 if (p_verbose >= 12)
24910 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024911 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024912 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024913
Bram Moolenaar071d4272004-06-13 20:20:40 +000024914 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024915 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024916 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024917 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024918 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024919 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024920 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024921 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024922 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024923 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024924 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024925
Bram Moolenaar555b2802005-05-19 21:08:39 +000024926 /* The value may be very long. Skip the middle part, so that we
24927 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024928 * truncate it at the end. Don't want errors such as E724 here. */
24929 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024930 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024931 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024932 if (s != NULL)
24933 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024934 if (vim_strsize(s) > MSG_BUF_CLEN)
24935 {
24936 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24937 s = buf;
24938 }
24939 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024940 vim_free(tofree);
24941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024942 }
24943 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024944
24945 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024946 --no_wait_return;
24947 }
24948
24949 vim_free(sourcing_name);
24950 sourcing_name = save_sourcing_name;
24951 sourcing_lnum = save_sourcing_lnum;
24952 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024953#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024954 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024955 script_prof_restore(&wait_start);
24956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024957
24958 if (p_verbose >= 12 && sourcing_name != NULL)
24959 {
24960 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024961 verbose_enter_scroll();
24962
Bram Moolenaar555b2802005-05-19 21:08:39 +000024963 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024964 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024965
24966 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024967 --no_wait_return;
24968 }
24969
24970 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024971 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024972 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024973
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024974 /* If the a:000 list and the l: and a: dicts are not referenced we can
24975 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024976 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24977 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24978 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24979 {
24980 free_funccal(fc, FALSE);
24981 }
24982 else
24983 {
24984 hashitem_T *hi;
24985 listitem_T *li;
24986 int todo;
24987
24988 /* "fc" is still in use. This can happen when returning "a:000" or
24989 * assigning "l:" to a global variable.
24990 * Link "fc" in the list for garbage collection later. */
24991 fc->caller = previous_funccal;
24992 previous_funccal = fc;
24993
24994 /* Make a copy of the a: variables, since we didn't do that above. */
24995 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24996 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24997 {
24998 if (!HASHITEM_EMPTY(hi))
24999 {
25000 --todo;
25001 v = HI2DI(hi);
25002 copy_tv(&v->di_tv, &v->di_tv);
25003 }
25004 }
25005
25006 /* Make a copy of the a:000 items, since we didn't do that above. */
25007 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25008 copy_tv(&li->li_tv, &li->li_tv);
25009 }
25010}
25011
25012/*
25013 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025014 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025015 */
25016 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025017can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025018{
25019 return (fc->l_varlist.lv_copyID != copyID
25020 && fc->l_vars.dv_copyID != copyID
25021 && fc->l_avars.dv_copyID != copyID);
25022}
25023
25024/*
25025 * Free "fc" and what it contains.
25026 */
25027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025028free_funccal(
25029 funccall_T *fc,
25030 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025031{
25032 listitem_T *li;
25033
25034 /* The a: variables typevals may not have been allocated, only free the
25035 * allocated variables. */
25036 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25037
25038 /* free all l: variables */
25039 vars_clear(&fc->l_vars.dv_hashtab);
25040
25041 /* Free the a:000 variables if they were allocated. */
25042 if (free_val)
25043 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25044 clear_tv(&li->li_tv);
25045
25046 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025047}
25048
25049/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025050 * Add a number variable "name" to dict "dp" with value "nr".
25051 */
25052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025053add_nr_var(
25054 dict_T *dp,
25055 dictitem_T *v,
25056 char *name,
25057 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025058{
25059 STRCPY(v->di_key, name);
25060 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25061 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25062 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025063 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025064 v->di_tv.vval.v_number = nr;
25065}
25066
25067/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025068 * ":return [expr]"
25069 */
25070 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025071ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025072{
25073 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025074 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025075 int returning = FALSE;
25076
25077 if (current_funccal == NULL)
25078 {
25079 EMSG(_("E133: :return not inside a function"));
25080 return;
25081 }
25082
25083 if (eap->skip)
25084 ++emsg_skip;
25085
25086 eap->nextcmd = NULL;
25087 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025088 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025089 {
25090 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025091 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025092 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025093 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025094 }
25095 /* It's safer to return also on error. */
25096 else if (!eap->skip)
25097 {
25098 /*
25099 * Return unless the expression evaluation has been cancelled due to an
25100 * aborting error, an interrupt, or an exception.
25101 */
25102 if (!aborting())
25103 returning = do_return(eap, FALSE, TRUE, NULL);
25104 }
25105
25106 /* When skipping or the return gets pending, advance to the next command
25107 * in this line (!returning). Otherwise, ignore the rest of the line.
25108 * Following lines will be ignored by get_func_line(). */
25109 if (returning)
25110 eap->nextcmd = NULL;
25111 else if (eap->nextcmd == NULL) /* no argument */
25112 eap->nextcmd = check_nextcmd(arg);
25113
25114 if (eap->skip)
25115 --emsg_skip;
25116}
25117
25118/*
25119 * Return from a function. Possibly makes the return pending. Also called
25120 * for a pending return at the ":endtry" or after returning from an extra
25121 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025122 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025123 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025124 * FALSE when the return gets pending.
25125 */
25126 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025127do_return(
25128 exarg_T *eap,
25129 int reanimate,
25130 int is_cmd,
25131 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025132{
25133 int idx;
25134 struct condstack *cstack = eap->cstack;
25135
25136 if (reanimate)
25137 /* Undo the return. */
25138 current_funccal->returned = FALSE;
25139
25140 /*
25141 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25142 * not in its finally clause (which then is to be executed next) is found.
25143 * In this case, make the ":return" pending for execution at the ":endtry".
25144 * Otherwise, return normally.
25145 */
25146 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25147 if (idx >= 0)
25148 {
25149 cstack->cs_pending[idx] = CSTP_RETURN;
25150
25151 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025152 /* A pending return again gets pending. "rettv" points to an
25153 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025154 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025155 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025156 else
25157 {
25158 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025159 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025160 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025161 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025162
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025163 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025164 {
25165 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025166 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025167 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025168 else
25169 EMSG(_(e_outofmem));
25170 }
25171 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025172 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025173
25174 if (reanimate)
25175 {
25176 /* The pending return value could be overwritten by a ":return"
25177 * without argument in a finally clause; reset the default
25178 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025179 current_funccal->rettv->v_type = VAR_NUMBER;
25180 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025181 }
25182 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025183 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025184 }
25185 else
25186 {
25187 current_funccal->returned = TRUE;
25188
25189 /* If the return is carried out now, store the return value. For
25190 * a return immediately after reanimation, the value is already
25191 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025192 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025193 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025194 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025195 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025196 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025197 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025198 }
25199 }
25200
25201 return idx < 0;
25202}
25203
25204/*
25205 * Free the variable with a pending return value.
25206 */
25207 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025208discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025209{
Bram Moolenaar33570922005-01-25 22:26:29 +000025210 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025211}
25212
25213/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025214 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025215 * is an allocated string. Used by report_pending() for verbose messages.
25216 */
25217 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025218get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025219{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025220 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025221 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025222 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025223
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025224 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025225 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025226 if (s == NULL)
25227 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025228
25229 STRCPY(IObuff, ":return ");
25230 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25231 if (STRLEN(s) + 8 >= IOSIZE)
25232 STRCPY(IObuff + IOSIZE - 4, "...");
25233 vim_free(tofree);
25234 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025235}
25236
25237/*
25238 * Get next function line.
25239 * Called by do_cmdline() to get the next line.
25240 * Returns allocated string, or NULL for end of function.
25241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025242 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025243get_func_line(
25244 int c UNUSED,
25245 void *cookie,
25246 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025247{
Bram Moolenaar33570922005-01-25 22:26:29 +000025248 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025249 ufunc_T *fp = fcp->func;
25250 char_u *retval;
25251 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025252
25253 /* If breakpoints have been added/deleted need to check for it. */
25254 if (fcp->dbg_tick != debug_tick)
25255 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025256 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025257 sourcing_lnum);
25258 fcp->dbg_tick = debug_tick;
25259 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025260#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025261 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025262 func_line_end(cookie);
25263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025264
Bram Moolenaar05159a02005-02-26 23:04:13 +000025265 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025266 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25267 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025268 retval = NULL;
25269 else
25270 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025271 /* Skip NULL lines (continuation lines). */
25272 while (fcp->linenr < gap->ga_len
25273 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25274 ++fcp->linenr;
25275 if (fcp->linenr >= gap->ga_len)
25276 retval = NULL;
25277 else
25278 {
25279 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25280 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025281#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025282 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025283 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025284#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025286 }
25287
25288 /* Did we encounter a breakpoint? */
25289 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25290 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025291 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025292 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025293 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025294 sourcing_lnum);
25295 fcp->dbg_tick = debug_tick;
25296 }
25297
25298 return retval;
25299}
25300
Bram Moolenaar05159a02005-02-26 23:04:13 +000025301#if defined(FEAT_PROFILE) || defined(PROTO)
25302/*
25303 * Called when starting to read a function line.
25304 * "sourcing_lnum" must be correct!
25305 * When skipping lines it may not actually be executed, but we won't find out
25306 * until later and we need to store the time now.
25307 */
25308 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025309func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025310{
25311 funccall_T *fcp = (funccall_T *)cookie;
25312 ufunc_T *fp = fcp->func;
25313
25314 if (fp->uf_profiling && sourcing_lnum >= 1
25315 && sourcing_lnum <= fp->uf_lines.ga_len)
25316 {
25317 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025318 /* Skip continuation lines. */
25319 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25320 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025321 fp->uf_tml_execed = FALSE;
25322 profile_start(&fp->uf_tml_start);
25323 profile_zero(&fp->uf_tml_children);
25324 profile_get_wait(&fp->uf_tml_wait);
25325 }
25326}
25327
25328/*
25329 * Called when actually executing a function line.
25330 */
25331 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025332func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025333{
25334 funccall_T *fcp = (funccall_T *)cookie;
25335 ufunc_T *fp = fcp->func;
25336
25337 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25338 fp->uf_tml_execed = TRUE;
25339}
25340
25341/*
25342 * Called when done with a function line.
25343 */
25344 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025345func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025346{
25347 funccall_T *fcp = (funccall_T *)cookie;
25348 ufunc_T *fp = fcp->func;
25349
25350 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25351 {
25352 if (fp->uf_tml_execed)
25353 {
25354 ++fp->uf_tml_count[fp->uf_tml_idx];
25355 profile_end(&fp->uf_tml_start);
25356 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025357 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025358 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25359 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025360 }
25361 fp->uf_tml_idx = -1;
25362 }
25363}
25364#endif
25365
Bram Moolenaar071d4272004-06-13 20:20:40 +000025366/*
25367 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025368 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025369 */
25370 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025371func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025372{
Bram Moolenaar33570922005-01-25 22:26:29 +000025373 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025374
25375 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25376 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025377 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025378 || fcp->returned);
25379}
25380
25381/*
25382 * return TRUE if cookie indicates a function which "abort"s on errors.
25383 */
25384 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025385func_has_abort(
25386 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025387{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025388 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025389}
25390
25391#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25392typedef enum
25393{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025394 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25395 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25396 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025397} var_flavour_T;
25398
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025399static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025400
25401 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025402var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025403{
25404 char_u *p = varname;
25405
25406 if (ASCII_ISUPPER(*p))
25407 {
25408 while (*(++p))
25409 if (ASCII_ISLOWER(*p))
25410 return VAR_FLAVOUR_SESSION;
25411 return VAR_FLAVOUR_VIMINFO;
25412 }
25413 else
25414 return VAR_FLAVOUR_DEFAULT;
25415}
25416#endif
25417
25418#if defined(FEAT_VIMINFO) || defined(PROTO)
25419/*
25420 * Restore global vars that start with a capital from the viminfo file
25421 */
25422 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025423read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025424{
25425 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025426 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025427 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025428 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025429
25430 if (!writing && (find_viminfo_parameter('!') != NULL))
25431 {
25432 tab = vim_strchr(virp->vir_line + 1, '\t');
25433 if (tab != NULL)
25434 {
25435 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025436 switch (*tab)
25437 {
25438 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025439#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025440 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025441#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025442 case 'D': type = VAR_DICT; break;
25443 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025444 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025446
25447 tab = vim_strchr(tab, '\t');
25448 if (tab != NULL)
25449 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025450 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025451 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025452 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025453 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025454#ifdef FEAT_FLOAT
25455 else if (type == VAR_FLOAT)
25456 (void)string2float(tab + 1, &tv.vval.v_float);
25457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025458 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025459 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025460 if (type == VAR_DICT || type == VAR_LIST)
25461 {
25462 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25463
25464 if (etv == NULL)
25465 /* Failed to parse back the dict or list, use it as a
25466 * string. */
25467 tv.v_type = VAR_STRING;
25468 else
25469 {
25470 vim_free(tv.vval.v_string);
25471 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025472 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025473 }
25474 }
25475
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025476 /* when in a function use global variables */
25477 save_funccal = current_funccal;
25478 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025479 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025480 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025481
25482 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025483 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025484 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25485 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025486 }
25487 }
25488 }
25489
25490 return viminfo_readline(virp);
25491}
25492
25493/*
25494 * Write global vars that start with a capital to the viminfo file
25495 */
25496 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025497write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025498{
Bram Moolenaar33570922005-01-25 22:26:29 +000025499 hashitem_T *hi;
25500 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025501 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025502 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025503 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025504 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025505 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025506
25507 if (find_viminfo_parameter('!') == NULL)
25508 return;
25509
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025510 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025511
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025512 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025513 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025514 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025515 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025516 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025517 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025518 this_var = HI2DI(hi);
25519 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025520 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025521 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025522 {
25523 case VAR_STRING: s = "STR"; break;
25524 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025525 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025526 case VAR_DICT: s = "DIC"; break;
25527 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025528 case VAR_SPECIAL: s = "XPL"; break;
25529
25530 case VAR_UNKNOWN:
25531 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025532 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025533 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025534 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025535 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025536 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025537 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025538 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025539 if (p != NULL)
25540 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025541 vim_free(tofree);
25542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025543 }
25544 }
25545}
25546#endif
25547
25548#if defined(FEAT_SESSION) || defined(PROTO)
25549 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025550store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025551{
Bram Moolenaar33570922005-01-25 22:26:29 +000025552 hashitem_T *hi;
25553 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025554 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025555 char_u *p, *t;
25556
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025557 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025558 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025559 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025560 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025561 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025562 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025563 this_var = HI2DI(hi);
25564 if ((this_var->di_tv.v_type == VAR_NUMBER
25565 || this_var->di_tv.v_type == VAR_STRING)
25566 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025567 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025568 /* Escape special characters with a backslash. Turn a LF and
25569 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025570 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025571 (char_u *)"\\\"\n\r");
25572 if (p == NULL) /* out of memory */
25573 break;
25574 for (t = p; *t != NUL; ++t)
25575 if (*t == '\n')
25576 *t = 'n';
25577 else if (*t == '\r')
25578 *t = 'r';
25579 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025580 this_var->di_key,
25581 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25582 : ' ',
25583 p,
25584 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25585 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025586 || put_eol(fd) == FAIL)
25587 {
25588 vim_free(p);
25589 return FAIL;
25590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025591 vim_free(p);
25592 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025593#ifdef FEAT_FLOAT
25594 else if (this_var->di_tv.v_type == VAR_FLOAT
25595 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25596 {
25597 float_T f = this_var->di_tv.vval.v_float;
25598 int sign = ' ';
25599
25600 if (f < 0)
25601 {
25602 f = -f;
25603 sign = '-';
25604 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025605 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025606 this_var->di_key, sign, f) < 0)
25607 || put_eol(fd) == FAIL)
25608 return FAIL;
25609 }
25610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025611 }
25612 }
25613 return OK;
25614}
25615#endif
25616
Bram Moolenaar661b1822005-07-28 22:36:45 +000025617/*
25618 * Display script name where an item was last set.
25619 * Should only be invoked when 'verbose' is non-zero.
25620 */
25621 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025622last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025623{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025624 char_u *p;
25625
Bram Moolenaar661b1822005-07-28 22:36:45 +000025626 if (scriptID != 0)
25627 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025628 p = home_replace_save(NULL, get_scriptname(scriptID));
25629 if (p != NULL)
25630 {
25631 verbose_enter();
25632 MSG_PUTS(_("\n\tLast set from "));
25633 MSG_PUTS(p);
25634 vim_free(p);
25635 verbose_leave();
25636 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025637 }
25638}
25639
Bram Moolenaard812df62008-11-09 12:46:09 +000025640/*
25641 * List v:oldfiles in a nice way.
25642 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025643 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025644ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025645{
25646 list_T *l = vimvars[VV_OLDFILES].vv_list;
25647 listitem_T *li;
25648 int nr = 0;
25649
25650 if (l == NULL)
25651 msg((char_u *)_("No old files"));
25652 else
25653 {
25654 msg_start();
25655 msg_scroll = TRUE;
25656 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25657 {
25658 msg_outnum((long)++nr);
25659 MSG_PUTS(": ");
25660 msg_outtrans(get_tv_string(&li->li_tv));
25661 msg_putchar('\n');
25662 out_flush(); /* output one line at a time */
25663 ui_breakcheck();
25664 }
25665 /* Assume "got_int" was set to truncate the listing. */
25666 got_int = FALSE;
25667
25668#ifdef FEAT_BROWSE_CMD
25669 if (cmdmod.browse)
25670 {
25671 quit_more = FALSE;
25672 nr = prompt_for_number(FALSE);
25673 msg_starthere();
25674 if (nr > 0)
25675 {
25676 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25677 (long)nr);
25678
25679 if (p != NULL)
25680 {
25681 p = expand_env_save(p);
25682 eap->arg = p;
25683 eap->cmdidx = CMD_edit;
25684 cmdmod.browse = FALSE;
25685 do_exedit(eap, NULL);
25686 vim_free(p);
25687 }
25688 }
25689 }
25690#endif
25691 }
25692}
25693
Bram Moolenaar53744302015-07-17 17:38:22 +020025694/* reset v:option_new, v:option_old and v:option_type */
25695 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025696reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025697{
25698 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25699 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25700 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25701}
25702
25703
Bram Moolenaar071d4272004-06-13 20:20:40 +000025704#endif /* FEAT_EVAL */
25705
Bram Moolenaar071d4272004-06-13 20:20:40 +000025706
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025707#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025708
25709#ifdef WIN3264
25710/*
25711 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25712 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025713static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25714static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25715static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025716
25717/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025718 * Get the short path (8.3) for the filename in "fnamep".
25719 * Only works for a valid file name.
25720 * When the path gets longer "fnamep" is changed and the allocated buffer
25721 * is put in "bufp".
25722 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25723 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025724 */
25725 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025726get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025727{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025728 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025729 char_u *newbuf;
25730
25731 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025732 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025733 if (l > len - 1)
25734 {
25735 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025736 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025737 newbuf = vim_strnsave(*fnamep, l);
25738 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025739 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025740
25741 vim_free(*bufp);
25742 *fnamep = *bufp = newbuf;
25743
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025744 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025745 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025746 }
25747
25748 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025749 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025750}
25751
25752/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025753 * Get the short path (8.3) for the filename in "fname". The converted
25754 * path is returned in "bufp".
25755 *
25756 * Some of the directories specified in "fname" may not exist. This function
25757 * will shorten the existing directories at the beginning of the path and then
25758 * append the remaining non-existing path.
25759 *
25760 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025761 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025762 * bufp - Pointer to an allocated buffer for the filename.
25763 * fnamelen - Length of the filename pointed to by fname
25764 *
25765 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025766 */
25767 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025768shortpath_for_invalid_fname(
25769 char_u **fname,
25770 char_u **bufp,
25771 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025772{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025773 char_u *short_fname, *save_fname, *pbuf_unused;
25774 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025775 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025776 int old_len, len;
25777 int new_len, sfx_len;
25778 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025779
25780 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025781 old_len = *fnamelen;
25782 save_fname = vim_strnsave(*fname, old_len);
25783 pbuf_unused = NULL;
25784 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025785
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025786 endp = save_fname + old_len - 1; /* Find the end of the copy */
25787 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025788
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025789 /*
25790 * Try shortening the supplied path till it succeeds by removing one
25791 * directory at a time from the tail of the path.
25792 */
25793 len = 0;
25794 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025795 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025796 /* go back one path-separator */
25797 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25798 --endp;
25799 if (endp <= save_fname)
25800 break; /* processed the complete path */
25801
25802 /*
25803 * Replace the path separator with a NUL and try to shorten the
25804 * resulting path.
25805 */
25806 ch = *endp;
25807 *endp = 0;
25808 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025809 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025810 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25811 {
25812 retval = FAIL;
25813 goto theend;
25814 }
25815 *endp = ch; /* preserve the string */
25816
25817 if (len > 0)
25818 break; /* successfully shortened the path */
25819
25820 /* failed to shorten the path. Skip the path separator */
25821 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025822 }
25823
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025824 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025825 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025826 /*
25827 * Succeeded in shortening the path. Now concatenate the shortened
25828 * path with the remaining path at the tail.
25829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025830
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025831 /* Compute the length of the new path. */
25832 sfx_len = (int)(save_endp - endp) + 1;
25833 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025834
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025835 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025836 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025837 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025838 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025839 /* There is not enough space in the currently allocated string,
25840 * copy it to a buffer big enough. */
25841 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025842 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025843 {
25844 retval = FAIL;
25845 goto theend;
25846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025847 }
25848 else
25849 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025850 /* Transfer short_fname to the main buffer (it's big enough),
25851 * unless get_short_pathname() did its work in-place. */
25852 *fname = *bufp = save_fname;
25853 if (short_fname != save_fname)
25854 vim_strncpy(save_fname, short_fname, len);
25855 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025856 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025857
25858 /* concat the not-shortened part of the path */
25859 vim_strncpy(*fname + len, endp, sfx_len);
25860 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025861 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025862
25863theend:
25864 vim_free(pbuf_unused);
25865 vim_free(save_fname);
25866
25867 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025868}
25869
25870/*
25871 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025872 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025873 */
25874 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025875shortpath_for_partial(
25876 char_u **fnamep,
25877 char_u **bufp,
25878 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025879{
25880 int sepcount, len, tflen;
25881 char_u *p;
25882 char_u *pbuf, *tfname;
25883 int hasTilde;
25884
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025885 /* Count up the path separators from the RHS.. so we know which part
25886 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025887 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025888 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025889 if (vim_ispathsep(*p))
25890 ++sepcount;
25891
25892 /* Need full path first (use expand_env() to remove a "~/") */
25893 hasTilde = (**fnamep == '~');
25894 if (hasTilde)
25895 pbuf = tfname = expand_env_save(*fnamep);
25896 else
25897 pbuf = tfname = FullName_save(*fnamep, FALSE);
25898
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025899 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025900
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025901 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25902 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025903
25904 if (len == 0)
25905 {
25906 /* Don't have a valid filename, so shorten the rest of the
25907 * path if we can. This CAN give us invalid 8.3 filenames, but
25908 * there's not a lot of point in guessing what it might be.
25909 */
25910 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025911 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25912 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025913 }
25914
25915 /* Count the paths backward to find the beginning of the desired string. */
25916 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025917 {
25918#ifdef FEAT_MBYTE
25919 if (has_mbyte)
25920 p -= mb_head_off(tfname, p);
25921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025922 if (vim_ispathsep(*p))
25923 {
25924 if (sepcount == 0 || (hasTilde && sepcount == 1))
25925 break;
25926 else
25927 sepcount --;
25928 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025930 if (hasTilde)
25931 {
25932 --p;
25933 if (p >= tfname)
25934 *p = '~';
25935 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025936 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025937 }
25938 else
25939 ++p;
25940
25941 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25942 vim_free(*bufp);
25943 *fnamelen = (int)STRLEN(p);
25944 *bufp = pbuf;
25945 *fnamep = p;
25946
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025947 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025948}
25949#endif /* WIN3264 */
25950
25951/*
25952 * Adjust a filename, according to a string of modifiers.
25953 * *fnamep must be NUL terminated when called. When returning, the length is
25954 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025955 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025956 * When there is an error, *fnamep is set to NULL.
25957 */
25958 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025959modify_fname(
25960 char_u *src, /* string with modifiers */
25961 int *usedlen, /* characters after src that are used */
25962 char_u **fnamep, /* file name so far */
25963 char_u **bufp, /* buffer for allocated file name or NULL */
25964 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025965{
25966 int valid = 0;
25967 char_u *tail;
25968 char_u *s, *p, *pbuf;
25969 char_u dirname[MAXPATHL];
25970 int c;
25971 int has_fullname = 0;
25972#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025973 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025974 int has_shortname = 0;
25975#endif
25976
25977repeat:
25978 /* ":p" - full path/file_name */
25979 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25980 {
25981 has_fullname = 1;
25982
25983 valid |= VALID_PATH;
25984 *usedlen += 2;
25985
25986 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25987 if ((*fnamep)[0] == '~'
25988#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25989 && ((*fnamep)[1] == '/'
25990# ifdef BACKSLASH_IN_FILENAME
25991 || (*fnamep)[1] == '\\'
25992# endif
25993 || (*fnamep)[1] == NUL)
25994
25995#endif
25996 )
25997 {
25998 *fnamep = expand_env_save(*fnamep);
25999 vim_free(*bufp); /* free any allocated file name */
26000 *bufp = *fnamep;
26001 if (*fnamep == NULL)
26002 return -1;
26003 }
26004
26005 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026006 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026007 {
26008 if (vim_ispathsep(*p)
26009 && p[1] == '.'
26010 && (p[2] == NUL
26011 || vim_ispathsep(p[2])
26012 || (p[2] == '.'
26013 && (p[3] == NUL || vim_ispathsep(p[3])))))
26014 break;
26015 }
26016
26017 /* FullName_save() is slow, don't use it when not needed. */
26018 if (*p != NUL || !vim_isAbsName(*fnamep))
26019 {
26020 *fnamep = FullName_save(*fnamep, *p != NUL);
26021 vim_free(*bufp); /* free any allocated file name */
26022 *bufp = *fnamep;
26023 if (*fnamep == NULL)
26024 return -1;
26025 }
26026
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026027#ifdef WIN3264
26028# if _WIN32_WINNT >= 0x0500
26029 if (vim_strchr(*fnamep, '~') != NULL)
26030 {
26031 /* Expand 8.3 filename to full path. Needed to make sure the same
26032 * file does not have two different names.
26033 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26034 p = alloc(_MAX_PATH + 1);
26035 if (p != NULL)
26036 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026037 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026038 {
26039 vim_free(*bufp);
26040 *bufp = *fnamep = p;
26041 }
26042 else
26043 vim_free(p);
26044 }
26045 }
26046# endif
26047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026048 /* Append a path separator to a directory. */
26049 if (mch_isdir(*fnamep))
26050 {
26051 /* Make room for one or two extra characters. */
26052 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26053 vim_free(*bufp); /* free any allocated file name */
26054 *bufp = *fnamep;
26055 if (*fnamep == NULL)
26056 return -1;
26057 add_pathsep(*fnamep);
26058 }
26059 }
26060
26061 /* ":." - path relative to the current directory */
26062 /* ":~" - path relative to the home directory */
26063 /* ":8" - shortname path - postponed till after */
26064 while (src[*usedlen] == ':'
26065 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26066 {
26067 *usedlen += 2;
26068 if (c == '8')
26069 {
26070#ifdef WIN3264
26071 has_shortname = 1; /* Postpone this. */
26072#endif
26073 continue;
26074 }
26075 pbuf = NULL;
26076 /* Need full path first (use expand_env() to remove a "~/") */
26077 if (!has_fullname)
26078 {
26079 if (c == '.' && **fnamep == '~')
26080 p = pbuf = expand_env_save(*fnamep);
26081 else
26082 p = pbuf = FullName_save(*fnamep, FALSE);
26083 }
26084 else
26085 p = *fnamep;
26086
26087 has_fullname = 0;
26088
26089 if (p != NULL)
26090 {
26091 if (c == '.')
26092 {
26093 mch_dirname(dirname, MAXPATHL);
26094 s = shorten_fname(p, dirname);
26095 if (s != NULL)
26096 {
26097 *fnamep = s;
26098 if (pbuf != NULL)
26099 {
26100 vim_free(*bufp); /* free any allocated file name */
26101 *bufp = pbuf;
26102 pbuf = NULL;
26103 }
26104 }
26105 }
26106 else
26107 {
26108 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26109 /* Only replace it when it starts with '~' */
26110 if (*dirname == '~')
26111 {
26112 s = vim_strsave(dirname);
26113 if (s != NULL)
26114 {
26115 *fnamep = s;
26116 vim_free(*bufp);
26117 *bufp = s;
26118 }
26119 }
26120 }
26121 vim_free(pbuf);
26122 }
26123 }
26124
26125 tail = gettail(*fnamep);
26126 *fnamelen = (int)STRLEN(*fnamep);
26127
26128 /* ":h" - head, remove "/file_name", can be repeated */
26129 /* Don't remove the first "/" or "c:\" */
26130 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26131 {
26132 valid |= VALID_HEAD;
26133 *usedlen += 2;
26134 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026135 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026136 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026137 *fnamelen = (int)(tail - *fnamep);
26138#ifdef VMS
26139 if (*fnamelen > 0)
26140 *fnamelen += 1; /* the path separator is part of the path */
26141#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026142 if (*fnamelen == 0)
26143 {
26144 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26145 p = vim_strsave((char_u *)".");
26146 if (p == NULL)
26147 return -1;
26148 vim_free(*bufp);
26149 *bufp = *fnamep = tail = p;
26150 *fnamelen = 1;
26151 }
26152 else
26153 {
26154 while (tail > s && !after_pathsep(s, tail))
26155 mb_ptr_back(*fnamep, tail);
26156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026157 }
26158
26159 /* ":8" - shortname */
26160 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26161 {
26162 *usedlen += 2;
26163#ifdef WIN3264
26164 has_shortname = 1;
26165#endif
26166 }
26167
26168#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026169 /*
26170 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026171 */
26172 if (has_shortname)
26173 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026174 /* Copy the string if it is shortened by :h and when it wasn't copied
26175 * yet, because we are going to change it in place. Avoids changing
26176 * the buffer name for "%:8". */
26177 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026178 {
26179 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026180 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026181 return -1;
26182 vim_free(*bufp);
26183 *bufp = *fnamep = p;
26184 }
26185
26186 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026187 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026188 if (!has_fullname && !vim_isAbsName(*fnamep))
26189 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026190 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026191 return -1;
26192 }
26193 else
26194 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026195 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026196
Bram Moolenaardc935552011-08-17 15:23:23 +020026197 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026198 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026199 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026200 return -1;
26201
26202 if (l == 0)
26203 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026204 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026205 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026206 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026207 return -1;
26208 }
26209 *fnamelen = l;
26210 }
26211 }
26212#endif /* WIN3264 */
26213
26214 /* ":t" - tail, just the basename */
26215 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26216 {
26217 *usedlen += 2;
26218 *fnamelen -= (int)(tail - *fnamep);
26219 *fnamep = tail;
26220 }
26221
26222 /* ":e" - extension, can be repeated */
26223 /* ":r" - root, without extension, can be repeated */
26224 while (src[*usedlen] == ':'
26225 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26226 {
26227 /* find a '.' in the tail:
26228 * - for second :e: before the current fname
26229 * - otherwise: The last '.'
26230 */
26231 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26232 s = *fnamep - 2;
26233 else
26234 s = *fnamep + *fnamelen - 1;
26235 for ( ; s > tail; --s)
26236 if (s[0] == '.')
26237 break;
26238 if (src[*usedlen + 1] == 'e') /* :e */
26239 {
26240 if (s > tail)
26241 {
26242 *fnamelen += (int)(*fnamep - (s + 1));
26243 *fnamep = s + 1;
26244#ifdef VMS
26245 /* cut version from the extension */
26246 s = *fnamep + *fnamelen - 1;
26247 for ( ; s > *fnamep; --s)
26248 if (s[0] == ';')
26249 break;
26250 if (s > *fnamep)
26251 *fnamelen = s - *fnamep;
26252#endif
26253 }
26254 else if (*fnamep <= tail)
26255 *fnamelen = 0;
26256 }
26257 else /* :r */
26258 {
26259 if (s > tail) /* remove one extension */
26260 *fnamelen = (int)(s - *fnamep);
26261 }
26262 *usedlen += 2;
26263 }
26264
26265 /* ":s?pat?foo?" - substitute */
26266 /* ":gs?pat?foo?" - global substitute */
26267 if (src[*usedlen] == ':'
26268 && (src[*usedlen + 1] == 's'
26269 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26270 {
26271 char_u *str;
26272 char_u *pat;
26273 char_u *sub;
26274 int sep;
26275 char_u *flags;
26276 int didit = FALSE;
26277
26278 flags = (char_u *)"";
26279 s = src + *usedlen + 2;
26280 if (src[*usedlen + 1] == 'g')
26281 {
26282 flags = (char_u *)"g";
26283 ++s;
26284 }
26285
26286 sep = *s++;
26287 if (sep)
26288 {
26289 /* find end of pattern */
26290 p = vim_strchr(s, sep);
26291 if (p != NULL)
26292 {
26293 pat = vim_strnsave(s, (int)(p - s));
26294 if (pat != NULL)
26295 {
26296 s = p + 1;
26297 /* find end of substitution */
26298 p = vim_strchr(s, sep);
26299 if (p != NULL)
26300 {
26301 sub = vim_strnsave(s, (int)(p - s));
26302 str = vim_strnsave(*fnamep, *fnamelen);
26303 if (sub != NULL && str != NULL)
26304 {
26305 *usedlen = (int)(p + 1 - src);
26306 s = do_string_sub(str, pat, sub, flags);
26307 if (s != NULL)
26308 {
26309 *fnamep = s;
26310 *fnamelen = (int)STRLEN(s);
26311 vim_free(*bufp);
26312 *bufp = s;
26313 didit = TRUE;
26314 }
26315 }
26316 vim_free(sub);
26317 vim_free(str);
26318 }
26319 vim_free(pat);
26320 }
26321 }
26322 /* after using ":s", repeat all the modifiers */
26323 if (didit)
26324 goto repeat;
26325 }
26326 }
26327
Bram Moolenaar26df0922014-02-23 23:39:13 +010026328 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26329 {
26330 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26331 if (p == NULL)
26332 return -1;
26333 vim_free(*bufp);
26334 *bufp = *fnamep = p;
26335 *fnamelen = (int)STRLEN(p);
26336 *usedlen += 2;
26337 }
26338
Bram Moolenaar071d4272004-06-13 20:20:40 +000026339 return valid;
26340}
26341
26342/*
26343 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26344 * "flags" can be "g" to do a global substitute.
26345 * Returns an allocated string, NULL for error.
26346 */
26347 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026348do_string_sub(
26349 char_u *str,
26350 char_u *pat,
26351 char_u *sub,
26352 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026353{
26354 int sublen;
26355 regmatch_T regmatch;
26356 int i;
26357 int do_all;
26358 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026359 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026360 garray_T ga;
26361 char_u *ret;
26362 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026363 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026364
26365 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26366 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026367 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026368
26369 ga_init2(&ga, 1, 200);
26370
26371 do_all = (flags[0] == 'g');
26372
26373 regmatch.rm_ic = p_ic;
26374 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26375 if (regmatch.regprog != NULL)
26376 {
26377 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026378 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026379 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26380 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026381 /* Skip empty match except for first match. */
26382 if (regmatch.startp[0] == regmatch.endp[0])
26383 {
26384 if (zero_width == regmatch.startp[0])
26385 {
26386 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026387 i = MB_PTR2LEN(tail);
26388 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26389 (size_t)i);
26390 ga.ga_len += i;
26391 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026392 continue;
26393 }
26394 zero_width = regmatch.startp[0];
26395 }
26396
Bram Moolenaar071d4272004-06-13 20:20:40 +000026397 /*
26398 * Get some space for a temporary buffer to do the substitution
26399 * into. It will contain:
26400 * - The text up to where the match is.
26401 * - The substituted text.
26402 * - The text after the match.
26403 */
26404 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026405 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026406 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26407 {
26408 ga_clear(&ga);
26409 break;
26410 }
26411
26412 /* copy the text up to where the match is */
26413 i = (int)(regmatch.startp[0] - tail);
26414 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26415 /* add the substituted text */
26416 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26417 + ga.ga_len + i, TRUE, TRUE, FALSE);
26418 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026419 tail = regmatch.endp[0];
26420 if (*tail == NUL)
26421 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026422 if (!do_all)
26423 break;
26424 }
26425
26426 if (ga.ga_data != NULL)
26427 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26428
Bram Moolenaar473de612013-06-08 18:19:48 +020026429 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026430 }
26431
26432 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26433 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026434 if (p_cpo == empty_option)
26435 p_cpo = save_cpo;
26436 else
26437 /* Darn, evaluating {sub} expression changed the value. */
26438 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026439
26440 return ret;
26441}
26442
26443#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */