blob: 4afceccfaf12c9f0fbdd29c9a49cde8f6c6d344a [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);
437static int list_append_number(list_T *l, varnumber_T n);
438static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
439static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
440static list_T *list_copy(list_T *orig, int deep, int copyID);
441static char_u *list2string(typval_T *tv, int copyID);
442static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
443static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
444static int free_unref_items(int copyID);
445static dictitem_T *dictitem_copy(dictitem_T *org);
446static void dictitem_remove(dict_T *dict, dictitem_T *item);
447static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
448static long dict_len(dict_T *d);
449static char_u *dict2string(typval_T *tv, int copyID);
450static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
451static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
452static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
453static char_u *string_quote(char_u *str, int function);
454static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
455static int find_internal_func(char_u *name);
456static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload);
457static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100458static void emsg_funcname(char *ermsg, char_u *name);
459static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000460
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000461#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100462static void f_abs(typval_T *argvars, typval_T *rettv);
463static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100465static void f_add(typval_T *argvars, typval_T *rettv);
466static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
467static void f_and(typval_T *argvars, typval_T *rettv);
468static void f_append(typval_T *argvars, typval_T *rettv);
469static void f_argc(typval_T *argvars, typval_T *rettv);
470static void f_argidx(typval_T *argvars, typval_T *rettv);
471static void f_arglistid(typval_T *argvars, typval_T *rettv);
472static void f_argv(typval_T *argvars, typval_T *rettv);
473static void f_assert_equal(typval_T *argvars, typval_T *rettv);
474static void f_assert_exception(typval_T *argvars, typval_T *rettv);
475static void f_assert_fails(typval_T *argvars, typval_T *rettv);
476static void f_assert_false(typval_T *argvars, typval_T *rettv);
477static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000478#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100479static void f_asin(typval_T *argvars, typval_T *rettv);
480static void f_atan(typval_T *argvars, typval_T *rettv);
481static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000482#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100483static void f_browse(typval_T *argvars, typval_T *rettv);
484static void f_browsedir(typval_T *argvars, typval_T *rettv);
485static void f_bufexists(typval_T *argvars, typval_T *rettv);
486static void f_buflisted(typval_T *argvars, typval_T *rettv);
487static void f_bufloaded(typval_T *argvars, typval_T *rettv);
488static void f_bufname(typval_T *argvars, typval_T *rettv);
489static void f_bufnr(typval_T *argvars, typval_T *rettv);
490static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
491static void f_byte2line(typval_T *argvars, typval_T *rettv);
492static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
493static void f_byteidx(typval_T *argvars, typval_T *rettv);
494static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
495static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000496#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100497static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000498#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100499#ifdef FEAT_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100500static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100501static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
502static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100503static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100504# ifdef FEAT_JOB
505static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
506# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100507static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100508static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
509static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100510static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100511static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100512static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
513static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100514static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100515static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100516#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100517static void f_changenr(typval_T *argvars, typval_T *rettv);
518static void f_char2nr(typval_T *argvars, typval_T *rettv);
519static void f_cindent(typval_T *argvars, typval_T *rettv);
520static void f_clearmatches(typval_T *argvars, typval_T *rettv);
521static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000522#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100523static void f_complete(typval_T *argvars, typval_T *rettv);
524static void f_complete_add(typval_T *argvars, typval_T *rettv);
525static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000526#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100527static void f_confirm(typval_T *argvars, typval_T *rettv);
528static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000529#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100530static void f_cos(typval_T *argvars, typval_T *rettv);
531static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000532#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100533static void f_count(typval_T *argvars, typval_T *rettv);
534static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
535static void f_cursor(typval_T *argsvars, typval_T *rettv);
536static void f_deepcopy(typval_T *argvars, typval_T *rettv);
537static void f_delete(typval_T *argvars, typval_T *rettv);
538static void f_did_filetype(typval_T *argvars, typval_T *rettv);
539static void f_diff_filler(typval_T *argvars, typval_T *rettv);
540static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100541static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100542static void f_empty(typval_T *argvars, typval_T *rettv);
543static void f_escape(typval_T *argvars, typval_T *rettv);
544static void f_eval(typval_T *argvars, typval_T *rettv);
545static void f_eventhandler(typval_T *argvars, typval_T *rettv);
546static void f_executable(typval_T *argvars, typval_T *rettv);
547static void f_exepath(typval_T *argvars, typval_T *rettv);
548static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200549#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100550static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200551#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100552static void f_expand(typval_T *argvars, typval_T *rettv);
553static void f_extend(typval_T *argvars, typval_T *rettv);
554static void f_feedkeys(typval_T *argvars, typval_T *rettv);
555static void f_filereadable(typval_T *argvars, typval_T *rettv);
556static void f_filewritable(typval_T *argvars, typval_T *rettv);
557static void f_filter(typval_T *argvars, typval_T *rettv);
558static void f_finddir(typval_T *argvars, typval_T *rettv);
559static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000560#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100561static void f_float2nr(typval_T *argvars, typval_T *rettv);
562static void f_floor(typval_T *argvars, typval_T *rettv);
563static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000564#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100565static void f_fnameescape(typval_T *argvars, typval_T *rettv);
566static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
567static void f_foldclosed(typval_T *argvars, typval_T *rettv);
568static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
569static void f_foldlevel(typval_T *argvars, typval_T *rettv);
570static void f_foldtext(typval_T *argvars, typval_T *rettv);
571static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
572static void f_foreground(typval_T *argvars, typval_T *rettv);
573static void f_function(typval_T *argvars, typval_T *rettv);
574static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
575static void f_get(typval_T *argvars, typval_T *rettv);
576static void f_getbufline(typval_T *argvars, typval_T *rettv);
577static void f_getbufvar(typval_T *argvars, typval_T *rettv);
578static void f_getchar(typval_T *argvars, typval_T *rettv);
579static void f_getcharmod(typval_T *argvars, typval_T *rettv);
580static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
581static void f_getcmdline(typval_T *argvars, typval_T *rettv);
582static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
583static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
584static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
585static void f_getcwd(typval_T *argvars, typval_T *rettv);
586static void f_getfontname(typval_T *argvars, typval_T *rettv);
587static void f_getfperm(typval_T *argvars, typval_T *rettv);
588static void f_getfsize(typval_T *argvars, typval_T *rettv);
589static void f_getftime(typval_T *argvars, typval_T *rettv);
590static void f_getftype(typval_T *argvars, typval_T *rettv);
591static void f_getline(typval_T *argvars, typval_T *rettv);
592static void f_getmatches(typval_T *argvars, typval_T *rettv);
593static void f_getpid(typval_T *argvars, typval_T *rettv);
594static void f_getcurpos(typval_T *argvars, typval_T *rettv);
595static void f_getpos(typval_T *argvars, typval_T *rettv);
596static void f_getqflist(typval_T *argvars, typval_T *rettv);
597static void f_getreg(typval_T *argvars, typval_T *rettv);
598static void f_getregtype(typval_T *argvars, typval_T *rettv);
599static void f_gettabvar(typval_T *argvars, typval_T *rettv);
600static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
601static void f_getwinposx(typval_T *argvars, typval_T *rettv);
602static void f_getwinposy(typval_T *argvars, typval_T *rettv);
603static void f_getwinvar(typval_T *argvars, typval_T *rettv);
604static void f_glob(typval_T *argvars, typval_T *rettv);
605static void f_globpath(typval_T *argvars, typval_T *rettv);
606static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
607static void f_has(typval_T *argvars, typval_T *rettv);
608static void f_has_key(typval_T *argvars, typval_T *rettv);
609static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
610static void f_hasmapto(typval_T *argvars, typval_T *rettv);
611static void f_histadd(typval_T *argvars, typval_T *rettv);
612static void f_histdel(typval_T *argvars, typval_T *rettv);
613static void f_histget(typval_T *argvars, typval_T *rettv);
614static void f_histnr(typval_T *argvars, typval_T *rettv);
615static void f_hlID(typval_T *argvars, typval_T *rettv);
616static void f_hlexists(typval_T *argvars, typval_T *rettv);
617static void f_hostname(typval_T *argvars, typval_T *rettv);
618static void f_iconv(typval_T *argvars, typval_T *rettv);
619static void f_indent(typval_T *argvars, typval_T *rettv);
620static void f_index(typval_T *argvars, typval_T *rettv);
621static void f_input(typval_T *argvars, typval_T *rettv);
622static void f_inputdialog(typval_T *argvars, typval_T *rettv);
623static void f_inputlist(typval_T *argvars, typval_T *rettv);
624static void f_inputrestore(typval_T *argvars, typval_T *rettv);
625static void f_inputsave(typval_T *argvars, typval_T *rettv);
626static void f_inputsecret(typval_T *argvars, typval_T *rettv);
627static void f_insert(typval_T *argvars, typval_T *rettv);
628static void f_invert(typval_T *argvars, typval_T *rettv);
629static void f_isdirectory(typval_T *argvars, typval_T *rettv);
630static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100631#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
632static void f_isnan(typval_T *argvars, typval_T *rettv);
633#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100634static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100635#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100636# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100637static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100638# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +0100639static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100640static void f_job_start(typval_T *argvars, typval_T *rettv);
641static void f_job_stop(typval_T *argvars, typval_T *rettv);
642static void f_job_status(typval_T *argvars, typval_T *rettv);
643#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100644static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100645static void f_js_decode(typval_T *argvars, typval_T *rettv);
646static void f_js_encode(typval_T *argvars, typval_T *rettv);
647static void f_json_decode(typval_T *argvars, typval_T *rettv);
648static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100649static void f_keys(typval_T *argvars, typval_T *rettv);
650static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
651static void f_len(typval_T *argvars, typval_T *rettv);
652static void f_libcall(typval_T *argvars, typval_T *rettv);
653static void f_libcallnr(typval_T *argvars, typval_T *rettv);
654static void f_line(typval_T *argvars, typval_T *rettv);
655static void f_line2byte(typval_T *argvars, typval_T *rettv);
656static void f_lispindent(typval_T *argvars, typval_T *rettv);
657static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000658#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100659static void f_log(typval_T *argvars, typval_T *rettv);
660static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000661#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200662#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100663static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200664#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100665static void f_map(typval_T *argvars, typval_T *rettv);
666static void f_maparg(typval_T *argvars, typval_T *rettv);
667static void f_mapcheck(typval_T *argvars, typval_T *rettv);
668static void f_match(typval_T *argvars, typval_T *rettv);
669static void f_matchadd(typval_T *argvars, typval_T *rettv);
670static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
671static void f_matcharg(typval_T *argvars, typval_T *rettv);
672static void f_matchdelete(typval_T *argvars, typval_T *rettv);
673static void f_matchend(typval_T *argvars, typval_T *rettv);
674static void f_matchlist(typval_T *argvars, typval_T *rettv);
675static void f_matchstr(typval_T *argvars, typval_T *rettv);
676static void f_max(typval_T *argvars, typval_T *rettv);
677static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000678#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100679static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000680#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100681static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100682#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100683static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100684#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100685static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
686static void f_nr2char(typval_T *argvars, typval_T *rettv);
687static void f_or(typval_T *argvars, typval_T *rettv);
688static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100689#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100691#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000692#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
696static void f_printf(typval_T *argvars, typval_T *rettv);
697static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200698#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100699static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200700#endif
701#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100702static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200703#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100704static void f_range(typval_T *argvars, typval_T *rettv);
705static void f_readfile(typval_T *argvars, typval_T *rettv);
706static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100707#ifdef FEAT_FLOAT
708static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
709#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100710static void f_reltimestr(typval_T *argvars, typval_T *rettv);
711static void f_remote_expr(typval_T *argvars, typval_T *rettv);
712static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
713static void f_remote_peek(typval_T *argvars, typval_T *rettv);
714static void f_remote_read(typval_T *argvars, typval_T *rettv);
715static void f_remote_send(typval_T *argvars, typval_T *rettv);
716static void f_remove(typval_T *argvars, typval_T *rettv);
717static void f_rename(typval_T *argvars, typval_T *rettv);
718static void f_repeat(typval_T *argvars, typval_T *rettv);
719static void f_resolve(typval_T *argvars, typval_T *rettv);
720static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000721#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100722static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000723#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100724static void f_screenattr(typval_T *argvars, typval_T *rettv);
725static void f_screenchar(typval_T *argvars, typval_T *rettv);
726static void f_screencol(typval_T *argvars, typval_T *rettv);
727static void f_screenrow(typval_T *argvars, typval_T *rettv);
728static void f_search(typval_T *argvars, typval_T *rettv);
729static void f_searchdecl(typval_T *argvars, typval_T *rettv);
730static void f_searchpair(typval_T *argvars, typval_T *rettv);
731static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
732static void f_searchpos(typval_T *argvars, typval_T *rettv);
733static void f_server2client(typval_T *argvars, typval_T *rettv);
734static void f_serverlist(typval_T *argvars, typval_T *rettv);
735static void f_setbufvar(typval_T *argvars, typval_T *rettv);
736static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
737static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
738static void f_setline(typval_T *argvars, typval_T *rettv);
739static void f_setloclist(typval_T *argvars, typval_T *rettv);
740static void f_setmatches(typval_T *argvars, typval_T *rettv);
741static void f_setpos(typval_T *argvars, typval_T *rettv);
742static void f_setqflist(typval_T *argvars, typval_T *rettv);
743static void f_setreg(typval_T *argvars, typval_T *rettv);
744static void f_settabvar(typval_T *argvars, typval_T *rettv);
745static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
746static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100747#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100748static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100749#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100750static void f_shellescape(typval_T *argvars, typval_T *rettv);
751static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
752static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000753#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100754static void f_sin(typval_T *argvars, typval_T *rettv);
755static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000756#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100757static void f_sort(typval_T *argvars, typval_T *rettv);
758static void f_soundfold(typval_T *argvars, typval_T *rettv);
759static void f_spellbadword(typval_T *argvars, typval_T *rettv);
760static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
761static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000762#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_sqrt(typval_T *argvars, typval_T *rettv);
764static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000765#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100766static void f_str2nr(typval_T *argvars, typval_T *rettv);
767static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000768#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100769static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000770#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100771static void f_stridx(typval_T *argvars, typval_T *rettv);
772static void f_string(typval_T *argvars, typval_T *rettv);
773static void f_strlen(typval_T *argvars, typval_T *rettv);
774static void f_strpart(typval_T *argvars, typval_T *rettv);
775static void f_strridx(typval_T *argvars, typval_T *rettv);
776static void f_strtrans(typval_T *argvars, typval_T *rettv);
777static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
778static void f_strwidth(typval_T *argvars, typval_T *rettv);
779static void f_submatch(typval_T *argvars, typval_T *rettv);
780static void f_substitute(typval_T *argvars, typval_T *rettv);
781static void f_synID(typval_T *argvars, typval_T *rettv);
782static void f_synIDattr(typval_T *argvars, typval_T *rettv);
783static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
784static void f_synstack(typval_T *argvars, typval_T *rettv);
785static void f_synconcealed(typval_T *argvars, typval_T *rettv);
786static void f_system(typval_T *argvars, typval_T *rettv);
787static void f_systemlist(typval_T *argvars, typval_T *rettv);
788static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
789static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
790static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
791static void f_taglist(typval_T *argvars, typval_T *rettv);
792static void f_tagfiles(typval_T *argvars, typval_T *rettv);
793static void f_tempname(typval_T *argvars, typval_T *rettv);
794static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200795#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100796static void f_tan(typval_T *argvars, typval_T *rettv);
797static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200798#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100799static void f_tolower(typval_T *argvars, typval_T *rettv);
800static void f_toupper(typval_T *argvars, typval_T *rettv);
801static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000802#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100803static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000804#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100805static void f_type(typval_T *argvars, typval_T *rettv);
806static void f_undofile(typval_T *argvars, typval_T *rettv);
807static void f_undotree(typval_T *argvars, typval_T *rettv);
808static void f_uniq(typval_T *argvars, typval_T *rettv);
809static void f_values(typval_T *argvars, typval_T *rettv);
810static void f_virtcol(typval_T *argvars, typval_T *rettv);
811static void f_visualmode(typval_T *argvars, typval_T *rettv);
812static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
813static void f_winbufnr(typval_T *argvars, typval_T *rettv);
814static void f_wincol(typval_T *argvars, typval_T *rettv);
815static void f_winheight(typval_T *argvars, typval_T *rettv);
816static void f_winline(typval_T *argvars, typval_T *rettv);
817static void f_winnr(typval_T *argvars, typval_T *rettv);
818static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
819static void f_winrestview(typval_T *argvars, typval_T *rettv);
820static void f_winsaveview(typval_T *argvars, typval_T *rettv);
821static void f_winwidth(typval_T *argvars, typval_T *rettv);
822static void f_writefile(typval_T *argvars, typval_T *rettv);
823static void f_wordcount(typval_T *argvars, typval_T *rettv);
824static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000825
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100826static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
827static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
828static int get_env_len(char_u **arg);
829static int get_id_len(char_u **arg);
830static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
831static 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 +0000832#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
833#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
834 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100835static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
836static int eval_isnamec(int c);
837static int eval_isnamec1(int c);
838static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
839static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100840static typval_T *alloc_string_tv(char_u *string);
841static void init_tv(typval_T *varp);
842static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100843#ifdef FEAT_FLOAT
844static float_T get_tv_float(typval_T *varp);
845#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100846static linenr_T get_tv_lnum(typval_T *argvars);
847static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
848static char_u *get_tv_string(typval_T *varp);
849static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
850static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
851static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
852static hashtab_T *find_var_ht(char_u *name, char_u **varname);
853static funccall_T *get_funccal(void);
854static void vars_clear_ext(hashtab_T *ht, int free_val);
855static void delete_var(hashtab_T *ht, hashitem_T *hi);
856static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
857static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
858static void set_var(char_u *name, typval_T *varp, int copy);
859static int var_check_ro(int flags, char_u *name, int use_gettext);
860static int var_check_fixed(int flags, char_u *name, int use_gettext);
861static int var_check_func_name(char_u *name, int new_var);
862static int valid_varname(char_u *varname);
863static int tv_check_lock(int lock, char_u *name, int use_gettext);
864static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
865static char_u *find_option_end(char_u **arg, int *opt_flags);
866static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
867static int eval_fname_script(char_u *p);
868static int eval_fname_sid(char_u *p);
869static void list_func_head(ufunc_T *fp, int indent);
870static ufunc_T *find_func(char_u *name);
871static int function_exists(char_u *name);
872static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000873#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100874static void func_do_profile(ufunc_T *fp);
875static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
876static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000877static int
878# ifdef __BORLANDC__
879 _RTLENTRYF
880# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100881 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000882static int
883# ifdef __BORLANDC__
884 _RTLENTRYF
885# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100886 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000887#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100888static int script_autoload(char_u *name, int reload);
889static char_u *autoload_name(char_u *name);
890static void cat_func_name(char_u *buf, ufunc_T *fp);
891static void func_free(ufunc_T *fp);
892static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
893static int can_free_funccal(funccall_T *fc, int copyID) ;
894static void free_funccal(funccall_T *fc, int free_val);
895static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
896static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
897static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
898static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
899static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
900static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
901static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
902static int write_list(FILE *fd, list_T *list, int binary);
903static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000904
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200905
906#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100907static int compare_func_name(const void *s1, const void *s2);
908static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200909#endif
910
Bram Moolenaar33570922005-01-25 22:26:29 +0000911/*
912 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000913 */
914 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100915eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000916{
Bram Moolenaar33570922005-01-25 22:26:29 +0000917 int i;
918 struct vimvar *p;
919
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200920 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
921 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200922 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000923 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000924 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000925
926 for (i = 0; i < VV_LEN; ++i)
927 {
928 p = &vimvars[i];
929 STRCPY(p->vv_di.di_key, p->vv_name);
930 if (p->vv_flags & VV_RO)
931 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
932 else if (p->vv_flags & VV_RO_SBX)
933 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
934 else
935 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000936
937 /* add to v: scope dict, unless the value is not always available */
938 if (p->vv_type != VAR_UNKNOWN)
939 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000940 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000941 /* add to compat scope dict */
942 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000943 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100944 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
945
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000946 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100947 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200948 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100949 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100950
951 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
952 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
953 set_vim_var_nr(VV_NONE, VVAL_NONE);
954 set_vim_var_nr(VV_NULL, VVAL_NULL);
955
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200956 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200957
958#ifdef EBCDIC
959 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100960 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200961 */
962 sortFunctions();
963#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000964}
965
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000966#if defined(EXITFREE) || defined(PROTO)
967 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100968eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000969{
970 int i;
971 struct vimvar *p;
972
973 for (i = 0; i < VV_LEN; ++i)
974 {
975 p = &vimvars[i];
976 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000977 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000978 vim_free(p->vv_str);
979 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000980 }
981 else if (p->vv_di.di_tv.v_type == VAR_LIST)
982 {
983 list_unref(p->vv_list);
984 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000985 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000986 }
987 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000988 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000989 hash_clear(&compat_hashtab);
990
Bram Moolenaard9fba312005-06-26 22:34:35 +0000991 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100992# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200993 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100994# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000995
996 /* global variables */
997 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000998
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000999 /* autoloaded script names */
1000 ga_clear_strings(&ga_loaded);
1001
Bram Moolenaarcca74132013-09-25 21:00:28 +02001002 /* Script-local variables. First clear all the variables and in a second
1003 * loop free the scriptvar_T, because a variable in one script might hold
1004 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001005 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001006 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001007 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001008 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001009 ga_clear(&ga_scripts);
1010
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001011 /* unreferenced lists and dicts */
1012 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001013
1014 /* functions */
1015 free_all_functions();
1016 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001017}
1018#endif
1019
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 * Return the name of the executed function.
1022 */
1023 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001024func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001026 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027}
1028
1029/*
1030 * Return the address holding the next breakpoint line for a funccall cookie.
1031 */
1032 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001033func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034{
Bram Moolenaar33570922005-01-25 22:26:29 +00001035 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036}
1037
1038/*
1039 * Return the address holding the debug tick for a funccall cookie.
1040 */
1041 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001042func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043{
Bram Moolenaar33570922005-01-25 22:26:29 +00001044 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045}
1046
1047/*
1048 * Return the nesting level for a funccall cookie.
1049 */
1050 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001051func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052{
Bram Moolenaar33570922005-01-25 22:26:29 +00001053 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054}
1055
1056/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001057funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001059/* pointer to list of previously used funccal, still around because some
1060 * item in it is still being used. */
1061funccall_T *previous_funccal = NULL;
1062
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063/*
1064 * Return TRUE when a function was ended by a ":return" command.
1065 */
1066 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001067current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068{
1069 return current_funccal->returned;
1070}
1071
1072
1073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 * Set an internal variable to a string value. Creates the variable if it does
1075 * not already exist.
1076 */
1077 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001078set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001080 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001081 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082
1083 val = vim_strsave(value);
1084 if (val != NULL)
1085 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001086 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001087 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001089 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001090 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 }
1092 }
1093}
1094
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001096static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001097static char_u *redir_endp = NULL;
1098static char_u *redir_varname = NULL;
1099
1100/*
1101 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001102 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 * Returns OK if successfully completed the setup. FAIL otherwise.
1104 */
1105 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001106var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107{
1108 int save_emsg;
1109 int err;
1110 typval_T tv;
1111
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001112 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001113 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114 {
1115 EMSG(_(e_invarg));
1116 return FAIL;
1117 }
1118
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001119 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 redir_varname = vim_strsave(name);
1121 if (redir_varname == NULL)
1122 return FAIL;
1123
1124 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1125 if (redir_lval == NULL)
1126 {
1127 var_redir_stop();
1128 return FAIL;
1129 }
1130
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001131 /* The output is stored in growarray "redir_ga" until redirection ends. */
1132 ga_init2(&redir_ga, (int)sizeof(char), 500);
1133
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001135 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001136 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1138 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001139 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 if (redir_endp != NULL && *redir_endp != NUL)
1141 /* Trailing characters are present after the variable name */
1142 EMSG(_(e_trailing));
1143 else
1144 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001145 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146 var_redir_stop();
1147 return FAIL;
1148 }
1149
1150 /* check if we can write to the variable: set it to or append an empty
1151 * string */
1152 save_emsg = did_emsg;
1153 did_emsg = FALSE;
1154 tv.v_type = VAR_STRING;
1155 tv.vval.v_string = (char_u *)"";
1156 if (append)
1157 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1158 else
1159 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001160 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001162 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001163 if (err)
1164 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001165 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001166 var_redir_stop();
1167 return FAIL;
1168 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169
1170 return OK;
1171}
1172
1173/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001174 * Append "value[value_len]" to the variable set by var_redir_start().
1175 * The actual appending is postponed until redirection ends, because the value
1176 * appended may in fact be the string we write to, changing it may cause freed
1177 * memory to be used:
1178 * :redir => foo
1179 * :let foo
1180 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181 */
1182 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001183var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001184{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001185 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001186
1187 if (redir_lval == NULL)
1188 return;
1189
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001190 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001191 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001192 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001193 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001194
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001195 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001196 {
1197 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001198 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001199 }
1200 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001201 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001202}
1203
1204/*
1205 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001206 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207 */
1208 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001209var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001210{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001211 typval_T tv;
1212
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001213 if (redir_lval != NULL)
1214 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001215 /* If there was no error: assign the text to the variable. */
1216 if (redir_endp != NULL)
1217 {
1218 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1219 tv.v_type = VAR_STRING;
1220 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001221 /* Call get_lval() again, if it's inside a Dict or List it may
1222 * have changed. */
1223 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001224 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001225 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1226 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1227 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001228 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001229
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001230 /* free the collected output */
1231 vim_free(redir_ga.ga_data);
1232 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001233
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001234 vim_free(redir_lval);
1235 redir_lval = NULL;
1236 }
1237 vim_free(redir_varname);
1238 redir_varname = NULL;
1239}
1240
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241# if defined(FEAT_MBYTE) || defined(PROTO)
1242 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001243eval_charconvert(
1244 char_u *enc_from,
1245 char_u *enc_to,
1246 char_u *fname_from,
1247 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248{
1249 int err = FALSE;
1250
1251 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1252 set_vim_var_string(VV_CC_TO, enc_to, -1);
1253 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1254 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1255 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1256 err = TRUE;
1257 set_vim_var_string(VV_CC_FROM, NULL, -1);
1258 set_vim_var_string(VV_CC_TO, NULL, -1);
1259 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1260 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1261
1262 if (err)
1263 return FAIL;
1264 return OK;
1265}
1266# endif
1267
1268# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1269 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001270eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271{
1272 int err = FALSE;
1273
1274 set_vim_var_string(VV_FNAME_IN, fname, -1);
1275 set_vim_var_string(VV_CMDARG, args, -1);
1276 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1277 err = TRUE;
1278 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1279 set_vim_var_string(VV_CMDARG, NULL, -1);
1280
1281 if (err)
1282 {
1283 mch_remove(fname);
1284 return FAIL;
1285 }
1286 return OK;
1287}
1288# endif
1289
1290# if defined(FEAT_DIFF) || defined(PROTO)
1291 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001292eval_diff(
1293 char_u *origfile,
1294 char_u *newfile,
1295 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296{
1297 int err = FALSE;
1298
1299 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1300 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1301 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1302 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1303 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1304 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1305 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1306}
1307
1308 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001309eval_patch(
1310 char_u *origfile,
1311 char_u *difffile,
1312 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313{
1314 int err;
1315
1316 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1317 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1318 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1319 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1320 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1321 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1322 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1323}
1324# endif
1325
1326/*
1327 * Top level evaluation function, returning a boolean.
1328 * Sets "error" to TRUE if there was an error.
1329 * Return TRUE or FALSE.
1330 */
1331 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001332eval_to_bool(
1333 char_u *arg,
1334 int *error,
1335 char_u **nextcmd,
1336 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337{
Bram Moolenaar33570922005-01-25 22:26:29 +00001338 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 int retval = FALSE;
1340
1341 if (skip)
1342 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001343 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 else
1346 {
1347 *error = FALSE;
1348 if (!skip)
1349 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001350 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001351 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 }
1353 }
1354 if (skip)
1355 --emsg_skip;
1356
1357 return retval;
1358}
1359
1360/*
1361 * Top level evaluation function, returning a string. If "skip" is TRUE,
1362 * only parsing to "nextcmd" is done, without reporting errors. Return
1363 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1364 */
1365 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001366eval_to_string_skip(
1367 char_u *arg,
1368 char_u **nextcmd,
1369 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370{
Bram Moolenaar33570922005-01-25 22:26:29 +00001371 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 char_u *retval;
1373
1374 if (skip)
1375 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001376 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 retval = NULL;
1378 else
1379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001380 retval = vim_strsave(get_tv_string(&tv));
1381 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 }
1383 if (skip)
1384 --emsg_skip;
1385
1386 return retval;
1387}
1388
1389/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001390 * Skip over an expression at "*pp".
1391 * Return FAIL for an error, OK otherwise.
1392 */
1393 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001394skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001395{
Bram Moolenaar33570922005-01-25 22:26:29 +00001396 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001397
1398 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001399 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001400}
1401
1402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001404 * When "convert" is TRUE convert a List into a sequence of lines and convert
1405 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 * Return pointer to allocated memory, or NULL for failure.
1407 */
1408 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001409eval_to_string(
1410 char_u *arg,
1411 char_u **nextcmd,
1412 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413{
Bram Moolenaar33570922005-01-25 22:26:29 +00001414 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001416 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001417#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001418 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001421 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 retval = NULL;
1423 else
1424 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001425 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001426 {
1427 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001428 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001429 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001430 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001431 if (tv.vval.v_list->lv_len > 0)
1432 ga_append(&ga, NL);
1433 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001434 ga_append(&ga, NUL);
1435 retval = (char_u *)ga.ga_data;
1436 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001437#ifdef FEAT_FLOAT
1438 else if (convert && tv.v_type == VAR_FLOAT)
1439 {
1440 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1441 retval = vim_strsave(numbuf);
1442 }
1443#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001444 else
1445 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001446 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 }
1448
1449 return retval;
1450}
1451
1452/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001453 * Call eval_to_string() without using current local variables and using
1454 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 */
1456 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001457eval_to_string_safe(
1458 char_u *arg,
1459 char_u **nextcmd,
1460 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461{
1462 char_u *retval;
1463 void *save_funccalp;
1464
1465 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001466 if (use_sandbox)
1467 ++sandbox;
1468 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001469 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001470 if (use_sandbox)
1471 --sandbox;
1472 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 restore_funccal(save_funccalp);
1474 return retval;
1475}
1476
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477/*
1478 * Top level evaluation function, returning a number.
1479 * Evaluates "expr" silently.
1480 * Returns -1 for an error.
1481 */
1482 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001483eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
Bram Moolenaar33570922005-01-25 22:26:29 +00001485 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001487 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488
1489 ++emsg_off;
1490
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001491 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 retval = -1;
1493 else
1494 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001495 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001496 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 }
1498 --emsg_off;
1499
1500 return retval;
1501}
1502
Bram Moolenaara40058a2005-07-11 22:42:07 +00001503/*
1504 * Prepare v: variable "idx" to be used.
1505 * Save the current typeval in "save_tv".
1506 * When not used yet add the variable to the v: hashtable.
1507 */
1508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001509prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001510{
1511 *save_tv = vimvars[idx].vv_tv;
1512 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1513 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1514}
1515
1516/*
1517 * Restore v: variable "idx" to typeval "save_tv".
1518 * When no longer defined, remove the variable from the v: hashtable.
1519 */
1520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001521restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001522{
1523 hashitem_T *hi;
1524
Bram Moolenaara40058a2005-07-11 22:42:07 +00001525 vimvars[idx].vv_tv = *save_tv;
1526 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1527 {
1528 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1529 if (HASHITEM_EMPTY(hi))
1530 EMSG2(_(e_intern2), "restore_vimvar()");
1531 else
1532 hash_remove(&vimvarht, hi);
1533 }
1534}
1535
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001536#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001537/*
1538 * Evaluate an expression to a list with suggestions.
1539 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001540 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001541 */
1542 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001543eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001544{
1545 typval_T save_val;
1546 typval_T rettv;
1547 list_T *list = NULL;
1548 char_u *p = skipwhite(expr);
1549
1550 /* Set "v:val" to the bad word. */
1551 prepare_vimvar(VV_VAL, &save_val);
1552 vimvars[VV_VAL].vv_type = VAR_STRING;
1553 vimvars[VV_VAL].vv_str = badword;
1554 if (p_verbose == 0)
1555 ++emsg_off;
1556
1557 if (eval1(&p, &rettv, TRUE) == OK)
1558 {
1559 if (rettv.v_type != VAR_LIST)
1560 clear_tv(&rettv);
1561 else
1562 list = rettv.vval.v_list;
1563 }
1564
1565 if (p_verbose == 0)
1566 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001567 restore_vimvar(VV_VAL, &save_val);
1568
1569 return list;
1570}
1571
1572/*
1573 * "list" is supposed to contain two items: a word and a number. Return the
1574 * word in "pp" and the number as the return value.
1575 * Return -1 if anything isn't right.
1576 * Used to get the good word and score from the eval_spell_expr() result.
1577 */
1578 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001579get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001580{
1581 listitem_T *li;
1582
1583 li = list->lv_first;
1584 if (li == NULL)
1585 return -1;
1586 *pp = get_tv_string(&li->li_tv);
1587
1588 li = li->li_next;
1589 if (li == NULL)
1590 return -1;
1591 return get_tv_number(&li->li_tv);
1592}
1593#endif
1594
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001595/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001596 * Top level evaluation function.
1597 * Returns an allocated typval_T with the result.
1598 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001599 */
1600 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001601eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001602{
1603 typval_T *tv;
1604
1605 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001606 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001607 {
1608 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001609 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001610 }
1611
1612 return tv;
1613}
1614
1615
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001617 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001618 * Uses argv[argc] for the function arguments. Only Number and String
1619 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001620 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001622 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001623call_vim_function(
1624 char_u *func,
1625 int argc,
1626 char_u **argv,
1627 int safe, /* use the sandbox */
1628 int str_arg_only, /* all arguments are strings */
1629 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630{
Bram Moolenaar33570922005-01-25 22:26:29 +00001631 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 long n;
1633 int len;
1634 int i;
1635 int doesrange;
1636 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001637 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001639 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001641 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642
1643 for (i = 0; i < argc; i++)
1644 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001645 /* Pass a NULL or empty argument as an empty string */
1646 if (argv[i] == NULL || *argv[i] == NUL)
1647 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001648 argvars[i].v_type = VAR_STRING;
1649 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001650 continue;
1651 }
1652
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001653 if (str_arg_only)
1654 len = 0;
1655 else
1656 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001657 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 if (len != 0 && len == (int)STRLEN(argv[i]))
1659 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001660 argvars[i].v_type = VAR_NUMBER;
1661 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 }
1663 else
1664 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001665 argvars[i].v_type = VAR_STRING;
1666 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 }
1668 }
1669
1670 if (safe)
1671 {
1672 save_funccalp = save_funccal();
1673 ++sandbox;
1674 }
1675
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1677 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (safe)
1681 {
1682 --sandbox;
1683 restore_funccal(save_funccalp);
1684 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001685 vim_free(argvars);
1686
1687 if (ret == FAIL)
1688 clear_tv(rettv);
1689
1690 return ret;
1691}
1692
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001693/*
1694 * Call vimL function "func" and return the result as a number.
1695 * Returns -1 when calling the function fails.
1696 * Uses argv[argc] for the function arguments.
1697 */
1698 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001699call_func_retnr(
1700 char_u *func,
1701 int argc,
1702 char_u **argv,
1703 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001704{
1705 typval_T rettv;
1706 long retval;
1707
1708 /* All arguments are passed as strings, no conversion to number. */
1709 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1710 return -1;
1711
1712 retval = get_tv_number_chk(&rettv, NULL);
1713 clear_tv(&rettv);
1714 return retval;
1715}
1716
1717#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1718 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1719
Bram Moolenaar4f688582007-07-24 12:34:30 +00001720# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001721/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001722 * Call vimL function "func" and return the result as a string.
1723 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001724 * Uses argv[argc] for the function arguments.
1725 */
1726 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001727call_func_retstr(
1728 char_u *func,
1729 int argc,
1730 char_u **argv,
1731 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001732{
1733 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001734 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001735
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001736 /* All arguments are passed as strings, no conversion to number. */
1737 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001738 return NULL;
1739
1740 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001741 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 return retval;
1743}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001744# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001745
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001746/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001747 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001748 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001749 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001750 */
1751 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001752call_func_retlist(
1753 char_u *func,
1754 int argc,
1755 char_u **argv,
1756 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001757{
1758 typval_T rettv;
1759
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001760 /* All arguments are passed as strings, no conversion to number. */
1761 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001762 return NULL;
1763
1764 if (rettv.v_type != VAR_LIST)
1765 {
1766 clear_tv(&rettv);
1767 return NULL;
1768 }
1769
1770 return rettv.vval.v_list;
1771}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772#endif
1773
1774/*
1775 * Save the current function call pointer, and set it to NULL.
1776 * Used when executing autocommands and for ":source".
1777 */
1778 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001779save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001781 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 current_funccal = NULL;
1784 return (void *)fc;
1785}
1786
1787 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001788restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001790 funccall_T *fc = (funccall_T *)vfc;
1791
1792 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793}
1794
Bram Moolenaar05159a02005-02-26 23:04:13 +00001795#if defined(FEAT_PROFILE) || defined(PROTO)
1796/*
1797 * Prepare profiling for entering a child or something else that is not
1798 * counted for the script/function itself.
1799 * Should always be called in pair with prof_child_exit().
1800 */
1801 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001802prof_child_enter(
1803 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001804{
1805 funccall_T *fc = current_funccal;
1806
1807 if (fc != NULL && fc->func->uf_profiling)
1808 profile_start(&fc->prof_child);
1809 script_prof_save(tm);
1810}
1811
1812/*
1813 * Take care of time spent in a child.
1814 * Should always be called after prof_child_enter().
1815 */
1816 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001817prof_child_exit(
1818 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001819{
1820 funccall_T *fc = current_funccal;
1821
1822 if (fc != NULL && fc->func->uf_profiling)
1823 {
1824 profile_end(&fc->prof_child);
1825 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1826 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1827 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1828 }
1829 script_prof_restore(tm);
1830}
1831#endif
1832
1833
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834#ifdef FEAT_FOLDING
1835/*
1836 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1837 * it in "*cp". Doesn't give error messages.
1838 */
1839 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001840eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841{
Bram Moolenaar33570922005-01-25 22:26:29 +00001842 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 int retval;
1844 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001845 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1846 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847
1848 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001849 if (use_sandbox)
1850 ++sandbox;
1851 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 retval = 0;
1855 else
1856 {
1857 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 if (tv.v_type == VAR_NUMBER)
1859 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001860 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 retval = 0;
1862 else
1863 {
1864 /* If the result is a string, check if there is a non-digit before
1865 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 if (!VIM_ISDIGIT(*s) && *s != '-')
1868 *cp = *s++;
1869 retval = atol((char *)s);
1870 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 }
1873 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001874 if (use_sandbox)
1875 --sandbox;
1876 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
1878 return retval;
1879}
1880#endif
1881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 * ":let" list all variable values
1884 * ":let var1 var2" list variable values
1885 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001886 * ":let var += expr" assignment command.
1887 * ":let var -= expr" assignment command.
1888 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 */
1891 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001892ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893{
1894 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001896 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 int var_count = 0;
1899 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001901 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001902 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903
Bram Moolenaardb552d602006-03-23 22:59:57 +00001904 argend = skip_var_list(arg, &var_count, &semicolon);
1905 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001907 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1908 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001909 expr = skipwhite(argend);
1910 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1911 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001913 /*
1914 * ":let" without "=": list variables
1915 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001916 if (*arg == '[')
1917 EMSG(_(e_invarg));
1918 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001920 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001921 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001922 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001924 list_glob_vars(&first);
1925 list_buf_vars(&first);
1926 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001927#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001928 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001929#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001930 list_script_vars(&first);
1931 list_func_vars(&first);
1932 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 eap->nextcmd = check_nextcmd(arg);
1935 }
1936 else
1937 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001938 op[0] = '=';
1939 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001940 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001941 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001942 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1943 op[0] = *expr; /* +=, -= or .= */
1944 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001945 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001946 else
1947 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001948
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 if (eap->skip)
1950 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 if (eap->skip)
1953 {
1954 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 --emsg_skip;
1957 }
1958 else if (i != FAIL)
1959 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001961 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001962 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 }
1964 }
1965}
1966
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001967/*
1968 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1969 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001970 * When "nextchars" is not NULL it points to a string with characters that
1971 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1972 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 * Returns OK or FAIL;
1974 */
1975 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001976ex_let_vars(
1977 char_u *arg_start,
1978 typval_T *tv,
1979 int copy, /* copy values from "tv", don't move */
1980 int semicolon, /* from skip_var_list() */
1981 int var_count, /* from skip_var_list() */
1982 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983{
1984 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001985 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001987 listitem_T *item;
1988 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989
1990 if (*arg != '[')
1991 {
1992 /*
1993 * ":let var = expr" or ":for var in list"
1994 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001995 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 return FAIL;
1997 return OK;
1998 }
1999
2000 /*
2001 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2002 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002003 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004 {
2005 EMSG(_(e_listreq));
2006 return FAIL;
2007 }
2008
2009 i = list_len(l);
2010 if (semicolon == 0 && var_count < i)
2011 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002012 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013 return FAIL;
2014 }
2015 if (var_count - semicolon > i)
2016 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002017 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002018 return FAIL;
2019 }
2020
2021 item = l->lv_first;
2022 while (*arg != ']')
2023 {
2024 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002025 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002026 item = item->li_next;
2027 if (arg == NULL)
2028 return FAIL;
2029
2030 arg = skipwhite(arg);
2031 if (*arg == ';')
2032 {
2033 /* Put the rest of the list (may be empty) in the var after ';'.
2034 * Create a new list for this. */
2035 l = list_alloc();
2036 if (l == NULL)
2037 return FAIL;
2038 while (item != NULL)
2039 {
2040 list_append_tv(l, &item->li_tv);
2041 item = item->li_next;
2042 }
2043
2044 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002045 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002046 ltv.vval.v_list = l;
2047 l->lv_refcount = 1;
2048
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002049 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2050 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002051 clear_tv(&ltv);
2052 if (arg == NULL)
2053 return FAIL;
2054 break;
2055 }
2056 else if (*arg != ',' && *arg != ']')
2057 {
2058 EMSG2(_(e_intern2), "ex_let_vars()");
2059 return FAIL;
2060 }
2061 }
2062
2063 return OK;
2064}
2065
2066/*
2067 * Skip over assignable variable "var" or list of variables "[var, var]".
2068 * Used for ":let varvar = expr" and ":for varvar in expr".
2069 * For "[var, var]" increment "*var_count" for each variable.
2070 * for "[var, var; var]" set "semicolon".
2071 * Return NULL for an error.
2072 */
2073 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002074skip_var_list(
2075 char_u *arg,
2076 int *var_count,
2077 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002078{
2079 char_u *p, *s;
2080
2081 if (*arg == '[')
2082 {
2083 /* "[var, var]": find the matching ']'. */
2084 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002085 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002086 {
2087 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2088 s = skip_var_one(p);
2089 if (s == p)
2090 {
2091 EMSG2(_(e_invarg2), p);
2092 return NULL;
2093 }
2094 ++*var_count;
2095
2096 p = skipwhite(s);
2097 if (*p == ']')
2098 break;
2099 else if (*p == ';')
2100 {
2101 if (*semicolon == 1)
2102 {
2103 EMSG(_("Double ; in list of variables"));
2104 return NULL;
2105 }
2106 *semicolon = 1;
2107 }
2108 else if (*p != ',')
2109 {
2110 EMSG2(_(e_invarg2), p);
2111 return NULL;
2112 }
2113 }
2114 return p + 1;
2115 }
2116 else
2117 return skip_var_one(arg);
2118}
2119
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002120/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002121 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002122 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002123 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002124 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002125skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002126{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002127 if (*arg == '@' && arg[1] != NUL)
2128 return arg + 2;
2129 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2130 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002131}
2132
Bram Moolenaara7043832005-01-21 11:56:39 +00002133/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002134 * List variables for hashtab "ht" with prefix "prefix".
2135 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002136 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002138list_hashtable_vars(
2139 hashtab_T *ht,
2140 char_u *prefix,
2141 int empty,
2142 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002143{
Bram Moolenaar33570922005-01-25 22:26:29 +00002144 hashitem_T *hi;
2145 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002146 int todo;
2147
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002148 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002149 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2150 {
2151 if (!HASHITEM_EMPTY(hi))
2152 {
2153 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002154 di = HI2DI(hi);
2155 if (empty || di->di_tv.v_type != VAR_STRING
2156 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002157 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002158 }
2159 }
2160}
2161
2162/*
2163 * List global variables.
2164 */
2165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002166list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002167{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002169}
2170
2171/*
2172 * List buffer variables.
2173 */
2174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002175list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002176{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002177 char_u numbuf[NUMBUFLEN];
2178
Bram Moolenaar429fa852013-04-15 12:27:36 +02002179 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002181
2182 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2184 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002185}
2186
2187/*
2188 * List window variables.
2189 */
2190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002191list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002192{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002193 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002195}
2196
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002197#ifdef FEAT_WINDOWS
2198/*
2199 * List tab page variables.
2200 */
2201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002202list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002203{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002204 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002205 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002206}
2207#endif
2208
Bram Moolenaara7043832005-01-21 11:56:39 +00002209/*
2210 * List Vim variables.
2211 */
2212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002213list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002215 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216}
2217
2218/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002219 * List script-local variables, if there is a script.
2220 */
2221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002222list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002223{
2224 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002225 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2226 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002227}
2228
2229/*
2230 * List function variables, if there is a function.
2231 */
2232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002233list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002234{
2235 if (current_funccal != NULL)
2236 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002237 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002238}
2239
2240/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241 * List variables in "arg".
2242 */
2243 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002244list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245{
2246 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 char_u *name_start;
2250 char_u *arg_subsc;
2251 char_u *tofree;
2252 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253
2254 while (!ends_excmd(*arg) && !got_int)
2255 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002258 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2260 {
2261 emsg_severe = TRUE;
2262 EMSG(_(e_trailing));
2263 break;
2264 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002268 /* get_name_len() takes care of expanding curly braces */
2269 name_start = name = arg;
2270 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2271 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 /* This is mainly to keep test 49 working: when expanding
2274 * curly braces fails overrule the exception error message. */
2275 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002277 emsg_severe = TRUE;
2278 EMSG2(_(e_invarg2), arg);
2279 break;
2280 }
2281 error = TRUE;
2282 }
2283 else
2284 {
2285 if (tofree != NULL)
2286 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002287 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 else
2290 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002291 /* handle d.key, l[idx], f(expr) */
2292 arg_subsc = arg;
2293 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002294 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002296 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002297 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002298 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002300 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002301 case 'g': list_glob_vars(first); break;
2302 case 'b': list_buf_vars(first); break;
2303 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002304#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002305 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002306#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002307 case 'v': list_vim_vars(first); break;
2308 case 's': list_script_vars(first); break;
2309 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002310 default:
2311 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002312 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002313 }
2314 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002315 {
2316 char_u numbuf[NUMBUFLEN];
2317 char_u *tf;
2318 int c;
2319 char_u *s;
2320
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002321 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002322 c = *arg;
2323 *arg = NUL;
2324 list_one_var_a((char_u *)"",
2325 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002326 tv.v_type,
2327 s == NULL ? (char_u *)"" : s,
2328 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002329 *arg = c;
2330 vim_free(tf);
2331 }
2332 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002333 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 }
2335 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002336
2337 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002338 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002339
2340 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 }
2342
2343 return arg;
2344}
2345
2346/*
2347 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2348 * Returns a pointer to the char just after the var name.
2349 * Returns NULL if there is an error.
2350 */
2351 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002352ex_let_one(
2353 char_u *arg, /* points to variable name */
2354 typval_T *tv, /* value to assign to variable */
2355 int copy, /* copy value from "tv" */
2356 char_u *endchars, /* valid chars after variable name or NULL */
2357 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002358{
2359 int c1;
2360 char_u *name;
2361 char_u *p;
2362 char_u *arg_end = NULL;
2363 int len;
2364 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002365 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002366
2367 /*
2368 * ":let $VAR = expr": Set environment variable.
2369 */
2370 if (*arg == '$')
2371 {
2372 /* Find the end of the name. */
2373 ++arg;
2374 name = arg;
2375 len = get_env_len(&arg);
2376 if (len == 0)
2377 EMSG2(_(e_invarg2), name - 1);
2378 else
2379 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002380 if (op != NULL && (*op == '+' || *op == '-'))
2381 EMSG2(_(e_letwrong), op);
2382 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002383 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002384 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002385 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002386 {
2387 c1 = name[len];
2388 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002389 p = get_tv_string_chk(tv);
2390 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 {
2392 int mustfree = FALSE;
2393 char_u *s = vim_getenv(name, &mustfree);
2394
2395 if (s != NULL)
2396 {
2397 p = tofree = concat_str(s, p);
2398 if (mustfree)
2399 vim_free(s);
2400 }
2401 }
2402 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002403 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002404 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002405 if (STRICMP(name, "HOME") == 0)
2406 init_homedir();
2407 else if (didset_vim && STRICMP(name, "VIM") == 0)
2408 didset_vim = FALSE;
2409 else if (didset_vimruntime
2410 && STRICMP(name, "VIMRUNTIME") == 0)
2411 didset_vimruntime = FALSE;
2412 arg_end = arg;
2413 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002414 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002416 }
2417 }
2418 }
2419
2420 /*
2421 * ":let &option = expr": Set option value.
2422 * ":let &l:option = expr": Set local option value.
2423 * ":let &g:option = expr": Set global option value.
2424 */
2425 else if (*arg == '&')
2426 {
2427 /* Find the end of the name. */
2428 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002429 if (p == NULL || (endchars != NULL
2430 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002431 EMSG(_(e_letunexp));
2432 else
2433 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002434 long n;
2435 int opt_type;
2436 long numval;
2437 char_u *stringval = NULL;
2438 char_u *s;
2439
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002440 c1 = *p;
2441 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442
2443 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002444 s = get_tv_string_chk(tv); /* != NULL if number or string */
2445 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 {
2447 opt_type = get_option_value(arg, &numval,
2448 &stringval, opt_flags);
2449 if ((opt_type == 1 && *op == '.')
2450 || (opt_type == 0 && *op != '.'))
2451 EMSG2(_(e_letwrong), op);
2452 else
2453 {
2454 if (opt_type == 1) /* number */
2455 {
2456 if (*op == '+')
2457 n = numval + n;
2458 else
2459 n = numval - n;
2460 }
2461 else if (opt_type == 0 && stringval != NULL) /* string */
2462 {
2463 s = concat_str(stringval, s);
2464 vim_free(stringval);
2465 stringval = s;
2466 }
2467 }
2468 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002469 if (s != NULL)
2470 {
2471 set_option_value(arg, n, s, opt_flags);
2472 arg_end = p;
2473 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 }
2477 }
2478
2479 /*
2480 * ":let @r = expr": Set register contents.
2481 */
2482 else if (*arg == '@')
2483 {
2484 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002485 if (op != NULL && (*op == '+' || *op == '-'))
2486 EMSG2(_(e_letwrong), op);
2487 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002488 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002489 EMSG(_(e_letunexp));
2490 else
2491 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002492 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002493 char_u *s;
2494
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002495 p = get_tv_string_chk(tv);
2496 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002497 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002498 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002499 if (s != NULL)
2500 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002501 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 vim_free(s);
2503 }
2504 }
2505 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002506 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002507 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002508 arg_end = arg + 1;
2509 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002510 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 }
2512 }
2513
2514 /*
2515 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002518 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002520 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002522 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2526 EMSG(_(e_letunexp));
2527 else
2528 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002529 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 arg_end = p;
2531 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002532 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002534 }
2535
2536 else
2537 EMSG2(_(e_invarg2), arg);
2538
2539 return arg_end;
2540}
2541
2542/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002543 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2544 */
2545 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002546check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002547{
2548 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2549 {
2550 EMSG2(_(e_readonlyvar), arg);
2551 return TRUE;
2552 }
2553 return FALSE;
2554}
2555
2556/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 * Get an lval: variable, Dict item or List item that can be assigned a value
2558 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2559 * "name.key", "name.key[expr]" etc.
2560 * Indexing only works if "name" is an existing List or Dictionary.
2561 * "name" points to the start of the name.
2562 * If "rettv" is not NULL it points to the value to be assigned.
2563 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2564 * wrong; must end in space or cmd separator.
2565 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002566 * flags:
2567 * GLV_QUIET: do not give error messages
2568 * GLV_NO_AUTOLOAD: do not use script autoloading
2569 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002571 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002573 */
2574 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002575get_lval(
2576 char_u *name,
2577 typval_T *rettv,
2578 lval_T *lp,
2579 int unlet,
2580 int skip,
2581 int flags, /* GLV_ values */
2582 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002583{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 char_u *expr_start, *expr_end;
2586 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002587 dictitem_T *v;
2588 typval_T var1;
2589 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002591 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002592 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002593 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002594 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002595 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002598 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599
2600 if (skip)
2601 {
2602 /* When skipping just find the end of the name. */
2603 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002604 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 }
2606
2607 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002608 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 if (expr_start != NULL)
2610 {
2611 /* Don't expand the name when we already know there is an error. */
2612 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2613 && *p != '[' && *p != '.')
2614 {
2615 EMSG(_(e_trailing));
2616 return NULL;
2617 }
2618
2619 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2620 if (lp->ll_exp_name == NULL)
2621 {
2622 /* Report an invalid expression in braces, unless the
2623 * expression evaluation has been cancelled due to an
2624 * aborting error, an interrupt, or an exception. */
2625 if (!aborting() && !quiet)
2626 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002627 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 EMSG2(_(e_invarg2), name);
2629 return NULL;
2630 }
2631 }
2632 lp->ll_name = lp->ll_exp_name;
2633 }
2634 else
2635 lp->ll_name = name;
2636
2637 /* Without [idx] or .key we are done. */
2638 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2639 return p;
2640
2641 cc = *p;
2642 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002643 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 if (v == NULL && !quiet)
2645 EMSG2(_(e_undefvar), lp->ll_name);
2646 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002647 if (v == NULL)
2648 return NULL;
2649
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 /*
2651 * Loop until no more [idx] or .key is following.
2652 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002653 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2657 && !(lp->ll_tv->v_type == VAR_DICT
2658 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002659 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 if (!quiet)
2661 EMSG(_("E689: Can only index a List or Dictionary"));
2662 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002663 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (!quiet)
2667 EMSG(_("E708: [:] must come last"));
2668 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002669 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002670
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 len = -1;
2672 if (*p == '.')
2673 {
2674 key = p + 1;
2675 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2676 ;
2677 if (len == 0)
2678 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 if (!quiet)
2680 EMSG(_(e_emptykey));
2681 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 }
2683 p = key + len;
2684 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 else
2686 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 if (*p == ':')
2690 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 else
2692 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 empty1 = FALSE;
2694 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002696 if (get_tv_string_chk(&var1) == NULL)
2697 {
2698 /* not a number or string */
2699 clear_tv(&var1);
2700 return NULL;
2701 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 }
2703
2704 /* Optionally get the second index [ :expr]. */
2705 if (*p == ':')
2706 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002710 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002711 if (!empty1)
2712 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002714 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (rettv != NULL && (rettv->v_type != VAR_LIST
2716 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 if (!quiet)
2719 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 if (!empty1)
2721 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 }
2724 p = skipwhite(p + 1);
2725 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 else
2728 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2731 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 if (!empty1)
2733 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002736 if (get_tv_string_chk(&var2) == NULL)
2737 {
2738 /* not a number or string */
2739 if (!empty1)
2740 clear_tv(&var1);
2741 clear_tv(&var2);
2742 return NULL;
2743 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002746 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002749
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002751 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 if (!quiet)
2753 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 if (!empty1)
2755 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 }
2760
2761 /* Skip to past ']'. */
2762 ++p;
2763 }
2764
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 {
2767 if (len == -1)
2768 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002770 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 if (*key == NUL)
2772 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 if (!quiet)
2774 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 }
2778 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002779 lp->ll_list = NULL;
2780 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002781 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002782
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002783 /* When assigning to a scope dictionary check that a function and
2784 * variable name is valid (only variable name unless it is l: or
2785 * g: dictionary). Disallow overwriting a builtin function. */
2786 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002787 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002788 int prevval;
2789 int wrong;
2790
2791 if (len != -1)
2792 {
2793 prevval = key[len];
2794 key[len] = NUL;
2795 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002796 else
2797 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002798 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2799 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002800 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002801 || !valid_varname(key);
2802 if (len != -1)
2803 key[len] = prevval;
2804 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002805 return NULL;
2806 }
2807
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002810 /* Can't add "v:" variable. */
2811 if (lp->ll_dict == &vimvardict)
2812 {
2813 EMSG2(_(e_illvar), name);
2814 return NULL;
2815 }
2816
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002817 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002821 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 if (len == -1)
2823 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 }
2826 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 if (len == -1)
2831 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 p = NULL;
2834 break;
2835 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002836 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002837 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002838 return NULL;
2839
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 if (len == -1)
2841 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 }
2844 else
2845 {
2846 /*
2847 * Get the number and item for the only or first index of the List.
2848 */
2849 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002851 else
2852 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002853 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 clear_tv(&var1);
2855 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002856 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 lp->ll_list = lp->ll_tv->vval.v_list;
2858 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2859 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002861 if (lp->ll_n1 < 0)
2862 {
2863 lp->ll_n1 = 0;
2864 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2865 }
2866 }
2867 if (lp->ll_li == NULL)
2868 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002870 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002871 if (!quiet)
2872 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 }
2875
2876 /*
2877 * May need to find the item or absolute index for the second
2878 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 * When no index given: "lp->ll_empty2" is TRUE.
2880 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002881 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002883 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002884 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002885 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002890 {
2891 if (!quiet)
2892 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002894 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002896 }
2897
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2899 if (lp->ll_n1 < 0)
2900 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2901 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002902 {
2903 if (!quiet)
2904 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002906 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002907 }
2908
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002910 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002911 }
2912
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 return p;
2914}
2915
2916/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002917 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 */
2919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002920clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921{
2922 vim_free(lp->ll_exp_name);
2923 vim_free(lp->ll_newkey);
2924}
2925
2926/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002927 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002929 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 */
2931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002932set_var_lval(
2933 lval_T *lp,
2934 char_u *endp,
2935 typval_T *rettv,
2936 int copy,
2937 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938{
2939 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002940 listitem_T *ri;
2941 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942
2943 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002944 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002946 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 cc = *endp;
2948 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002949 if (op != NULL && *op != '=')
2950 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002951 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002952
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002953 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002954 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002955 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002956 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002957 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002958 if ((di == NULL
2959 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2960 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2961 FALSE)))
2962 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002963 set_var(lp->ll_name, &tv, FALSE);
2964 clear_tv(&tv);
2965 }
2966 }
2967 else
2968 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002970 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002972 else if (tv_check_lock(lp->ll_newkey == NULL
2973 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002974 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002975 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976 else if (lp->ll_range)
2977 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002978 listitem_T *ll_li = lp->ll_li;
2979 int ll_n1 = lp->ll_n1;
2980
2981 /*
2982 * Check whether any of the list items is locked
2983 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002984 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002985 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002986 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002987 return;
2988 ri = ri->li_next;
2989 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2990 break;
2991 ll_li = ll_li->li_next;
2992 ++ll_n1;
2993 }
2994
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 /*
2996 * Assign the List values to the list items.
2997 */
2998 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002999 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003000 if (op != NULL && *op != '=')
3001 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3002 else
3003 {
3004 clear_tv(&lp->ll_li->li_tv);
3005 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3006 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 ri = ri->li_next;
3008 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3009 break;
3010 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003011 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003013 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003014 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003015 ri = NULL;
3016 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003017 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003018 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003019 lp->ll_li = lp->ll_li->li_next;
3020 ++lp->ll_n1;
3021 }
3022 if (ri != NULL)
3023 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003024 else if (lp->ll_empty2
3025 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003026 : lp->ll_n1 != lp->ll_n2)
3027 EMSG(_("E711: List value has not enough items"));
3028 }
3029 else
3030 {
3031 /*
3032 * Assign to a List or Dictionary item.
3033 */
3034 if (lp->ll_newkey != NULL)
3035 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003036 if (op != NULL && *op != '=')
3037 {
3038 EMSG2(_(e_letwrong), op);
3039 return;
3040 }
3041
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003043 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003044 if (di == NULL)
3045 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003046 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3047 {
3048 vim_free(di);
3049 return;
3050 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003051 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003052 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003053 else if (op != NULL && *op != '=')
3054 {
3055 tv_op(lp->ll_tv, rettv, op);
3056 return;
3057 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003058 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003059 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003060
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003061 /*
3062 * Assign the value to the variable or list item.
3063 */
3064 if (copy)
3065 copy_tv(rettv, lp->ll_tv);
3066 else
3067 {
3068 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003069 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003070 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003071 }
3072 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003073}
3074
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003075/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003076 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3077 * Returns OK or FAIL.
3078 */
3079 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003080tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003081{
3082 long n;
3083 char_u numbuf[NUMBUFLEN];
3084 char_u *s;
3085
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003086 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3087 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3088 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003089 {
3090 switch (tv1->v_type)
3091 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003092 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003093 case VAR_DICT:
3094 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003095 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003096 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003097 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003098 break;
3099
3100 case VAR_LIST:
3101 if (*op != '+' || tv2->v_type != VAR_LIST)
3102 break;
3103 /* List += List */
3104 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3105 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3106 return OK;
3107
3108 case VAR_NUMBER:
3109 case VAR_STRING:
3110 if (tv2->v_type == VAR_LIST)
3111 break;
3112 if (*op == '+' || *op == '-')
3113 {
3114 /* nr += nr or nr -= nr*/
3115 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003116#ifdef FEAT_FLOAT
3117 if (tv2->v_type == VAR_FLOAT)
3118 {
3119 float_T f = n;
3120
3121 if (*op == '+')
3122 f += tv2->vval.v_float;
3123 else
3124 f -= tv2->vval.v_float;
3125 clear_tv(tv1);
3126 tv1->v_type = VAR_FLOAT;
3127 tv1->vval.v_float = f;
3128 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003129 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003130#endif
3131 {
3132 if (*op == '+')
3133 n += get_tv_number(tv2);
3134 else
3135 n -= get_tv_number(tv2);
3136 clear_tv(tv1);
3137 tv1->v_type = VAR_NUMBER;
3138 tv1->vval.v_number = n;
3139 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003140 }
3141 else
3142 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003143 if (tv2->v_type == VAR_FLOAT)
3144 break;
3145
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003146 /* str .= str */
3147 s = get_tv_string(tv1);
3148 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3149 clear_tv(tv1);
3150 tv1->v_type = VAR_STRING;
3151 tv1->vval.v_string = s;
3152 }
3153 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003154
3155#ifdef FEAT_FLOAT
3156 case VAR_FLOAT:
3157 {
3158 float_T f;
3159
3160 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3161 && tv2->v_type != VAR_NUMBER
3162 && tv2->v_type != VAR_STRING))
3163 break;
3164 if (tv2->v_type == VAR_FLOAT)
3165 f = tv2->vval.v_float;
3166 else
3167 f = get_tv_number(tv2);
3168 if (*op == '+')
3169 tv1->vval.v_float += f;
3170 else
3171 tv1->vval.v_float -= f;
3172 }
3173 return OK;
3174#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003175 }
3176 }
3177
3178 EMSG2(_(e_letwrong), op);
3179 return FAIL;
3180}
3181
3182/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003183 * Add a watcher to a list.
3184 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003185 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003186list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003187{
3188 lw->lw_next = l->lv_watch;
3189 l->lv_watch = lw;
3190}
3191
3192/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003193 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194 * No warning when it isn't found...
3195 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003196 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003197list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198{
Bram Moolenaar33570922005-01-25 22:26:29 +00003199 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003200
3201 lwp = &l->lv_watch;
3202 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3203 {
3204 if (lw == lwrem)
3205 {
3206 *lwp = lw->lw_next;
3207 break;
3208 }
3209 lwp = &lw->lw_next;
3210 }
3211}
3212
3213/*
3214 * Just before removing an item from a list: advance watchers to the next
3215 * item.
3216 */
3217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003218list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219{
Bram Moolenaar33570922005-01-25 22:26:29 +00003220 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221
3222 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3223 if (lw->lw_item == item)
3224 lw->lw_item = item->li_next;
3225}
3226
3227/*
3228 * Evaluate the expression used in a ":for var in expr" command.
3229 * "arg" points to "var".
3230 * Set "*errp" to TRUE for an error, FALSE otherwise;
3231 * Return a pointer that holds the info. Null when there is an error.
3232 */
3233 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003234eval_for_line(
3235 char_u *arg,
3236 int *errp,
3237 char_u **nextcmdp,
3238 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239{
Bram Moolenaar33570922005-01-25 22:26:29 +00003240 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003241 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003242 typval_T tv;
3243 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244
3245 *errp = TRUE; /* default: there is an error */
3246
Bram Moolenaar33570922005-01-25 22:26:29 +00003247 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003248 if (fi == NULL)
3249 return NULL;
3250
3251 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3252 if (expr == NULL)
3253 return fi;
3254
3255 expr = skipwhite(expr);
3256 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3257 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003258 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003259 return fi;
3260 }
3261
3262 if (skip)
3263 ++emsg_skip;
3264 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3265 {
3266 *errp = FALSE;
3267 if (!skip)
3268 {
3269 l = tv.vval.v_list;
3270 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003271 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003273 clear_tv(&tv);
3274 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003275 else
3276 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003277 /* No need to increment the refcount, it's already set for the
3278 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 fi->fi_list = l;
3280 list_add_watch(l, &fi->fi_lw);
3281 fi->fi_lw.lw_item = l->lv_first;
3282 }
3283 }
3284 }
3285 if (skip)
3286 --emsg_skip;
3287
3288 return fi;
3289}
3290
3291/*
3292 * Use the first item in a ":for" list. Advance to the next.
3293 * Assign the values to the variable (list). "arg" points to the first one.
3294 * Return TRUE when a valid item was found, FALSE when at end of list or
3295 * something wrong.
3296 */
3297 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003298next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003299{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003300 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003302 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003303
3304 item = fi->fi_lw.lw_item;
3305 if (item == NULL)
3306 result = FALSE;
3307 else
3308 {
3309 fi->fi_lw.lw_item = item->li_next;
3310 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3311 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3312 }
3313 return result;
3314}
3315
3316/*
3317 * Free the structure used to store info used by ":for".
3318 */
3319 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003320free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321{
Bram Moolenaar33570922005-01-25 22:26:29 +00003322 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003323
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003324 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003325 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003326 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003327 list_unref(fi->fi_list);
3328 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003329 vim_free(fi);
3330}
3331
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3333
3334 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003335set_context_for_expression(
3336 expand_T *xp,
3337 char_u *arg,
3338 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339{
3340 int got_eq = FALSE;
3341 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003344 if (cmdidx == CMD_let)
3345 {
3346 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003347 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348 {
3349 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003350 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003351 {
3352 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003353 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003354 if (vim_iswhite(*p))
3355 break;
3356 }
3357 return;
3358 }
3359 }
3360 else
3361 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3362 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 while ((xp->xp_pattern = vim_strpbrk(arg,
3364 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3365 {
3366 c = *xp->xp_pattern;
3367 if (c == '&')
3368 {
3369 c = xp->xp_pattern[1];
3370 if (c == '&')
3371 {
3372 ++xp->xp_pattern;
3373 xp->xp_context = cmdidx != CMD_let || got_eq
3374 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3375 }
3376 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003377 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003379 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3380 xp->xp_pattern += 2;
3381
3382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 }
3384 else if (c == '$')
3385 {
3386 /* environment variable */
3387 xp->xp_context = EXPAND_ENV_VARS;
3388 }
3389 else if (c == '=')
3390 {
3391 got_eq = TRUE;
3392 xp->xp_context = EXPAND_EXPRESSION;
3393 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003394 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 && xp->xp_context == EXPAND_FUNCTIONS
3396 && vim_strchr(xp->xp_pattern, '(') == NULL)
3397 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003398 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 break;
3400 }
3401 else if (cmdidx != CMD_let || got_eq)
3402 {
3403 if (c == '"') /* string */
3404 {
3405 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3406 if (c == '\\' && xp->xp_pattern[1] != NUL)
3407 ++xp->xp_pattern;
3408 xp->xp_context = EXPAND_NOTHING;
3409 }
3410 else if (c == '\'') /* literal string */
3411 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003412 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3414 /* skip */ ;
3415 xp->xp_context = EXPAND_NOTHING;
3416 }
3417 else if (c == '|')
3418 {
3419 if (xp->xp_pattern[1] == '|')
3420 {
3421 ++xp->xp_pattern;
3422 xp->xp_context = EXPAND_EXPRESSION;
3423 }
3424 else
3425 xp->xp_context = EXPAND_COMMANDS;
3426 }
3427 else
3428 xp->xp_context = EXPAND_EXPRESSION;
3429 }
3430 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003431 /* Doesn't look like something valid, expand as an expression
3432 * anyway. */
3433 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 arg = xp->xp_pattern;
3435 if (*arg != NUL)
3436 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3437 /* skip */ ;
3438 }
3439 xp->xp_pattern = arg;
3440}
3441
3442#endif /* FEAT_CMDL_COMPL */
3443
3444/*
3445 * ":1,25call func(arg1, arg2)" function call.
3446 */
3447 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003448ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449{
3450 char_u *arg = eap->arg;
3451 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003453 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003455 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 linenr_T lnum;
3457 int doesrange;
3458 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003459 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003461 if (eap->skip)
3462 {
3463 /* trans_function_name() doesn't work well when skipping, use eval0()
3464 * instead to skip to any following command, e.g. for:
3465 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003466 ++emsg_skip;
3467 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3468 clear_tv(&rettv);
3469 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003470 return;
3471 }
3472
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003473 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003474 if (fudi.fd_newkey != NULL)
3475 {
3476 /* Still need to give an error message for missing key. */
3477 EMSG2(_(e_dictkey), fudi.fd_newkey);
3478 vim_free(fudi.fd_newkey);
3479 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003480 if (tofree == NULL)
3481 return;
3482
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003483 /* Increase refcount on dictionary, it could get deleted when evaluating
3484 * the arguments. */
3485 if (fudi.fd_dict != NULL)
3486 ++fudi.fd_dict->dv_refcount;
3487
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003488 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003489 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003490 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491
Bram Moolenaar532c7802005-01-27 14:44:31 +00003492 /* Skip white space to allow ":call func ()". Not good, but required for
3493 * backward compatibility. */
3494 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003495 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496
3497 if (*startarg != '(')
3498 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003499 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 goto end;
3501 }
3502
3503 /*
3504 * When skipping, evaluate the function once, to find the end of the
3505 * arguments.
3506 * When the function takes a range, this is discovered after the first
3507 * call, and the loop is broken.
3508 */
3509 if (eap->skip)
3510 {
3511 ++emsg_skip;
3512 lnum = eap->line2; /* do it once, also with an invalid range */
3513 }
3514 else
3515 lnum = eap->line1;
3516 for ( ; lnum <= eap->line2; ++lnum)
3517 {
3518 if (!eap->skip && eap->addr_count > 0)
3519 {
3520 curwin->w_cursor.lnum = lnum;
3521 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003522#ifdef FEAT_VIRTUALEDIT
3523 curwin->w_cursor.coladd = 0;
3524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 }
3526 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003527 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003528 eap->line1, eap->line2, &doesrange,
3529 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 {
3531 failed = TRUE;
3532 break;
3533 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003534
3535 /* Handle a function returning a Funcref, Dictionary or List. */
3536 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3537 {
3538 failed = TRUE;
3539 break;
3540 }
3541
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003542 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 if (doesrange || eap->skip)
3544 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003545
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003547 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003548 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003549 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 if (aborting())
3551 break;
3552 }
3553 if (eap->skip)
3554 --emsg_skip;
3555
3556 if (!failed)
3557 {
3558 /* Check for trailing illegal characters and a following command. */
3559 if (!ends_excmd(*arg))
3560 {
3561 emsg_severe = TRUE;
3562 EMSG(_(e_trailing));
3563 }
3564 else
3565 eap->nextcmd = check_nextcmd(arg);
3566 }
3567
3568end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003569 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003570 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571}
3572
3573/*
3574 * ":unlet[!] var1 ... " command.
3575 */
3576 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003577ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003579 ex_unletlock(eap, eap->arg, 0);
3580}
3581
3582/*
3583 * ":lockvar" and ":unlockvar" commands
3584 */
3585 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003586ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003587{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003589 int deep = 2;
3590
3591 if (eap->forceit)
3592 deep = -1;
3593 else if (vim_isdigit(*arg))
3594 {
3595 deep = getdigits(&arg);
3596 arg = skipwhite(arg);
3597 }
3598
3599 ex_unletlock(eap, arg, deep);
3600}
3601
3602/*
3603 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3604 */
3605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003606ex_unletlock(
3607 exarg_T *eap,
3608 char_u *argstart,
3609 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003610{
3611 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003614 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615
3616 do
3617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003619 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003620 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003621 if (lv.ll_name == NULL)
3622 error = TRUE; /* error but continue parsing */
3623 if (name_end == NULL || (!vim_iswhite(*name_end)
3624 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003626 if (name_end != NULL)
3627 {
3628 emsg_severe = TRUE;
3629 EMSG(_(e_trailing));
3630 }
3631 if (!(eap->skip || error))
3632 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 break;
3634 }
3635
3636 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003637 {
3638 if (eap->cmdidx == CMD_unlet)
3639 {
3640 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3641 error = TRUE;
3642 }
3643 else
3644 {
3645 if (do_lock_var(&lv, name_end, deep,
3646 eap->cmdidx == CMD_lockvar) == FAIL)
3647 error = TRUE;
3648 }
3649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 if (!eap->skip)
3652 clear_lval(&lv);
3653
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 arg = skipwhite(name_end);
3655 } while (!ends_excmd(*arg));
3656
3657 eap->nextcmd = check_nextcmd(arg);
3658}
3659
Bram Moolenaar8c711452005-01-14 21:53:12 +00003660 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003661do_unlet_var(
3662 lval_T *lp,
3663 char_u *name_end,
3664 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003665{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003666 int ret = OK;
3667 int cc;
3668
3669 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003670 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003671 cc = *name_end;
3672 *name_end = NUL;
3673
3674 /* Normal name or expanded name. */
3675 if (check_changedtick(lp->ll_name))
3676 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003677 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003678 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003680 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003681 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003682 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003683 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003684 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003685 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003686 else if (lp->ll_range)
3687 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003688 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003689 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003690 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003691
3692 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3693 {
3694 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003695 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003696 return FAIL;
3697 ll_li = li;
3698 ++ll_n1;
3699 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003700
3701 /* Delete a range of List items. */
3702 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3703 {
3704 li = lp->ll_li->li_next;
3705 listitem_remove(lp->ll_list, lp->ll_li);
3706 lp->ll_li = li;
3707 ++lp->ll_n1;
3708 }
3709 }
3710 else
3711 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003712 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 /* unlet a List item. */
3714 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003716 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003717 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003718 }
3719
3720 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003721}
3722
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723/*
3724 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003725 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 */
3727 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003728do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729{
Bram Moolenaar33570922005-01-25 22:26:29 +00003730 hashtab_T *ht;
3731 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003732 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003733 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003734 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735
Bram Moolenaar33570922005-01-25 22:26:29 +00003736 ht = find_var_ht(name, &varname);
3737 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003739 if (ht == &globvarht)
3740 d = &globvardict;
3741 else if (current_funccal != NULL
3742 && ht == &current_funccal->l_vars.dv_hashtab)
3743 d = &current_funccal->l_vars;
3744 else if (ht == &compat_hashtab)
3745 d = &vimvardict;
3746 else
3747 {
3748 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3749 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3750 }
3751 if (d == NULL)
3752 {
3753 EMSG2(_(e_intern2), "do_unlet()");
3754 return FAIL;
3755 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003756 hi = hash_find(ht, varname);
3757 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003758 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003759 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003760 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003761 || var_check_ro(di->di_flags, name, FALSE)
3762 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003763 return FAIL;
3764
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003765 delete_var(ht, hi);
3766 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003769 if (forceit)
3770 return OK;
3771 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 return FAIL;
3773}
3774
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003775/*
3776 * Lock or unlock variable indicated by "lp".
3777 * "deep" is the levels to go (-1 for unlimited);
3778 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3779 */
3780 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003781do_lock_var(
3782 lval_T *lp,
3783 char_u *name_end,
3784 int deep,
3785 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003786{
3787 int ret = OK;
3788 int cc;
3789 dictitem_T *di;
3790
3791 if (deep == 0) /* nothing to do */
3792 return OK;
3793
3794 if (lp->ll_tv == NULL)
3795 {
3796 cc = *name_end;
3797 *name_end = NUL;
3798
3799 /* Normal name or expanded name. */
3800 if (check_changedtick(lp->ll_name))
3801 ret = FAIL;
3802 else
3803 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003804 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003805 if (di == NULL)
3806 ret = FAIL;
3807 else
3808 {
3809 if (lock)
3810 di->di_flags |= DI_FLAGS_LOCK;
3811 else
3812 di->di_flags &= ~DI_FLAGS_LOCK;
3813 item_lock(&di->di_tv, deep, lock);
3814 }
3815 }
3816 *name_end = cc;
3817 }
3818 else if (lp->ll_range)
3819 {
3820 listitem_T *li = lp->ll_li;
3821
3822 /* (un)lock a range of List items. */
3823 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3824 {
3825 item_lock(&li->li_tv, deep, lock);
3826 li = li->li_next;
3827 ++lp->ll_n1;
3828 }
3829 }
3830 else if (lp->ll_list != NULL)
3831 /* (un)lock a List item. */
3832 item_lock(&lp->ll_li->li_tv, deep, lock);
3833 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003834 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003835 item_lock(&lp->ll_di->di_tv, deep, lock);
3836
3837 return ret;
3838}
3839
3840/*
3841 * Lock or unlock an item. "deep" is nr of levels to go.
3842 */
3843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003844item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003845{
3846 static int recurse = 0;
3847 list_T *l;
3848 listitem_T *li;
3849 dict_T *d;
3850 hashitem_T *hi;
3851 int todo;
3852
3853 if (recurse >= DICT_MAXNEST)
3854 {
3855 EMSG(_("E743: variable nested too deep for (un)lock"));
3856 return;
3857 }
3858 if (deep == 0)
3859 return;
3860 ++recurse;
3861
3862 /* lock/unlock the item itself */
3863 if (lock)
3864 tv->v_lock |= VAR_LOCKED;
3865 else
3866 tv->v_lock &= ~VAR_LOCKED;
3867
3868 switch (tv->v_type)
3869 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003870 case VAR_UNKNOWN:
3871 case VAR_NUMBER:
3872 case VAR_STRING:
3873 case VAR_FUNC:
3874 case VAR_FLOAT:
3875 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003876 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003877 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003878 break;
3879
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003880 case VAR_LIST:
3881 if ((l = tv->vval.v_list) != NULL)
3882 {
3883 if (lock)
3884 l->lv_lock |= VAR_LOCKED;
3885 else
3886 l->lv_lock &= ~VAR_LOCKED;
3887 if (deep < 0 || deep > 1)
3888 /* recursive: lock/unlock the items the List contains */
3889 for (li = l->lv_first; li != NULL; li = li->li_next)
3890 item_lock(&li->li_tv, deep - 1, lock);
3891 }
3892 break;
3893 case VAR_DICT:
3894 if ((d = tv->vval.v_dict) != NULL)
3895 {
3896 if (lock)
3897 d->dv_lock |= VAR_LOCKED;
3898 else
3899 d->dv_lock &= ~VAR_LOCKED;
3900 if (deep < 0 || deep > 1)
3901 {
3902 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003903 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003904 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3905 {
3906 if (!HASHITEM_EMPTY(hi))
3907 {
3908 --todo;
3909 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3910 }
3911 }
3912 }
3913 }
3914 }
3915 --recurse;
3916}
3917
Bram Moolenaara40058a2005-07-11 22:42:07 +00003918/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003919 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3920 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003921 */
3922 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003923tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003924{
3925 return (tv->v_lock & VAR_LOCKED)
3926 || (tv->v_type == VAR_LIST
3927 && tv->vval.v_list != NULL
3928 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3929 || (tv->v_type == VAR_DICT
3930 && tv->vval.v_dict != NULL
3931 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3932}
3933
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3935/*
3936 * Delete all "menutrans_" variables.
3937 */
3938 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003939del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940{
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003942 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003945 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003946 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003947 {
3948 if (!HASHITEM_EMPTY(hi))
3949 {
3950 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003951 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3952 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003953 }
3954 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003955 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956}
3957#endif
3958
3959#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3960
3961/*
3962 * Local string buffer for the next two functions to store a variable name
3963 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3964 * get_user_var_name().
3965 */
3966
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003967static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968
3969static char_u *varnamebuf = NULL;
3970static int varnamebuflen = 0;
3971
3972/*
3973 * Function to concatenate a prefix and a variable name.
3974 */
3975 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003976cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977{
3978 int len;
3979
3980 len = (int)STRLEN(name) + 3;
3981 if (len > varnamebuflen)
3982 {
3983 vim_free(varnamebuf);
3984 len += 10; /* some additional space */
3985 varnamebuf = alloc(len);
3986 if (varnamebuf == NULL)
3987 {
3988 varnamebuflen = 0;
3989 return NULL;
3990 }
3991 varnamebuflen = len;
3992 }
3993 *varnamebuf = prefix;
3994 varnamebuf[1] = ':';
3995 STRCPY(varnamebuf + 2, name);
3996 return varnamebuf;
3997}
3998
3999/*
4000 * Function given to ExpandGeneric() to obtain the list of user defined
4001 * (global/buffer/window/built-in) variable names.
4002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004004get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004006 static long_u gdone;
4007 static long_u bdone;
4008 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004009#ifdef FEAT_WINDOWS
4010 static long_u tdone;
4011#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004012 static int vidx;
4013 static hashitem_T *hi;
4014 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015
4016 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004017 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004018 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004019#ifdef FEAT_WINDOWS
4020 tdone = 0;
4021#endif
4022 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004023
4024 /* Global variables */
4025 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004027 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004028 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004029 else
4030 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004031 while (HASHITEM_EMPTY(hi))
4032 ++hi;
4033 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4034 return cat_prefix_varname('g', hi->hi_key);
4035 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004037
4038 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004039 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004040 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004042 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004043 hi = ht->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 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004050 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004052 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 return (char_u *)"b:changedtick";
4054 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004055
4056 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004057 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004058 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004060 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004061 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004062 else
4063 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004064 while (HASHITEM_EMPTY(hi))
4065 ++hi;
4066 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004068
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004069#ifdef FEAT_WINDOWS
4070 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004071 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004072 if (tdone < ht->ht_used)
4073 {
4074 if (tdone++ == 0)
4075 hi = ht->ht_array;
4076 else
4077 ++hi;
4078 while (HASHITEM_EMPTY(hi))
4079 ++hi;
4080 return cat_prefix_varname('t', hi->hi_key);
4081 }
4082#endif
4083
Bram Moolenaar33570922005-01-25 22:26:29 +00004084 /* v: variables */
4085 if (vidx < VV_LEN)
4086 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087
4088 vim_free(varnamebuf);
4089 varnamebuf = NULL;
4090 varnamebuflen = 0;
4091 return NULL;
4092}
4093
4094#endif /* FEAT_CMDL_COMPL */
4095
4096/*
4097 * types for expressions.
4098 */
4099typedef enum
4100{
4101 TYPE_UNKNOWN = 0
4102 , TYPE_EQUAL /* == */
4103 , TYPE_NEQUAL /* != */
4104 , TYPE_GREATER /* > */
4105 , TYPE_GEQUAL /* >= */
4106 , TYPE_SMALLER /* < */
4107 , TYPE_SEQUAL /* <= */
4108 , TYPE_MATCH /* =~ */
4109 , TYPE_NOMATCH /* !~ */
4110} exptype_T;
4111
4112/*
4113 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004114 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4116 */
4117
4118/*
4119 * Handle zero level expression.
4120 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004122 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 * Return OK or FAIL.
4124 */
4125 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004126eval0(
4127 char_u *arg,
4128 typval_T *rettv,
4129 char_u **nextcmd,
4130 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131{
4132 int ret;
4133 char_u *p;
4134
4135 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004136 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 if (ret == FAIL || !ends_excmd(*p))
4138 {
4139 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 /*
4142 * Report the invalid expression unless the expression evaluation has
4143 * been cancelled due to an aborting error, an interrupt, or an
4144 * exception.
4145 */
4146 if (!aborting())
4147 EMSG2(_(e_invexpr2), arg);
4148 ret = FAIL;
4149 }
4150 if (nextcmd != NULL)
4151 *nextcmd = check_nextcmd(p);
4152
4153 return ret;
4154}
4155
4156/*
4157 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004158 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 *
4160 * "arg" must point to the first non-white of the expression.
4161 * "arg" is advanced to the next non-white after the recognized expression.
4162 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004163 * Note: "rettv.v_lock" is not set.
4164 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 * Return OK or FAIL.
4166 */
4167 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004168eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169{
4170 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004171 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172
4173 /*
4174 * Get the first variable.
4175 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 return FAIL;
4178
4179 if ((*arg)[0] == '?')
4180 {
4181 result = FALSE;
4182 if (evaluate)
4183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004184 int error = FALSE;
4185
4186 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004188 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004189 if (error)
4190 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 }
4192
4193 /*
4194 * Get the second variable.
4195 */
4196 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 return FAIL;
4199
4200 /*
4201 * Check for the ":".
4202 */
4203 if ((*arg)[0] != ':')
4204 {
4205 EMSG(_("E109: Missing ':' after '?'"));
4206 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004207 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 return FAIL;
4209 }
4210
4211 /*
4212 * Get the third variable.
4213 */
4214 *arg = skipwhite(*arg + 1);
4215 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4216 {
4217 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004218 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 return FAIL;
4220 }
4221 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004222 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 }
4224
4225 return OK;
4226}
4227
4228/*
4229 * Handle first level expression:
4230 * expr2 || expr2 || expr2 logical OR
4231 *
4232 * "arg" must point to the first non-white of the expression.
4233 * "arg" is advanced to the next non-white after the recognized expression.
4234 *
4235 * Return OK or FAIL.
4236 */
4237 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004238eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239{
Bram Moolenaar33570922005-01-25 22:26:29 +00004240 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 long result;
4242 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
4245 /*
4246 * Get the first variable.
4247 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004248 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 return FAIL;
4250
4251 /*
4252 * Repeat until there is no following "||".
4253 */
4254 first = TRUE;
4255 result = FALSE;
4256 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4257 {
4258 if (evaluate && first)
4259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004260 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004262 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004263 if (error)
4264 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 first = FALSE;
4266 }
4267
4268 /*
4269 * Get the second variable.
4270 */
4271 *arg = skipwhite(*arg + 2);
4272 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4273 return FAIL;
4274
4275 /*
4276 * Compute the result.
4277 */
4278 if (evaluate && !result)
4279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004280 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004283 if (error)
4284 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286 if (evaluate)
4287 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 rettv->v_type = VAR_NUMBER;
4289 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 }
4291 }
4292
4293 return OK;
4294}
4295
4296/*
4297 * Handle second level expression:
4298 * expr3 && expr3 && expr3 logical AND
4299 *
4300 * "arg" must point to the first non-white of the expression.
4301 * "arg" is advanced to the next non-white after the recognized expression.
4302 *
4303 * Return OK or FAIL.
4304 */
4305 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004306eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307{
Bram Moolenaar33570922005-01-25 22:26:29 +00004308 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 long result;
4310 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004311 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312
4313 /*
4314 * Get the first variable.
4315 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004316 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 return FAIL;
4318
4319 /*
4320 * Repeat until there is no following "&&".
4321 */
4322 first = TRUE;
4323 result = TRUE;
4324 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4325 {
4326 if (evaluate && first)
4327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004328 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004330 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004331 if (error)
4332 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 first = FALSE;
4334 }
4335
4336 /*
4337 * Get the second variable.
4338 */
4339 *arg = skipwhite(*arg + 2);
4340 if (eval4(arg, &var2, evaluate && result) == FAIL)
4341 return FAIL;
4342
4343 /*
4344 * Compute the result.
4345 */
4346 if (evaluate && result)
4347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004348 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004350 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004351 if (error)
4352 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 }
4354 if (evaluate)
4355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004356 rettv->v_type = VAR_NUMBER;
4357 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
4359 }
4360
4361 return OK;
4362}
4363
4364/*
4365 * Handle third level expression:
4366 * var1 == var2
4367 * var1 =~ var2
4368 * var1 != var2
4369 * var1 !~ var2
4370 * var1 > var2
4371 * var1 >= var2
4372 * var1 < var2
4373 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004374 * var1 is var2
4375 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 *
4377 * "arg" must point to the first non-white of the expression.
4378 * "arg" is advanced to the next non-white after the recognized expression.
4379 *
4380 * Return OK or FAIL.
4381 */
4382 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004383eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384{
Bram Moolenaar33570922005-01-25 22:26:29 +00004385 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 char_u *p;
4387 int i;
4388 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004389 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 int len = 2;
4391 long n1, n2;
4392 char_u *s1, *s2;
4393 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4394 regmatch_T regmatch;
4395 int ic;
4396 char_u *save_cpo;
4397
4398 /*
4399 * Get the first variable.
4400 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004401 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 return FAIL;
4403
4404 p = *arg;
4405 switch (p[0])
4406 {
4407 case '=': if (p[1] == '=')
4408 type = TYPE_EQUAL;
4409 else if (p[1] == '~')
4410 type = TYPE_MATCH;
4411 break;
4412 case '!': if (p[1] == '=')
4413 type = TYPE_NEQUAL;
4414 else if (p[1] == '~')
4415 type = TYPE_NOMATCH;
4416 break;
4417 case '>': if (p[1] != '=')
4418 {
4419 type = TYPE_GREATER;
4420 len = 1;
4421 }
4422 else
4423 type = TYPE_GEQUAL;
4424 break;
4425 case '<': if (p[1] != '=')
4426 {
4427 type = TYPE_SMALLER;
4428 len = 1;
4429 }
4430 else
4431 type = TYPE_SEQUAL;
4432 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004433 case 'i': if (p[1] == 's')
4434 {
4435 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4436 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004437 i = p[len];
4438 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004439 {
4440 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4441 type_is = TRUE;
4442 }
4443 }
4444 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 }
4446
4447 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004448 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 */
4450 if (type != TYPE_UNKNOWN)
4451 {
4452 /* extra question mark appended: ignore case */
4453 if (p[len] == '?')
4454 {
4455 ic = TRUE;
4456 ++len;
4457 }
4458 /* extra '#' appended: match case */
4459 else if (p[len] == '#')
4460 {
4461 ic = FALSE;
4462 ++len;
4463 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004464 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 else
4466 ic = p_ic;
4467
4468 /*
4469 * Get the second variable.
4470 */
4471 *arg = skipwhite(p + len);
4472 if (eval5(arg, &var2, evaluate) == FAIL)
4473 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004474 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 return FAIL;
4476 }
4477
4478 if (evaluate)
4479 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004480 if (type_is && rettv->v_type != var2.v_type)
4481 {
4482 /* For "is" a different type always means FALSE, for "notis"
4483 * it means TRUE. */
4484 n1 = (type == TYPE_NEQUAL);
4485 }
4486 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4487 {
4488 if (type_is)
4489 {
4490 n1 = (rettv->v_type == var2.v_type
4491 && rettv->vval.v_list == var2.vval.v_list);
4492 if (type == TYPE_NEQUAL)
4493 n1 = !n1;
4494 }
4495 else if (rettv->v_type != var2.v_type
4496 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4497 {
4498 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004499 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004500 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004501 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004502 clear_tv(rettv);
4503 clear_tv(&var2);
4504 return FAIL;
4505 }
4506 else
4507 {
4508 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004509 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4510 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004511 if (type == TYPE_NEQUAL)
4512 n1 = !n1;
4513 }
4514 }
4515
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004516 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4517 {
4518 if (type_is)
4519 {
4520 n1 = (rettv->v_type == var2.v_type
4521 && rettv->vval.v_dict == var2.vval.v_dict);
4522 if (type == TYPE_NEQUAL)
4523 n1 = !n1;
4524 }
4525 else if (rettv->v_type != var2.v_type
4526 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4527 {
4528 if (rettv->v_type != var2.v_type)
4529 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4530 else
4531 EMSG(_("E736: Invalid operation for Dictionary"));
4532 clear_tv(rettv);
4533 clear_tv(&var2);
4534 return FAIL;
4535 }
4536 else
4537 {
4538 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004539 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4540 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004541 if (type == TYPE_NEQUAL)
4542 n1 = !n1;
4543 }
4544 }
4545
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004546 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4547 {
4548 if (rettv->v_type != var2.v_type
4549 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4550 {
4551 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004552 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004553 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004554 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004555 clear_tv(rettv);
4556 clear_tv(&var2);
4557 return FAIL;
4558 }
4559 else
4560 {
4561 /* Compare two Funcrefs for being equal or unequal. */
4562 if (rettv->vval.v_string == NULL
4563 || var2.vval.v_string == NULL)
4564 n1 = FALSE;
4565 else
4566 n1 = STRCMP(rettv->vval.v_string,
4567 var2.vval.v_string) == 0;
4568 if (type == TYPE_NEQUAL)
4569 n1 = !n1;
4570 }
4571 }
4572
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004573#ifdef FEAT_FLOAT
4574 /*
4575 * If one of the two variables is a float, compare as a float.
4576 * When using "=~" or "!~", always compare as string.
4577 */
4578 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4579 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4580 {
4581 float_T f1, f2;
4582
4583 if (rettv->v_type == VAR_FLOAT)
4584 f1 = rettv->vval.v_float;
4585 else
4586 f1 = get_tv_number(rettv);
4587 if (var2.v_type == VAR_FLOAT)
4588 f2 = var2.vval.v_float;
4589 else
4590 f2 = get_tv_number(&var2);
4591 n1 = FALSE;
4592 switch (type)
4593 {
4594 case TYPE_EQUAL: n1 = (f1 == f2); break;
4595 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4596 case TYPE_GREATER: n1 = (f1 > f2); break;
4597 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4598 case TYPE_SMALLER: n1 = (f1 < f2); break;
4599 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4600 case TYPE_UNKNOWN:
4601 case TYPE_MATCH:
4602 case TYPE_NOMATCH: break; /* avoid gcc warning */
4603 }
4604 }
4605#endif
4606
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 /*
4608 * If one of the two variables is a number, compare as a number.
4609 * When using "=~" or "!~", always compare as string.
4610 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004611 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4613 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004614 n1 = get_tv_number(rettv);
4615 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 switch (type)
4617 {
4618 case TYPE_EQUAL: n1 = (n1 == n2); break;
4619 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4620 case TYPE_GREATER: n1 = (n1 > n2); break;
4621 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4622 case TYPE_SMALLER: n1 = (n1 < n2); break;
4623 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4624 case TYPE_UNKNOWN:
4625 case TYPE_MATCH:
4626 case TYPE_NOMATCH: break; /* avoid gcc warning */
4627 }
4628 }
4629 else
4630 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004631 s1 = get_tv_string_buf(rettv, buf1);
4632 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4634 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4635 else
4636 i = 0;
4637 n1 = FALSE;
4638 switch (type)
4639 {
4640 case TYPE_EQUAL: n1 = (i == 0); break;
4641 case TYPE_NEQUAL: n1 = (i != 0); break;
4642 case TYPE_GREATER: n1 = (i > 0); break;
4643 case TYPE_GEQUAL: n1 = (i >= 0); break;
4644 case TYPE_SMALLER: n1 = (i < 0); break;
4645 case TYPE_SEQUAL: n1 = (i <= 0); break;
4646
4647 case TYPE_MATCH:
4648 case TYPE_NOMATCH:
4649 /* avoid 'l' flag in 'cpoptions' */
4650 save_cpo = p_cpo;
4651 p_cpo = (char_u *)"";
4652 regmatch.regprog = vim_regcomp(s2,
4653 RE_MAGIC + RE_STRING);
4654 regmatch.rm_ic = ic;
4655 if (regmatch.regprog != NULL)
4656 {
4657 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004658 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 if (type == TYPE_NOMATCH)
4660 n1 = !n1;
4661 }
4662 p_cpo = save_cpo;
4663 break;
4664
4665 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4666 }
4667 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004668 clear_tv(rettv);
4669 clear_tv(&var2);
4670 rettv->v_type = VAR_NUMBER;
4671 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 }
4673 }
4674
4675 return OK;
4676}
4677
4678/*
4679 * Handle fourth level expression:
4680 * + number addition
4681 * - number subtraction
4682 * . string concatenation
4683 *
4684 * "arg" must point to the first non-white of the expression.
4685 * "arg" is advanced to the next non-white after the recognized expression.
4686 *
4687 * Return OK or FAIL.
4688 */
4689 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004690eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691{
Bram Moolenaar33570922005-01-25 22:26:29 +00004692 typval_T var2;
4693 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 int op;
4695 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004696#ifdef FEAT_FLOAT
4697 float_T f1 = 0, f2 = 0;
4698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 char_u *s1, *s2;
4700 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4701 char_u *p;
4702
4703 /*
4704 * Get the first variable.
4705 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004706 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 return FAIL;
4708
4709 /*
4710 * Repeat computing, until no '+', '-' or '.' is following.
4711 */
4712 for (;;)
4713 {
4714 op = **arg;
4715 if (op != '+' && op != '-' && op != '.')
4716 break;
4717
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004718 if ((op != '+' || rettv->v_type != VAR_LIST)
4719#ifdef FEAT_FLOAT
4720 && (op == '.' || rettv->v_type != VAR_FLOAT)
4721#endif
4722 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004723 {
4724 /* For "list + ...", an illegal use of the first operand as
4725 * a number cannot be determined before evaluating the 2nd
4726 * operand: if this is also a list, all is ok.
4727 * For "something . ...", "something - ..." or "non-list + ...",
4728 * we know that the first operand needs to be a string or number
4729 * without evaluating the 2nd operand. So check before to avoid
4730 * side effects after an error. */
4731 if (evaluate && get_tv_string_chk(rettv) == NULL)
4732 {
4733 clear_tv(rettv);
4734 return FAIL;
4735 }
4736 }
4737
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 /*
4739 * Get the second variable.
4740 */
4741 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004742 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004744 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 return FAIL;
4746 }
4747
4748 if (evaluate)
4749 {
4750 /*
4751 * Compute the result.
4752 */
4753 if (op == '.')
4754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004755 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4756 s2 = get_tv_string_buf_chk(&var2, buf2);
4757 if (s2 == NULL) /* type error ? */
4758 {
4759 clear_tv(rettv);
4760 clear_tv(&var2);
4761 return FAIL;
4762 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004763 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004764 clear_tv(rettv);
4765 rettv->v_type = VAR_STRING;
4766 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004768 else if (op == '+' && rettv->v_type == VAR_LIST
4769 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004770 {
4771 /* concatenate Lists */
4772 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4773 &var3) == FAIL)
4774 {
4775 clear_tv(rettv);
4776 clear_tv(&var2);
4777 return FAIL;
4778 }
4779 clear_tv(rettv);
4780 *rettv = var3;
4781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 else
4783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004784 int error = FALSE;
4785
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786#ifdef FEAT_FLOAT
4787 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004788 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004789 f1 = rettv->vval.v_float;
4790 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004791 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004792 else
4793#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004794 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004795 n1 = get_tv_number_chk(rettv, &error);
4796 if (error)
4797 {
4798 /* This can only happen for "list + non-list". For
4799 * "non-list + ..." or "something - ...", we returned
4800 * before evaluating the 2nd operand. */
4801 clear_tv(rettv);
4802 return FAIL;
4803 }
4804#ifdef FEAT_FLOAT
4805 if (var2.v_type == VAR_FLOAT)
4806 f1 = n1;
4807#endif
4808 }
4809#ifdef FEAT_FLOAT
4810 if (var2.v_type == VAR_FLOAT)
4811 {
4812 f2 = var2.vval.v_float;
4813 n2 = 0;
4814 }
4815 else
4816#endif
4817 {
4818 n2 = get_tv_number_chk(&var2, &error);
4819 if (error)
4820 {
4821 clear_tv(rettv);
4822 clear_tv(&var2);
4823 return FAIL;
4824 }
4825#ifdef FEAT_FLOAT
4826 if (rettv->v_type == VAR_FLOAT)
4827 f2 = n2;
4828#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004829 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004830 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004831
4832#ifdef FEAT_FLOAT
4833 /* If there is a float on either side the result is a float. */
4834 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4835 {
4836 if (op == '+')
4837 f1 = f1 + f2;
4838 else
4839 f1 = f1 - f2;
4840 rettv->v_type = VAR_FLOAT;
4841 rettv->vval.v_float = f1;
4842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004844#endif
4845 {
4846 if (op == '+')
4847 n1 = n1 + n2;
4848 else
4849 n1 = n1 - n2;
4850 rettv->v_type = VAR_NUMBER;
4851 rettv->vval.v_number = n1;
4852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004854 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
4856 }
4857 return OK;
4858}
4859
4860/*
4861 * Handle fifth level expression:
4862 * * number multiplication
4863 * / number division
4864 * % number modulo
4865 *
4866 * "arg" must point to the first non-white of the expression.
4867 * "arg" is advanced to the next non-white after the recognized expression.
4868 *
4869 * Return OK or FAIL.
4870 */
4871 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004872eval6(
4873 char_u **arg,
4874 typval_T *rettv,
4875 int evaluate,
4876 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877{
Bram Moolenaar33570922005-01-25 22:26:29 +00004878 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 int op;
4880 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004881#ifdef FEAT_FLOAT
4882 int use_float = FALSE;
4883 float_T f1 = 0, f2;
4884#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004885 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886
4887 /*
4888 * Get the first variable.
4889 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004890 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 return FAIL;
4892
4893 /*
4894 * Repeat computing, until no '*', '/' or '%' is following.
4895 */
4896 for (;;)
4897 {
4898 op = **arg;
4899 if (op != '*' && op != '/' && op != '%')
4900 break;
4901
4902 if (evaluate)
4903 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004904#ifdef FEAT_FLOAT
4905 if (rettv->v_type == VAR_FLOAT)
4906 {
4907 f1 = rettv->vval.v_float;
4908 use_float = TRUE;
4909 n1 = 0;
4910 }
4911 else
4912#endif
4913 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004914 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004915 if (error)
4916 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918 else
4919 n1 = 0;
4920
4921 /*
4922 * Get the second variable.
4923 */
4924 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004925 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 return FAIL;
4927
4928 if (evaluate)
4929 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004930#ifdef FEAT_FLOAT
4931 if (var2.v_type == VAR_FLOAT)
4932 {
4933 if (!use_float)
4934 {
4935 f1 = n1;
4936 use_float = TRUE;
4937 }
4938 f2 = var2.vval.v_float;
4939 n2 = 0;
4940 }
4941 else
4942#endif
4943 {
4944 n2 = get_tv_number_chk(&var2, &error);
4945 clear_tv(&var2);
4946 if (error)
4947 return FAIL;
4948#ifdef FEAT_FLOAT
4949 if (use_float)
4950 f2 = n2;
4951#endif
4952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953
4954 /*
4955 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004956 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004958#ifdef FEAT_FLOAT
4959 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004961 if (op == '*')
4962 f1 = f1 * f2;
4963 else if (op == '/')
4964 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004965# ifdef VMS
4966 /* VMS crashes on divide by zero, work around it */
4967 if (f2 == 0.0)
4968 {
4969 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004970 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004971 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004972 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004973 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004974 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004975 }
4976 else
4977 f1 = f1 / f2;
4978# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004979 /* We rely on the floating point library to handle divide
4980 * by zero to result in "inf" and not a crash. */
4981 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004982# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004985 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004986 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004987 return FAIL;
4988 }
4989 rettv->v_type = VAR_FLOAT;
4990 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
4992 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004995 if (op == '*')
4996 n1 = n1 * n2;
4997 else if (op == '/')
4998 {
4999 if (n2 == 0) /* give an error message? */
5000 {
5001 if (n1 == 0)
5002 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5003 else if (n1 < 0)
5004 n1 = -0x7fffffffL;
5005 else
5006 n1 = 0x7fffffffL;
5007 }
5008 else
5009 n1 = n1 / n2;
5010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005012 {
5013 if (n2 == 0) /* give an error message? */
5014 n1 = 0;
5015 else
5016 n1 = n1 % n2;
5017 }
5018 rettv->v_type = VAR_NUMBER;
5019 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
5022 }
5023
5024 return OK;
5025}
5026
5027/*
5028 * Handle sixth level expression:
5029 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005030 * "string" string constant
5031 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 * &option-name option value
5033 * @r register contents
5034 * identifier variable value
5035 * function() function call
5036 * $VAR environment variable
5037 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005038 * [expr, expr] List
5039 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 *
5041 * Also handle:
5042 * ! in front logical NOT
5043 * - in front unary minus
5044 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005045 * trailing [] subscript in String or List
5046 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 *
5048 * "arg" must point to the first non-white of the expression.
5049 * "arg" is advanced to the next non-white after the recognized expression.
5050 *
5051 * Return OK or FAIL.
5052 */
5053 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005054eval7(
5055 char_u **arg,
5056 typval_T *rettv,
5057 int evaluate,
5058 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 long n;
5061 int len;
5062 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 char_u *start_leader, *end_leader;
5064 int ret = OK;
5065 char_u *alias;
5066
5067 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005068 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005069 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005071 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
5073 /*
5074 * Skip '!' and '-' characters. They are handled later.
5075 */
5076 start_leader = *arg;
5077 while (**arg == '!' || **arg == '-' || **arg == '+')
5078 *arg = skipwhite(*arg + 1);
5079 end_leader = *arg;
5080
5081 switch (**arg)
5082 {
5083 /*
5084 * Number constant.
5085 */
5086 case '0':
5087 case '1':
5088 case '2':
5089 case '3':
5090 case '4':
5091 case '5':
5092 case '6':
5093 case '7':
5094 case '8':
5095 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005096 {
5097#ifdef FEAT_FLOAT
5098 char_u *p = skipdigits(*arg + 1);
5099 int get_float = FALSE;
5100
5101 /* We accept a float when the format matches
5102 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005103 * strict to avoid backwards compatibility problems.
5104 * Don't look for a float after the "." operator, so that
5105 * ":let vers = 1.2.3" doesn't fail. */
5106 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005108 get_float = TRUE;
5109 p = skipdigits(p + 2);
5110 if (*p == 'e' || *p == 'E')
5111 {
5112 ++p;
5113 if (*p == '-' || *p == '+')
5114 ++p;
5115 if (!vim_isdigit(*p))
5116 get_float = FALSE;
5117 else
5118 p = skipdigits(p + 1);
5119 }
5120 if (ASCII_ISALPHA(*p) || *p == '.')
5121 get_float = FALSE;
5122 }
5123 if (get_float)
5124 {
5125 float_T f;
5126
5127 *arg += string2float(*arg, &f);
5128 if (evaluate)
5129 {
5130 rettv->v_type = VAR_FLOAT;
5131 rettv->vval.v_float = f;
5132 }
5133 }
5134 else
5135#endif
5136 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005137 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005138 *arg += len;
5139 if (evaluate)
5140 {
5141 rettv->v_type = VAR_NUMBER;
5142 rettv->vval.v_number = n;
5143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 }
5145 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147
5148 /*
5149 * String constant: "string".
5150 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005151 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 break;
5153
5154 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005157 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005158 break;
5159
5160 /*
5161 * List: [expr, expr]
5162 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005163 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 break;
5165
5166 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 * Dictionary: {key: val, key: val}
5168 */
5169 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5170 break;
5171
5172 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005173 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005175 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 break;
5177
5178 /*
5179 * Environment variable: $VAR.
5180 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005181 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 break;
5183
5184 /*
5185 * Register contents: @r.
5186 */
5187 case '@': ++*arg;
5188 if (evaluate)
5189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005190 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005191 rettv->vval.v_string = get_reg_contents(**arg,
5192 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 }
5194 if (**arg != NUL)
5195 ++*arg;
5196 break;
5197
5198 /*
5199 * nested expression: (expression).
5200 */
5201 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005202 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 if (**arg == ')')
5204 ++*arg;
5205 else if (ret == OK)
5206 {
5207 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005208 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 ret = FAIL;
5210 }
5211 break;
5212
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 break;
5215 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216
5217 if (ret == NOTDONE)
5218 {
5219 /*
5220 * Must be a variable or function name.
5221 * Can also be a curly-braces kind of name: {expr}.
5222 */
5223 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005224 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225 if (alias != NULL)
5226 s = alias;
5227
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005228 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 ret = FAIL;
5230 else
5231 {
5232 if (**arg == '(') /* recursive! */
5233 {
5234 /* If "s" is the name of a variable of type VAR_FUNC
5235 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005236 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005237
5238 /* Invoke the function. */
5239 ret = get_func_tv(s, len, rettv, arg,
5240 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005241 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005242
5243 /* If evaluate is FALSE rettv->v_type was not set in
5244 * get_func_tv, but it's needed in handle_subscript() to parse
5245 * what follows. So set it here. */
5246 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5247 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005248 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005249 rettv->v_type = VAR_FUNC;
5250 }
5251
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 /* Stop the expression evaluation when immediately
5253 * aborting on error, or when an interrupt occurred or
5254 * an exception was thrown but not caught. */
5255 if (aborting())
5256 {
5257 if (ret == OK)
5258 clear_tv(rettv);
5259 ret = FAIL;
5260 }
5261 }
5262 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005263 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005264 else
5265 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005267 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 }
5269
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 *arg = skipwhite(*arg);
5271
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005272 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5273 * expr(expr). */
5274 if (ret == OK)
5275 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276
5277 /*
5278 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5279 */
5280 if (ret == OK && evaluate && end_leader > start_leader)
5281 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005282 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005283 int val = 0;
5284#ifdef FEAT_FLOAT
5285 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005286
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005287 if (rettv->v_type == VAR_FLOAT)
5288 f = rettv->vval.v_float;
5289 else
5290#endif
5291 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005292 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005294 clear_tv(rettv);
5295 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005297 else
5298 {
5299 while (end_leader > start_leader)
5300 {
5301 --end_leader;
5302 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005303 {
5304#ifdef FEAT_FLOAT
5305 if (rettv->v_type == VAR_FLOAT)
5306 f = !f;
5307 else
5308#endif
5309 val = !val;
5310 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005311 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005312 {
5313#ifdef FEAT_FLOAT
5314 if (rettv->v_type == VAR_FLOAT)
5315 f = -f;
5316 else
5317#endif
5318 val = -val;
5319 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005320 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005321#ifdef FEAT_FLOAT
5322 if (rettv->v_type == VAR_FLOAT)
5323 {
5324 clear_tv(rettv);
5325 rettv->vval.v_float = f;
5326 }
5327 else
5328#endif
5329 {
5330 clear_tv(rettv);
5331 rettv->v_type = VAR_NUMBER;
5332 rettv->vval.v_number = val;
5333 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 }
5336
5337 return ret;
5338}
5339
5340/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005341 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5342 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5344 */
5345 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005346eval_index(
5347 char_u **arg,
5348 typval_T *rettv,
5349 int evaluate,
5350 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351{
5352 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005353 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355 long len = -1;
5356 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005358 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359
Bram Moolenaara03f2332016-02-06 18:09:59 +01005360 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005362 case VAR_FUNC:
5363 if (verbose)
5364 EMSG(_("E695: Cannot index a Funcref"));
5365 return FAIL;
5366 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005367#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005368 if (verbose)
5369 EMSG(_(e_float_as_string));
5370 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005371#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005372 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005373 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005374 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005375 if (verbose)
5376 EMSG(_("E909: Cannot index a special variable"));
5377 return FAIL;
5378 case VAR_UNKNOWN:
5379 if (evaluate)
5380 return FAIL;
5381 /* FALLTHROUGH */
5382
5383 case VAR_STRING:
5384 case VAR_NUMBER:
5385 case VAR_LIST:
5386 case VAR_DICT:
5387 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005388 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005390 init_tv(&var1);
5391 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005392 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005394 /*
5395 * dict.name
5396 */
5397 key = *arg + 1;
5398 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5399 ;
5400 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005402 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403 }
5404 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005405 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005406 /*
5407 * something[idx]
5408 *
5409 * Get the (first) variable from inside the [].
5410 */
5411 *arg = skipwhite(*arg + 1);
5412 if (**arg == ':')
5413 empty1 = TRUE;
5414 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5415 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005416 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5417 {
5418 /* not a number or string */
5419 clear_tv(&var1);
5420 return FAIL;
5421 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005422
5423 /*
5424 * Get the second variable from inside the [:].
5425 */
5426 if (**arg == ':')
5427 {
5428 range = TRUE;
5429 *arg = skipwhite(*arg + 1);
5430 if (**arg == ']')
5431 empty2 = TRUE;
5432 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005434 if (!empty1)
5435 clear_tv(&var1);
5436 return FAIL;
5437 }
5438 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5439 {
5440 /* not a number or string */
5441 if (!empty1)
5442 clear_tv(&var1);
5443 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005444 return FAIL;
5445 }
5446 }
5447
5448 /* Check for the ']'. */
5449 if (**arg != ']')
5450 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005451 if (verbose)
5452 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005453 clear_tv(&var1);
5454 if (range)
5455 clear_tv(&var2);
5456 return FAIL;
5457 }
5458 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 }
5460
5461 if (evaluate)
5462 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005463 n1 = 0;
5464 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005466 n1 = get_tv_number(&var1);
5467 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 }
5469 if (range)
5470 {
5471 if (empty2)
5472 n2 = -1;
5473 else
5474 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475 n2 = get_tv_number(&var2);
5476 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005477 }
5478 }
5479
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005480 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005481 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005482 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005483 case VAR_FUNC:
5484 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005485 case VAR_SPECIAL:
5486 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005487 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005488 break; /* not evaluating, skipping over subscript */
5489
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490 case VAR_NUMBER:
5491 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005492 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493 len = (long)STRLEN(s);
5494 if (range)
5495 {
5496 /* The resulting variable is a substring. If the indexes
5497 * are out of range the result is empty. */
5498 if (n1 < 0)
5499 {
5500 n1 = len + n1;
5501 if (n1 < 0)
5502 n1 = 0;
5503 }
5504 if (n2 < 0)
5505 n2 = len + n2;
5506 else if (n2 >= len)
5507 n2 = len;
5508 if (n1 >= len || n2 < 0 || n1 > n2)
5509 s = NULL;
5510 else
5511 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5512 }
5513 else
5514 {
5515 /* The resulting variable is a string of a single
5516 * character. If the index is too big or negative the
5517 * result is empty. */
5518 if (n1 >= len || n1 < 0)
5519 s = NULL;
5520 else
5521 s = vim_strnsave(s + n1, 1);
5522 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005523 clear_tv(rettv);
5524 rettv->v_type = VAR_STRING;
5525 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005526 break;
5527
5528 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005529 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 if (n1 < 0)
5531 n1 = len + n1;
5532 if (!empty1 && (n1 < 0 || n1 >= len))
5533 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005534 /* For a range we allow invalid values and return an empty
5535 * list. A list index out of range is an error. */
5536 if (!range)
5537 {
5538 if (verbose)
5539 EMSGN(_(e_listidx), n1);
5540 return FAIL;
5541 }
5542 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005543 }
5544 if (range)
5545 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005546 list_T *l;
5547 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005548
5549 if (n2 < 0)
5550 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005551 else if (n2 >= len)
5552 n2 = len - 1;
5553 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005554 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 l = list_alloc();
5556 if (l == NULL)
5557 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005558 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005559 n1 <= n2; ++n1)
5560 {
5561 if (list_append_tv(l, &item->li_tv) == FAIL)
5562 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005563 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005564 return FAIL;
5565 }
5566 item = item->li_next;
5567 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568 clear_tv(rettv);
5569 rettv->v_type = VAR_LIST;
5570 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005571 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005572 }
5573 else
5574 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005575 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005576 clear_tv(rettv);
5577 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005578 }
5579 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005580
5581 case VAR_DICT:
5582 if (range)
5583 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005584 if (verbose)
5585 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005586 if (len == -1)
5587 clear_tv(&var1);
5588 return FAIL;
5589 }
5590 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005591 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005592
5593 if (len == -1)
5594 {
5595 key = get_tv_string(&var1);
5596 if (*key == NUL)
5597 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005598 if (verbose)
5599 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005600 clear_tv(&var1);
5601 return FAIL;
5602 }
5603 }
5604
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005605 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005606
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005607 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005608 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005609 if (len == -1)
5610 clear_tv(&var1);
5611 if (item == NULL)
5612 return FAIL;
5613
5614 copy_tv(&item->di_tv, &var1);
5615 clear_tv(rettv);
5616 *rettv = var1;
5617 }
5618 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005619 }
5620 }
5621
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005622 return OK;
5623}
5624
5625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 * Get an option value.
5627 * "arg" points to the '&' or '+' before the option name.
5628 * "arg" is advanced to character after the option name.
5629 * Return OK or FAIL.
5630 */
5631 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005632get_option_tv(
5633 char_u **arg,
5634 typval_T *rettv, /* when NULL, only check if option exists */
5635 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636{
5637 char_u *option_end;
5638 long numval;
5639 char_u *stringval;
5640 int opt_type;
5641 int c;
5642 int working = (**arg == '+'); /* has("+option") */
5643 int ret = OK;
5644 int opt_flags;
5645
5646 /*
5647 * Isolate the option name and find its value.
5648 */
5649 option_end = find_option_end(arg, &opt_flags);
5650 if (option_end == NULL)
5651 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005652 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 EMSG2(_("E112: Option name missing: %s"), *arg);
5654 return FAIL;
5655 }
5656
5657 if (!evaluate)
5658 {
5659 *arg = option_end;
5660 return OK;
5661 }
5662
5663 c = *option_end;
5664 *option_end = NUL;
5665 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005666 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667
5668 if (opt_type == -3) /* invalid name */
5669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005670 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 EMSG2(_("E113: Unknown option: %s"), *arg);
5672 ret = FAIL;
5673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005674 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 {
5676 if (opt_type == -2) /* hidden string option */
5677 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005678 rettv->v_type = VAR_STRING;
5679 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 }
5681 else if (opt_type == -1) /* hidden number option */
5682 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005683 rettv->v_type = VAR_NUMBER;
5684 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 }
5686 else if (opt_type == 1) /* number option */
5687 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005688 rettv->v_type = VAR_NUMBER;
5689 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 }
5691 else /* string option */
5692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005693 rettv->v_type = VAR_STRING;
5694 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 }
5696 }
5697 else if (working && (opt_type == -2 || opt_type == -1))
5698 ret = FAIL;
5699
5700 *option_end = c; /* put back for error messages */
5701 *arg = option_end;
5702
5703 return ret;
5704}
5705
5706/*
5707 * Allocate a variable for a string constant.
5708 * Return OK or FAIL.
5709 */
5710 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005711get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712{
5713 char_u *p;
5714 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 int extra = 0;
5716
5717 /*
5718 * Find the end of the string, skipping backslashed characters.
5719 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005720 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 {
5722 if (*p == '\\' && p[1] != NUL)
5723 {
5724 ++p;
5725 /* A "\<x>" form occupies at least 4 characters, and produces up
5726 * to 6 characters: reserve space for 2 extra */
5727 if (*p == '<')
5728 extra += 2;
5729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 }
5731
5732 if (*p != '"')
5733 {
5734 EMSG2(_("E114: Missing quote: %s"), *arg);
5735 return FAIL;
5736 }
5737
5738 /* If only parsing, set *arg and return here */
5739 if (!evaluate)
5740 {
5741 *arg = p + 1;
5742 return OK;
5743 }
5744
5745 /*
5746 * Copy the string into allocated memory, handling backslashed
5747 * characters.
5748 */
5749 name = alloc((unsigned)(p - *arg + extra));
5750 if (name == NULL)
5751 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005752 rettv->v_type = VAR_STRING;
5753 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 {
5757 if (*p == '\\')
5758 {
5759 switch (*++p)
5760 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005761 case 'b': *name++ = BS; ++p; break;
5762 case 'e': *name++ = ESC; ++p; break;
5763 case 'f': *name++ = FF; ++p; break;
5764 case 'n': *name++ = NL; ++p; break;
5765 case 'r': *name++ = CAR; ++p; break;
5766 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767
5768 case 'X': /* hex: "\x1", "\x12" */
5769 case 'x':
5770 case 'u': /* Unicode: "\u0023" */
5771 case 'U':
5772 if (vim_isxdigit(p[1]))
5773 {
5774 int n, nr;
5775 int c = toupper(*p);
5776
5777 if (c == 'X')
5778 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005779 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005781 else
5782 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 nr = 0;
5784 while (--n >= 0 && vim_isxdigit(p[1]))
5785 {
5786 ++p;
5787 nr = (nr << 4) + hex2nr(*p);
5788 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790#ifdef FEAT_MBYTE
5791 /* For "\u" store the number according to
5792 * 'encoding'. */
5793 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005794 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 else
5796#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005797 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 break;
5800
5801 /* octal: "\1", "\12", "\123" */
5802 case '0':
5803 case '1':
5804 case '2':
5805 case '3':
5806 case '4':
5807 case '5':
5808 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 case '7': *name = *p++ - '0';
5810 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 *name = (*name << 3) + *p++ - '0';
5813 if (*p >= '0' && *p <= '7')
5814 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 break;
5818
5819 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 if (extra != 0)
5822 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005823 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 break;
5825 }
5826 /* FALLTHROUGH */
5827
Bram Moolenaar8c711452005-01-14 21:53:12 +00005828 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 break;
5830 }
5831 }
5832 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005833 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 *arg = p + 1;
5838
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 return OK;
5840}
5841
5842/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 * Return OK or FAIL.
5845 */
5846 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005847get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848{
5849 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005851 int reduce = 0;
5852
5853 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005854 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005855 */
5856 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5857 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005858 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005859 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005861 break;
5862 ++reduce;
5863 ++p;
5864 }
5865 }
5866
Bram Moolenaar8c711452005-01-14 21:53:12 +00005867 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005868 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005870 return FAIL;
5871 }
5872
Bram Moolenaar8c711452005-01-14 21:53:12 +00005873 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005874 if (!evaluate)
5875 {
5876 *arg = p + 1;
5877 return OK;
5878 }
5879
5880 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 */
5883 str = alloc((unsigned)((p - *arg) - reduce));
5884 if (str == NULL)
5885 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 rettv->v_type = VAR_STRING;
5887 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005890 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005891 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005892 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005893 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894 break;
5895 ++p;
5896 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005897 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005898 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005899 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005900 *arg = p + 1;
5901
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 return OK;
5903}
5904
5905/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906 * Allocate a variable for a List and fill it from "*arg".
5907 * Return OK or FAIL.
5908 */
5909 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005910get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911{
Bram Moolenaar33570922005-01-25 22:26:29 +00005912 list_T *l = NULL;
5913 typval_T tv;
5914 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915
5916 if (evaluate)
5917 {
5918 l = list_alloc();
5919 if (l == NULL)
5920 return FAIL;
5921 }
5922
5923 *arg = skipwhite(*arg + 1);
5924 while (**arg != ']' && **arg != NUL)
5925 {
5926 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5927 goto failret;
5928 if (evaluate)
5929 {
5930 item = listitem_alloc();
5931 if (item != NULL)
5932 {
5933 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005934 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935 list_append(l, item);
5936 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005937 else
5938 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 }
5940
5941 if (**arg == ']')
5942 break;
5943 if (**arg != ',')
5944 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005945 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946 goto failret;
5947 }
5948 *arg = skipwhite(*arg + 1);
5949 }
5950
5951 if (**arg != ']')
5952 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005953 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005954failret:
5955 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005956 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957 return FAIL;
5958 }
5959
5960 *arg = skipwhite(*arg + 1);
5961 if (evaluate)
5962 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005963 rettv->v_type = VAR_LIST;
5964 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 ++l->lv_refcount;
5966 }
5967
5968 return OK;
5969}
5970
5971/*
5972 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005973 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005975 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005976list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005978 list_T *l;
5979
5980 l = (list_T *)alloc_clear(sizeof(list_T));
5981 if (l != NULL)
5982 {
5983 /* Prepend the list to the list of lists for garbage collection. */
5984 if (first_list != NULL)
5985 first_list->lv_used_prev = l;
5986 l->lv_used_prev = NULL;
5987 l->lv_used_next = first_list;
5988 first_list = l;
5989 }
5990 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991}
5992
5993/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005994 * Allocate an empty list for a return value.
5995 * Returns OK or FAIL.
5996 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005997 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005998rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005999{
6000 list_T *l = list_alloc();
6001
6002 if (l == NULL)
6003 return FAIL;
6004
6005 rettv->vval.v_list = l;
6006 rettv->v_type = VAR_LIST;
6007 ++l->lv_refcount;
6008 return OK;
6009}
6010
6011/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 * Unreference a list: decrement the reference count and free it when it
6013 * becomes zero.
6014 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006015 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006016list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006018 if (l != NULL && --l->lv_refcount <= 0)
6019 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006020}
6021
6022/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006023 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024 * Ignores the reference count.
6025 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006026 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006027list_free(
6028 list_T *l,
6029 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030{
Bram Moolenaar33570922005-01-25 22:26:29 +00006031 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006032
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006033 /* Remove the list from the list of lists for garbage collection. */
6034 if (l->lv_used_prev == NULL)
6035 first_list = l->lv_used_next;
6036 else
6037 l->lv_used_prev->lv_used_next = l->lv_used_next;
6038 if (l->lv_used_next != NULL)
6039 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6040
Bram Moolenaard9fba312005-06-26 22:34:35 +00006041 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006043 /* Remove the item before deleting it. */
6044 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006045 if (recurse || (item->li_tv.v_type != VAR_LIST
6046 && item->li_tv.v_type != VAR_DICT))
6047 clear_tv(&item->li_tv);
6048 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049 }
6050 vim_free(l);
6051}
6052
6053/*
6054 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006055 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006057 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006058listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059{
Bram Moolenaar33570922005-01-25 22:26:29 +00006060 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006061}
6062
6063/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006064 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006066 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006067listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006068{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006069 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070 vim_free(item);
6071}
6072
6073/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006074 * Remove a list item from a List and free it. Also clears the value.
6075 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006076 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006077listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006078{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006079 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006080 listitem_free(item);
6081}
6082
6083/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006084 * Get the number of items in a list.
6085 */
6086 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006087list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089 if (l == NULL)
6090 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006091 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006092}
6093
6094/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006095 * Return TRUE when two lists have exactly the same values.
6096 */
6097 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006098list_equal(
6099 list_T *l1,
6100 list_T *l2,
6101 int ic, /* ignore case for strings */
6102 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006103{
Bram Moolenaar33570922005-01-25 22:26:29 +00006104 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006105
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006106 if (l1 == NULL || l2 == NULL)
6107 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006108 if (l1 == l2)
6109 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006110 if (list_len(l1) != list_len(l2))
6111 return FALSE;
6112
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006113 for (item1 = l1->lv_first, item2 = l2->lv_first;
6114 item1 != NULL && item2 != NULL;
6115 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006116 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006117 return FALSE;
6118 return item1 == NULL && item2 == NULL;
6119}
6120
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006121/*
6122 * Return the dictitem that an entry in a hashtable points to.
6123 */
6124 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006125dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006126{
6127 return HI2DI(hi);
6128}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006129
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006130/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006131 * Return TRUE when two dictionaries have exactly the same key/values.
6132 */
6133 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006134dict_equal(
6135 dict_T *d1,
6136 dict_T *d2,
6137 int ic, /* ignore case for strings */
6138 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006139{
Bram Moolenaar33570922005-01-25 22:26:29 +00006140 hashitem_T *hi;
6141 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006142 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006143
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006144 if (d1 == NULL || d2 == NULL)
6145 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006146 if (d1 == d2)
6147 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006148 if (dict_len(d1) != dict_len(d2))
6149 return FALSE;
6150
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006151 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006152 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006153 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006154 if (!HASHITEM_EMPTY(hi))
6155 {
6156 item2 = dict_find(d2, hi->hi_key, -1);
6157 if (item2 == NULL)
6158 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006159 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006160 return FALSE;
6161 --todo;
6162 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006163 }
6164 return TRUE;
6165}
6166
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006167static int tv_equal_recurse_limit;
6168
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006169/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006170 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006171 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006172 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006173 */
6174 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006175tv_equal(
6176 typval_T *tv1,
6177 typval_T *tv2,
6178 int ic, /* ignore case */
6179 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006180{
6181 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006182 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006183 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006184 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006185
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006186 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006187 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006188
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006189 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006190 * recursiveness to a limit. We guess they are equal then.
6191 * A fixed limit has the problem of still taking an awful long time.
6192 * Reduce the limit every time running into it. That should work fine for
6193 * deeply linked structures that are not recursively linked and catch
6194 * recursiveness quickly. */
6195 if (!recursive)
6196 tv_equal_recurse_limit = 1000;
6197 if (recursive_cnt >= tv_equal_recurse_limit)
6198 {
6199 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006200 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006201 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006202
6203 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006204 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006205 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006206 ++recursive_cnt;
6207 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6208 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006209 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006210
6211 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006212 ++recursive_cnt;
6213 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6214 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006215 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006216
6217 case VAR_FUNC:
6218 return (tv1->vval.v_string != NULL
6219 && tv2->vval.v_string != NULL
6220 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6221
6222 case VAR_NUMBER:
6223 return tv1->vval.v_number == tv2->vval.v_number;
6224
6225 case VAR_STRING:
6226 s1 = get_tv_string_buf(tv1, buf1);
6227 s2 = get_tv_string_buf(tv2, buf2);
6228 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006229
6230 case VAR_SPECIAL:
6231 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006232
6233 case VAR_FLOAT:
6234#ifdef FEAT_FLOAT
6235 return tv1->vval.v_float == tv2->vval.v_float;
6236#endif
6237 case VAR_JOB:
6238#ifdef FEAT_JOB
6239 return tv1->vval.v_job == tv2->vval.v_job;
6240#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006241 case VAR_CHANNEL:
6242#ifdef FEAT_CHANNEL
6243 return tv1->vval.v_channel == tv2->vval.v_channel;
6244#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006245 case VAR_UNKNOWN:
6246 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006247 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006248
Bram Moolenaara03f2332016-02-06 18:09:59 +01006249 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6250 * does not equal anything, not even itself. */
6251 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006252}
6253
6254/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006255 * Locate item with index "n" in list "l" and return it.
6256 * A negative index is counted from the end; -1 is the last item.
6257 * Returns NULL when "n" is out of range.
6258 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006259 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006260list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006261{
Bram Moolenaar33570922005-01-25 22:26:29 +00006262 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006263 long idx;
6264
6265 if (l == NULL)
6266 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006267
6268 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006269 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006270 n = l->lv_len + n;
6271
6272 /* Check for index out of range. */
6273 if (n < 0 || n >= l->lv_len)
6274 return NULL;
6275
6276 /* When there is a cached index may start search from there. */
6277 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006278 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006279 if (n < l->lv_idx / 2)
6280 {
6281 /* closest to the start of the list */
6282 item = l->lv_first;
6283 idx = 0;
6284 }
6285 else if (n > (l->lv_idx + l->lv_len) / 2)
6286 {
6287 /* closest to the end of the list */
6288 item = l->lv_last;
6289 idx = l->lv_len - 1;
6290 }
6291 else
6292 {
6293 /* closest to the cached index */
6294 item = l->lv_idx_item;
6295 idx = l->lv_idx;
6296 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006297 }
6298 else
6299 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006300 if (n < l->lv_len / 2)
6301 {
6302 /* closest to the start of the list */
6303 item = l->lv_first;
6304 idx = 0;
6305 }
6306 else
6307 {
6308 /* closest to the end of the list */
6309 item = l->lv_last;
6310 idx = l->lv_len - 1;
6311 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006313
6314 while (n > idx)
6315 {
6316 /* search forward */
6317 item = item->li_next;
6318 ++idx;
6319 }
6320 while (n < idx)
6321 {
6322 /* search backward */
6323 item = item->li_prev;
6324 --idx;
6325 }
6326
6327 /* cache the used index */
6328 l->lv_idx = idx;
6329 l->lv_idx_item = item;
6330
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331 return item;
6332}
6333
6334/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006335 * Get list item "l[idx]" as a number.
6336 */
6337 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006338list_find_nr(
6339 list_T *l,
6340 long idx,
6341 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006342{
6343 listitem_T *li;
6344
6345 li = list_find(l, idx);
6346 if (li == NULL)
6347 {
6348 if (errorp != NULL)
6349 *errorp = TRUE;
6350 return -1L;
6351 }
6352 return get_tv_number_chk(&li->li_tv, errorp);
6353}
6354
6355/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006356 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6357 */
6358 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006359list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006360{
6361 listitem_T *li;
6362
6363 li = list_find(l, idx - 1);
6364 if (li == NULL)
6365 {
6366 EMSGN(_(e_listidx), idx);
6367 return NULL;
6368 }
6369 return get_tv_string(&li->li_tv);
6370}
6371
6372/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006373 * Locate "item" list "l" and return its index.
6374 * Returns -1 when "item" is not in the list.
6375 */
6376 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006377list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006378{
6379 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006380 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006381
6382 if (l == NULL)
6383 return -1;
6384 idx = 0;
6385 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6386 ++idx;
6387 if (li == NULL)
6388 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006389 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006390}
6391
6392/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006393 * Append item "item" to the end of list "l".
6394 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006395 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006396list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006397{
6398 if (l->lv_last == NULL)
6399 {
6400 /* empty list */
6401 l->lv_first = item;
6402 l->lv_last = item;
6403 item->li_prev = NULL;
6404 }
6405 else
6406 {
6407 l->lv_last->li_next = item;
6408 item->li_prev = l->lv_last;
6409 l->lv_last = item;
6410 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006411 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006412 item->li_next = NULL;
6413}
6414
6415/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006416 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006417 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006419 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006420list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006421{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006422 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423
Bram Moolenaar05159a02005-02-26 23:04:13 +00006424 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006425 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006426 copy_tv(tv, &li->li_tv);
6427 list_append(l, li);
6428 return OK;
6429}
6430
6431/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006432 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006433 * Return FAIL when out of memory.
6434 */
6435 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006436list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006437{
6438 listitem_T *li = listitem_alloc();
6439
6440 if (li == NULL)
6441 return FAIL;
6442 li->li_tv.v_type = VAR_DICT;
6443 li->li_tv.v_lock = 0;
6444 li->li_tv.vval.v_dict = dict;
6445 list_append(list, li);
6446 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006447 return OK;
6448}
6449
6450/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006451 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006452 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006453 * Returns FAIL when out of memory.
6454 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006455 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006456list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006457{
6458 listitem_T *li = listitem_alloc();
6459
6460 if (li == NULL)
6461 return FAIL;
6462 list_append(l, li);
6463 li->li_tv.v_type = VAR_STRING;
6464 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006465 if (str == NULL)
6466 li->li_tv.vval.v_string = NULL;
6467 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006468 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006469 return FAIL;
6470 return OK;
6471}
6472
6473/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006474 * Append "n" to list "l".
6475 * Returns FAIL when out of memory.
6476 */
6477 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006478list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006479{
6480 listitem_T *li;
6481
6482 li = listitem_alloc();
6483 if (li == NULL)
6484 return FAIL;
6485 li->li_tv.v_type = VAR_NUMBER;
6486 li->li_tv.v_lock = 0;
6487 li->li_tv.vval.v_number = n;
6488 list_append(l, li);
6489 return OK;
6490}
6491
6492/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006494 * If "item" is NULL append at the end.
6495 * Return FAIL when out of memory.
6496 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006497 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006498list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006499{
Bram Moolenaar33570922005-01-25 22:26:29 +00006500 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006501
6502 if (ni == NULL)
6503 return FAIL;
6504 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006505 list_insert(l, ni, item);
6506 return OK;
6507}
6508
6509 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006510list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006511{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006512 if (item == NULL)
6513 /* Append new item at end of list. */
6514 list_append(l, ni);
6515 else
6516 {
6517 /* Insert new item before existing item. */
6518 ni->li_prev = item->li_prev;
6519 ni->li_next = item;
6520 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006521 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006523 ++l->lv_idx;
6524 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006525 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006526 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006528 l->lv_idx_item = NULL;
6529 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006531 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006532 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533}
6534
6535/*
6536 * Extend "l1" with "l2".
6537 * If "bef" is NULL append at the end, otherwise insert before this item.
6538 * Returns FAIL when out of memory.
6539 */
6540 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006541list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542{
Bram Moolenaar33570922005-01-25 22:26:29 +00006543 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006544 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006545
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006546 /* We also quit the loop when we have inserted the original item count of
6547 * the list, avoid a hang when we extend a list with itself. */
6548 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006549 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6550 return FAIL;
6551 return OK;
6552}
6553
6554/*
6555 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6556 * Return FAIL when out of memory.
6557 */
6558 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006559list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006560{
Bram Moolenaar33570922005-01-25 22:26:29 +00006561 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006563 if (l1 == NULL || l2 == NULL)
6564 return FAIL;
6565
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006567 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006568 if (l == NULL)
6569 return FAIL;
6570 tv->v_type = VAR_LIST;
6571 tv->vval.v_list = l;
6572
6573 /* append all items from the second list */
6574 return list_extend(l, l2, NULL);
6575}
6576
6577/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006578 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006580 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006581 * Returns NULL when out of memory.
6582 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006584list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585{
Bram Moolenaar33570922005-01-25 22:26:29 +00006586 list_T *copy;
6587 listitem_T *item;
6588 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589
6590 if (orig == NULL)
6591 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006592
6593 copy = list_alloc();
6594 if (copy != NULL)
6595 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006596 if (copyID != 0)
6597 {
6598 /* Do this before adding the items, because one of the items may
6599 * refer back to this list. */
6600 orig->lv_copyID = copyID;
6601 orig->lv_copylist = copy;
6602 }
6603 for (item = orig->lv_first; item != NULL && !got_int;
6604 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006605 {
6606 ni = listitem_alloc();
6607 if (ni == NULL)
6608 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006609 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006610 {
6611 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6612 {
6613 vim_free(ni);
6614 break;
6615 }
6616 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006617 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006618 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619 list_append(copy, ni);
6620 }
6621 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006622 if (item != NULL)
6623 {
6624 list_unref(copy);
6625 copy = NULL;
6626 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006627 }
6628
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006629 return copy;
6630}
6631
6632/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006633 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006634 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006635 * This used to be called list_remove, but that conflicts with a Sun header
6636 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006637 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006638 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006639vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006640{
Bram Moolenaar33570922005-01-25 22:26:29 +00006641 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006642
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006643 /* notify watchers */
6644 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006645 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006646 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006647 list_fix_watch(l, ip);
6648 if (ip == item2)
6649 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006650 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006651
6652 if (item2->li_next == NULL)
6653 l->lv_last = item->li_prev;
6654 else
6655 item2->li_next->li_prev = item->li_prev;
6656 if (item->li_prev == NULL)
6657 l->lv_first = item2->li_next;
6658 else
6659 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006660 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006661}
6662
6663/*
6664 * Return an allocated string with the string representation of a list.
6665 * May return NULL.
6666 */
6667 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006668list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006669{
6670 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006671
6672 if (tv->vval.v_list == NULL)
6673 return NULL;
6674 ga_init2(&ga, (int)sizeof(char), 80);
6675 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006676 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006677 {
6678 vim_free(ga.ga_data);
6679 return NULL;
6680 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006681 ga_append(&ga, ']');
6682 ga_append(&ga, NUL);
6683 return (char_u *)ga.ga_data;
6684}
6685
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006686typedef struct join_S {
6687 char_u *s;
6688 char_u *tofree;
6689} join_T;
6690
6691 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006692list_join_inner(
6693 garray_T *gap, /* to store the result in */
6694 list_T *l,
6695 char_u *sep,
6696 int echo_style,
6697 int copyID,
6698 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006699{
6700 int i;
6701 join_T *p;
6702 int len;
6703 int sumlen = 0;
6704 int first = TRUE;
6705 char_u *tofree;
6706 char_u numbuf[NUMBUFLEN];
6707 listitem_T *item;
6708 char_u *s;
6709
6710 /* Stringify each item in the list. */
6711 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6712 {
6713 if (echo_style)
6714 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6715 else
6716 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6717 if (s == NULL)
6718 return FAIL;
6719
6720 len = (int)STRLEN(s);
6721 sumlen += len;
6722
Bram Moolenaarcde88542015-08-11 19:14:00 +02006723 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006724 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6725 if (tofree != NULL || s != numbuf)
6726 {
6727 p->s = s;
6728 p->tofree = tofree;
6729 }
6730 else
6731 {
6732 p->s = vim_strnsave(s, len);
6733 p->tofree = p->s;
6734 }
6735
6736 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006737 if (did_echo_string_emsg) /* recursion error, bail out */
6738 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006739 }
6740
6741 /* Allocate result buffer with its total size, avoid re-allocation and
6742 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6743 if (join_gap->ga_len >= 2)
6744 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6745 if (ga_grow(gap, sumlen + 2) == FAIL)
6746 return FAIL;
6747
6748 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6749 {
6750 if (first)
6751 first = FALSE;
6752 else
6753 ga_concat(gap, sep);
6754 p = ((join_T *)join_gap->ga_data) + i;
6755
6756 if (p->s != NULL)
6757 ga_concat(gap, p->s);
6758 line_breakcheck();
6759 }
6760
6761 return OK;
6762}
6763
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006764/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006765 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006766 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006767 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006768 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006769 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006770list_join(
6771 garray_T *gap,
6772 list_T *l,
6773 char_u *sep,
6774 int echo_style,
6775 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006776{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006777 garray_T join_ga;
6778 int retval;
6779 join_T *p;
6780 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006781
Bram Moolenaard39a7512015-04-16 22:51:22 +02006782 if (l->lv_len < 1)
6783 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006784 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6785 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6786
6787 /* Dispose each item in join_ga. */
6788 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006789 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006790 p = (join_T *)join_ga.ga_data;
6791 for (i = 0; i < join_ga.ga_len; ++i)
6792 {
6793 vim_free(p->tofree);
6794 ++p;
6795 }
6796 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006797 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006798
6799 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006800}
6801
6802/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006803 * Return the next (unique) copy ID.
6804 * Used for serializing nested structures.
6805 */
6806 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006807get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006808{
6809 current_copyID += COPYID_INC;
6810 return current_copyID;
6811}
6812
6813/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006814 * Garbage collection for lists and dictionaries.
6815 *
6816 * We use reference counts to be able to free most items right away when they
6817 * are no longer used. But for composite items it's possible that it becomes
6818 * unused while the reference count is > 0: When there is a recursive
6819 * reference. Example:
6820 * :let l = [1, 2, 3]
6821 * :let d = {9: l}
6822 * :let l[1] = d
6823 *
6824 * Since this is quite unusual we handle this with garbage collection: every
6825 * once in a while find out which lists and dicts are not referenced from any
6826 * variable.
6827 *
6828 * Here is a good reference text about garbage collection (refers to Python
6829 * but it applies to all reference-counting mechanisms):
6830 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006831 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006832
6833/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006834 * Do garbage collection for lists and dicts.
6835 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006836 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006837 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006838garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006839{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006840 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006841 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006842 buf_T *buf;
6843 win_T *wp;
6844 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006845 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006846 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006847 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006848#ifdef FEAT_WINDOWS
6849 tabpage_T *tp;
6850#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006851
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006852 /* Only do this once. */
6853 want_garbage_collect = FALSE;
6854 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006855 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006856
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006857 /* We advance by two because we add one for items referenced through
6858 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006859 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006860
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006861 /*
6862 * 1. Go through all accessible variables and mark all lists and dicts
6863 * with copyID.
6864 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006865
6866 /* Don't free variables in the previous_funccal list unless they are only
6867 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006868 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006869 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6870 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006871 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6872 NULL);
6873 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6874 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006875 }
6876
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006877 /* script-local variables */
6878 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006879 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006880
6881 /* buffer-local variables */
6882 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006883 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6884 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006885
6886 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006887 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006888 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6889 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006890#ifdef FEAT_AUTOCMD
6891 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006892 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6893 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006894#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006896#ifdef FEAT_WINDOWS
6897 /* tabpage-local variables */
6898 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006899 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6900 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006901#endif
6902
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006903 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006904 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006905
6906 /* function-local variables */
6907 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6908 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006909 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6910 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911 }
6912
Bram Moolenaard812df62008-11-09 12:46:09 +00006913 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006914 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006915
Bram Moolenaar1dced572012-04-05 16:54:08 +02006916#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006917 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006918#endif
6919
Bram Moolenaardb913952012-06-29 12:54:53 +02006920#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006921 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006922#endif
6923
6924#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006925 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006926#endif
6927
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006928#ifdef FEAT_CHANNEL
6929 abort = abort || set_ref_in_channel(copyID);
6930#endif
6931
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006932 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006933 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006934 /*
6935 * 2. Free lists and dictionaries that are not referenced.
6936 */
6937 did_free = free_unref_items(copyID);
6938
6939 /*
6940 * 3. Check if any funccal can be freed now.
6941 */
6942 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006943 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006944 if (can_free_funccal(*pfc, copyID))
6945 {
6946 fc = *pfc;
6947 *pfc = fc->caller;
6948 free_funccal(fc, TRUE);
6949 did_free = TRUE;
6950 did_free_funccal = TRUE;
6951 }
6952 else
6953 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006955 if (did_free_funccal)
6956 /* When a funccal was freed some more items might be garbage
6957 * collected, so run again. */
6958 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006959 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006960 else if (p_verbose > 0)
6961 {
6962 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6963 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006964
6965 return did_free;
6966}
6967
6968/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006969 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006970 */
6971 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006972free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006973{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006974 dict_T *dd, *dd_next;
6975 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006976 int did_free = FALSE;
6977
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006978 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006979 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006980 */
6981 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006982 {
6983 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006984 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006985 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006986 /* Free the Dictionary and ordinary items it contains, but don't
6987 * recurse into Lists and Dictionaries, they will be in the list
6988 * of dicts or list of lists. */
6989 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006990 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006992 dd = dd_next;
6993 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994
6995 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006996 * Go through the list of lists and free items without the copyID.
6997 * But don't free a list that has a watcher (used in a for loop), these
6998 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006999 */
7000 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007001 {
7002 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007003 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7004 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007006 /* Free the List and ordinary items it contains, but don't recurse
7007 * into Lists and Dictionaries, they will be in the list of dicts
7008 * or list of lists. */
7009 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007010 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007012 ll = ll_next;
7013 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007014
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015 return did_free;
7016}
7017
7018/*
7019 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007020 * "list_stack" is used to add lists to be marked. Can be NULL.
7021 *
7022 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007024 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007025set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007026{
7027 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007028 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007030 hashtab_T *cur_ht;
7031 ht_stack_T *ht_stack = NULL;
7032 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 cur_ht = ht;
7035 for (;;)
7036 {
7037 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007038 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007039 /* Mark each item in the hashtab. If the item contains a hashtab
7040 * it is added to ht_stack, if it contains a list it is added to
7041 * list_stack. */
7042 todo = (int)cur_ht->ht_used;
7043 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7044 if (!HASHITEM_EMPTY(hi))
7045 {
7046 --todo;
7047 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7048 &ht_stack, list_stack);
7049 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007050 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007051
7052 if (ht_stack == NULL)
7053 break;
7054
7055 /* take an item from the stack */
7056 cur_ht = ht_stack->ht;
7057 tempitem = ht_stack;
7058 ht_stack = ht_stack->prev;
7059 free(tempitem);
7060 }
7061
7062 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007063}
7064
7065/*
7066 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007067 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7068 *
7069 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007070 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007071 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007072set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007073{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007074 listitem_T *li;
7075 int abort = FALSE;
7076 list_T *cur_l;
7077 list_stack_T *list_stack = NULL;
7078 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007079
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007080 cur_l = l;
7081 for (;;)
7082 {
7083 if (!abort)
7084 /* Mark each item in the list. If the item contains a hashtab
7085 * it is added to ht_stack, if it contains a list it is added to
7086 * list_stack. */
7087 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7088 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7089 ht_stack, &list_stack);
7090 if (list_stack == NULL)
7091 break;
7092
7093 /* take an item from the stack */
7094 cur_l = list_stack->list;
7095 tempitem = list_stack;
7096 list_stack = list_stack->prev;
7097 free(tempitem);
7098 }
7099
7100 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007101}
7102
7103/*
7104 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007105 * "list_stack" is used to add lists to be marked. Can be NULL.
7106 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7107 *
7108 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007109 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007110 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007111set_ref_in_item(
7112 typval_T *tv,
7113 int copyID,
7114 ht_stack_T **ht_stack,
7115 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007116{
7117 dict_T *dd;
7118 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007119 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007120
Bram Moolenaara03f2332016-02-06 18:09:59 +01007121 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007122 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007123 dd = tv->vval.v_dict;
7124 if (dd != NULL && dd->dv_copyID != copyID)
7125 {
7126 /* Didn't see this dict yet. */
7127 dd->dv_copyID = copyID;
7128 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007129 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007130 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7131 }
7132 else
7133 {
7134 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7135 if (newitem == NULL)
7136 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007137 else
7138 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007139 newitem->ht = &dd->dv_hashtab;
7140 newitem->prev = *ht_stack;
7141 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007142 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007143 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007144 }
7145 }
7146 else if (tv->v_type == VAR_LIST)
7147 {
7148 ll = tv->vval.v_list;
7149 if (ll != NULL && ll->lv_copyID != copyID)
7150 {
7151 /* Didn't see this list yet. */
7152 ll->lv_copyID = copyID;
7153 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007154 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007155 abort = set_ref_in_list(ll, copyID, ht_stack);
7156 }
7157 else
7158 {
7159 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007160 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007161 if (newitem == NULL)
7162 abort = TRUE;
7163 else
7164 {
7165 newitem->list = ll;
7166 newitem->prev = *list_stack;
7167 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007168 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007169 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007170 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007171 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007172 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007173}
7174
7175/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007176 * Allocate an empty header for a dictionary.
7177 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007178 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007179dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007180{
Bram Moolenaar33570922005-01-25 22:26:29 +00007181 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007182
Bram Moolenaar33570922005-01-25 22:26:29 +00007183 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007184 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007185 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007186 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007187 if (first_dict != NULL)
7188 first_dict->dv_used_prev = d;
7189 d->dv_used_next = first_dict;
7190 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007191 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007192
Bram Moolenaar33570922005-01-25 22:26:29 +00007193 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007194 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007195 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007196 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007197 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007198 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007199 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007200}
7201
7202/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007203 * Allocate an empty dict for a return value.
7204 * Returns OK or FAIL.
7205 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007206 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007207rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007208{
7209 dict_T *d = dict_alloc();
7210
7211 if (d == NULL)
7212 return FAIL;
7213
7214 rettv->vval.v_dict = d;
7215 rettv->v_type = VAR_DICT;
7216 ++d->dv_refcount;
7217 return OK;
7218}
7219
7220
7221/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222 * Unreference a Dictionary: decrement the reference count and free it when it
7223 * becomes zero.
7224 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007225 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007226dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007227{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007228 if (d != NULL && --d->dv_refcount <= 0)
7229 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007230}
7231
7232/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007233 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007234 * Ignores the reference count.
7235 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007236 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007237dict_free(
7238 dict_T *d,
7239 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007240{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007241 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007242 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007243 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007244
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007245 /* Remove the dict from the list of dicts for garbage collection. */
7246 if (d->dv_used_prev == NULL)
7247 first_dict = d->dv_used_next;
7248 else
7249 d->dv_used_prev->dv_used_next = d->dv_used_next;
7250 if (d->dv_used_next != NULL)
7251 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7252
7253 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007254 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007255 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007256 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007258 if (!HASHITEM_EMPTY(hi))
7259 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007260 /* Remove the item before deleting it, just in case there is
7261 * something recursive causing trouble. */
7262 di = HI2DI(hi);
7263 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007264 if (recurse || (di->di_tv.v_type != VAR_LIST
7265 && di->di_tv.v_type != VAR_DICT))
7266 clear_tv(&di->di_tv);
7267 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007268 --todo;
7269 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007270 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007271 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007272 vim_free(d);
7273}
7274
7275/*
7276 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007277 * The "key" is copied to the new item.
7278 * Note that the value of the item "di_tv" still needs to be initialized!
7279 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007280 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007281 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007282dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007283{
Bram Moolenaar33570922005-01-25 22:26:29 +00007284 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007285
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007286 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007287 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007288 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007289 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007290 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007291 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007292 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007293}
7294
7295/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007296 * Make a copy of a Dictionary item.
7297 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007298 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007299dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007300{
Bram Moolenaar33570922005-01-25 22:26:29 +00007301 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007303 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7304 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007305 if (di != NULL)
7306 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007308 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007309 copy_tv(&org->di_tv, &di->di_tv);
7310 }
7311 return di;
7312}
7313
7314/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007315 * Remove item "item" from Dictionary "dict" and free it.
7316 */
7317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007318dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007319{
Bram Moolenaar33570922005-01-25 22:26:29 +00007320 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321
Bram Moolenaar33570922005-01-25 22:26:29 +00007322 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007323 if (HASHITEM_EMPTY(hi))
7324 EMSG2(_(e_intern2), "dictitem_remove()");
7325 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007326 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007327 dictitem_free(item);
7328}
7329
7330/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007331 * Free a dict item. Also clears the value.
7332 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007333 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007334dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007335{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007336 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007337 if (item->di_flags & DI_FLAGS_ALLOC)
7338 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007339}
7340
7341/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007342 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7343 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007344 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007345 * Returns NULL when out of memory.
7346 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007347 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007348dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007349{
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 dict_T *copy;
7351 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007352 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007353 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007354
7355 if (orig == NULL)
7356 return NULL;
7357
7358 copy = dict_alloc();
7359 if (copy != NULL)
7360 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007361 if (copyID != 0)
7362 {
7363 orig->dv_copyID = copyID;
7364 orig->dv_copydict = copy;
7365 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007366 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007367 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007368 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007369 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007370 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007371 --todo;
7372
7373 di = dictitem_alloc(hi->hi_key);
7374 if (di == NULL)
7375 break;
7376 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007377 {
7378 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7379 copyID) == FAIL)
7380 {
7381 vim_free(di);
7382 break;
7383 }
7384 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007385 else
7386 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7387 if (dict_add(copy, di) == FAIL)
7388 {
7389 dictitem_free(di);
7390 break;
7391 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007392 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007393 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007394
Bram Moolenaare9a41262005-01-15 22:18:47 +00007395 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007396 if (todo > 0)
7397 {
7398 dict_unref(copy);
7399 copy = NULL;
7400 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007401 }
7402
7403 return copy;
7404}
7405
7406/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007407 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007408 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007409 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007410 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007411dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007412{
Bram Moolenaar33570922005-01-25 22:26:29 +00007413 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007414}
7415
Bram Moolenaar8c711452005-01-14 21:53:12 +00007416/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007417 * Add a number or string entry to dictionary "d".
7418 * When "str" is NULL use number "nr", otherwise use "str".
7419 * Returns FAIL when out of memory and when key already exists.
7420 */
7421 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007422dict_add_nr_str(
7423 dict_T *d,
7424 char *key,
7425 long nr,
7426 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007427{
7428 dictitem_T *item;
7429
7430 item = dictitem_alloc((char_u *)key);
7431 if (item == NULL)
7432 return FAIL;
7433 item->di_tv.v_lock = 0;
7434 if (str == NULL)
7435 {
7436 item->di_tv.v_type = VAR_NUMBER;
7437 item->di_tv.vval.v_number = nr;
7438 }
7439 else
7440 {
7441 item->di_tv.v_type = VAR_STRING;
7442 item->di_tv.vval.v_string = vim_strsave(str);
7443 }
7444 if (dict_add(d, item) == FAIL)
7445 {
7446 dictitem_free(item);
7447 return FAIL;
7448 }
7449 return OK;
7450}
7451
7452/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007453 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007454 * Returns FAIL when out of memory and when key already exists.
7455 */
7456 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007457dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007458{
7459 dictitem_T *item;
7460
7461 item = dictitem_alloc((char_u *)key);
7462 if (item == NULL)
7463 return FAIL;
7464 item->di_tv.v_lock = 0;
7465 item->di_tv.v_type = VAR_LIST;
7466 item->di_tv.vval.v_list = list;
7467 if (dict_add(d, item) == FAIL)
7468 {
7469 dictitem_free(item);
7470 return FAIL;
7471 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007472 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007473 return OK;
7474}
7475
7476/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007477 * Get the number of items in a Dictionary.
7478 */
7479 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007480dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007481{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007482 if (d == NULL)
7483 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007484 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007485}
7486
7487/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007488 * Find item "key[len]" in Dictionary "d".
7489 * If "len" is negative use strlen(key).
7490 * Returns NULL when not found.
7491 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007492 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007493dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007495#define AKEYLEN 200
7496 char_u buf[AKEYLEN];
7497 char_u *akey;
7498 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007499 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007501 if (len < 0)
7502 akey = key;
7503 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007504 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007505 tofree = akey = vim_strnsave(key, len);
7506 if (akey == NULL)
7507 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007508 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007509 else
7510 {
7511 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007512 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007513 akey = buf;
7514 }
7515
Bram Moolenaar33570922005-01-25 22:26:29 +00007516 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007517 vim_free(tofree);
7518 if (HASHITEM_EMPTY(hi))
7519 return NULL;
7520 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007521}
7522
7523/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007524 * Get a string item from a dictionary.
7525 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007526 * Returns NULL if the entry doesn't exist or out of memory.
7527 */
7528 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007529get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007530{
7531 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007532 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007533
7534 di = dict_find(d, key, -1);
7535 if (di == NULL)
7536 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007537 s = get_tv_string(&di->di_tv);
7538 if (save && s != NULL)
7539 s = vim_strsave(s);
7540 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007541}
7542
7543/*
7544 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007545 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007546 */
7547 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007548get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007549{
7550 dictitem_T *di;
7551
7552 di = dict_find(d, key, -1);
7553 if (di == NULL)
7554 return 0;
7555 return get_tv_number(&di->di_tv);
7556}
7557
7558/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007559 * Return an allocated string with the string representation of a Dictionary.
7560 * May return NULL.
7561 */
7562 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007563dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007564{
7565 garray_T ga;
7566 int first = TRUE;
7567 char_u *tofree;
7568 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007569 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007571 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007572 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007573
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007574 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007575 return NULL;
7576 ga_init2(&ga, (int)sizeof(char), 80);
7577 ga_append(&ga, '{');
7578
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007579 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007580 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007582 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007583 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007584 --todo;
7585
7586 if (first)
7587 first = FALSE;
7588 else
7589 ga_concat(&ga, (char_u *)", ");
7590
7591 tofree = string_quote(hi->hi_key, FALSE);
7592 if (tofree != NULL)
7593 {
7594 ga_concat(&ga, tofree);
7595 vim_free(tofree);
7596 }
7597 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007598 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007599 if (s != NULL)
7600 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007602 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007603 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007604 line_breakcheck();
7605
Bram Moolenaar8c711452005-01-14 21:53:12 +00007606 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007607 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007608 if (todo > 0)
7609 {
7610 vim_free(ga.ga_data);
7611 return NULL;
7612 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613
7614 ga_append(&ga, '}');
7615 ga_append(&ga, NUL);
7616 return (char_u *)ga.ga_data;
7617}
7618
7619/*
7620 * Allocate a variable for a Dictionary and fill it from "*arg".
7621 * Return OK or FAIL. Returns NOTDONE for {expr}.
7622 */
7623 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007624get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007625{
Bram Moolenaar33570922005-01-25 22:26:29 +00007626 dict_T *d = NULL;
7627 typval_T tvkey;
7628 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007629 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007630 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007632 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007633
7634 /*
7635 * First check if it's not a curly-braces thing: {expr}.
7636 * Must do this without evaluating, otherwise a function may be called
7637 * twice. Unfortunately this means we need to call eval1() twice for the
7638 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007639 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007640 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007641 if (*start != '}')
7642 {
7643 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7644 return FAIL;
7645 if (*start == '}')
7646 return NOTDONE;
7647 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007648
7649 if (evaluate)
7650 {
7651 d = dict_alloc();
7652 if (d == NULL)
7653 return FAIL;
7654 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007655 tvkey.v_type = VAR_UNKNOWN;
7656 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657
7658 *arg = skipwhite(*arg + 1);
7659 while (**arg != '}' && **arg != NUL)
7660 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007661 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 goto failret;
7663 if (**arg != ':')
7664 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007665 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007666 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007667 goto failret;
7668 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007669 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007670 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007671 key = get_tv_string_buf_chk(&tvkey, buf);
7672 if (key == NULL || *key == NUL)
7673 {
7674 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7675 if (key != NULL)
7676 EMSG(_(e_emptykey));
7677 clear_tv(&tvkey);
7678 goto failret;
7679 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681
7682 *arg = skipwhite(*arg + 1);
7683 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7684 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007685 if (evaluate)
7686 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687 goto failret;
7688 }
7689 if (evaluate)
7690 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007691 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692 if (item != NULL)
7693 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007694 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007695 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007696 clear_tv(&tv);
7697 goto failret;
7698 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007699 item = dictitem_alloc(key);
7700 clear_tv(&tvkey);
7701 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007703 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007704 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007705 if (dict_add(d, item) == FAIL)
7706 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007707 }
7708 }
7709
7710 if (**arg == '}')
7711 break;
7712 if (**arg != ',')
7713 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007714 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007715 goto failret;
7716 }
7717 *arg = skipwhite(*arg + 1);
7718 }
7719
7720 if (**arg != '}')
7721 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007722 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723failret:
7724 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007725 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007726 return FAIL;
7727 }
7728
7729 *arg = skipwhite(*arg + 1);
7730 if (evaluate)
7731 {
7732 rettv->v_type = VAR_DICT;
7733 rettv->vval.v_dict = d;
7734 ++d->dv_refcount;
7735 }
7736
7737 return OK;
7738}
7739
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007740#if defined(FEAT_CHANNEL) || defined(PROTO)
7741/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01007742 * Decrement the reference count on "channel" and maybe free it when it goes
7743 * down to zero. Don't free it if there is a pending action.
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007744 * Returns TRUE when the channel was freed.
7745 */
7746 int
Bram Moolenaar77073442016-02-13 23:23:53 +01007747channel_unref(channel_T *channel)
7748{
7749 if (channel != NULL && --channel->ch_refcount <= 0)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007750 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01007751 channel_may_free(channel);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007752 return TRUE;
7753 }
7754 return FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01007755}
7756#endif
7757
Bram Moolenaar65edff82016-02-21 16:40:11 +01007758#if defined(FEAT_JOB) || defined(PROTO)
7759static job_T *first_job = NULL;
7760
Bram Moolenaar835dc632016-02-07 14:27:38 +01007761 static void
7762job_free(job_T *job)
7763{
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007764# ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007765 if (job->jv_channel != NULL)
7766 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01007767 /* The link from the channel to the job doesn't count as a reference,
7768 * thus don't decrement the refcount of the job. The reference from
7769 * the job to the channel does count the refrence, decrement it and
7770 * NULL the reference. We don't set ch_job_killed, unreferencing the
7771 * job doesn't mean it stops running. */
Bram Moolenaar77073442016-02-13 23:23:53 +01007772 job->jv_channel->ch_job = NULL;
7773 channel_unref(job->jv_channel);
7774 }
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007775# endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01007776 mch_clear_job(job);
Bram Moolenaar65edff82016-02-21 16:40:11 +01007777
7778 if (job->jv_next != NULL)
7779 job->jv_next->jv_prev = job->jv_prev;
7780 if (job->jv_prev == NULL)
7781 first_job = job->jv_next;
7782 else
7783 job->jv_prev->jv_next = job->jv_next;
7784
7785 vim_free(job->jv_stoponexit);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007786 vim_free(job->jv_exit_cb);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007787 vim_free(job);
7788}
7789
7790 static void
7791job_unref(job_T *job)
7792{
7793 if (job != NULL && --job->jv_refcount <= 0)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007794 {
7795 /* Do not free the job when it has not ended yet and there is a
7796 * "stoponexit" flag or an exit callback. */
7797 if (job->jv_status != JOB_STARTED
7798 || (job->jv_stoponexit == NULL && job->jv_exit_cb == NULL))
7799 job_free(job);
7800 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007801}
7802
7803/*
Bram Moolenaar65edff82016-02-21 16:40:11 +01007804 * Allocate a job. Sets the refcount to one and sets options default.
Bram Moolenaar835dc632016-02-07 14:27:38 +01007805 */
7806 static job_T *
7807job_alloc(void)
7808{
7809 job_T *job;
7810
7811 job = (job_T *)alloc_clear(sizeof(job_T));
7812 if (job != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007813 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01007814 job->jv_refcount = 1;
Bram Moolenaar65edff82016-02-21 16:40:11 +01007815 job->jv_stoponexit = vim_strsave((char_u *)"term");
7816
7817 if (first_job != NULL)
7818 {
7819 first_job->jv_prev = job;
7820 job->jv_next = first_job;
7821 }
7822 first_job = job;
7823 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007824 return job;
7825}
7826
Bram Moolenaar65edff82016-02-21 16:40:11 +01007827 static void
7828job_set_options(job_T *job, jobopt_T *opt)
7829{
7830 if (opt->jo_set & JO_STOPONEXIT)
7831 {
7832 vim_free(job->jv_stoponexit);
7833 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
7834 job->jv_stoponexit = NULL;
7835 else
7836 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
7837 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007838 if (opt->jo_set & JO_EXIT_CB)
7839 {
7840 vim_free(job->jv_exit_cb);
7841 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
7842 job->jv_exit_cb = NULL;
7843 else
7844 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
7845 }
Bram Moolenaar65edff82016-02-21 16:40:11 +01007846}
7847
7848/*
7849 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
7850 */
7851 void
7852job_stop_on_exit()
7853{
7854 job_T *job;
7855
7856 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007857 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007858 mch_stop_job(job, job->jv_stoponexit);
7859}
Bram Moolenaar835dc632016-02-07 14:27:38 +01007860#endif
7861
Bram Moolenaar17a13432016-01-24 14:22:10 +01007862 static char *
7863get_var_special_name(int nr)
7864{
7865 switch (nr)
7866 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007867 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007868 case VVAL_TRUE: return "v:true";
7869 case VVAL_NONE: return "v:none";
7870 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007871 }
7872 EMSG2(_(e_intern2), "get_var_special_name()");
7873 return "42";
7874}
7875
Bram Moolenaar8c711452005-01-14 21:53:12 +00007876/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007877 * Return a string with the string representation of a variable.
7878 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007879 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007880 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007881 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007882 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007883 */
7884 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007885echo_string(
7886 typval_T *tv,
7887 char_u **tofree,
7888 char_u *numbuf,
7889 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007890{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007891 static int recurse = 0;
7892 char_u *r = NULL;
7893
Bram Moolenaar33570922005-01-25 22:26:29 +00007894 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007895 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007896 if (!did_echo_string_emsg)
7897 {
7898 /* Only give this message once for a recursive call to avoid
7899 * flooding the user with errors. And stop iterating over lists
7900 * and dicts. */
7901 did_echo_string_emsg = TRUE;
7902 EMSG(_("E724: variable nested too deep for displaying"));
7903 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007904 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007905 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007906 }
7907 ++recurse;
7908
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007909 switch (tv->v_type)
7910 {
7911 case VAR_FUNC:
7912 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007913 r = tv->vval.v_string;
7914 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007915
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007916 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007917 if (tv->vval.v_list == NULL)
7918 {
7919 *tofree = NULL;
7920 r = NULL;
7921 }
7922 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7923 {
7924 *tofree = NULL;
7925 r = (char_u *)"[...]";
7926 }
7927 else
7928 {
7929 tv->vval.v_list->lv_copyID = copyID;
7930 *tofree = list2string(tv, copyID);
7931 r = *tofree;
7932 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007933 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007934
Bram Moolenaar8c711452005-01-14 21:53:12 +00007935 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007936 if (tv->vval.v_dict == NULL)
7937 {
7938 *tofree = NULL;
7939 r = NULL;
7940 }
7941 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7942 {
7943 *tofree = NULL;
7944 r = (char_u *)"{...}";
7945 }
7946 else
7947 {
7948 tv->vval.v_dict->dv_copyID = copyID;
7949 *tofree = dict2string(tv, copyID);
7950 r = *tofree;
7951 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007952 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007953
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007954 case VAR_STRING:
7955 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007956 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007957 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007958 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007959 *tofree = NULL;
7960 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007961 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007962
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007963 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007964#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007965 *tofree = NULL;
7966 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7967 r = numbuf;
7968 break;
7969#endif
7970
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007971 case VAR_SPECIAL:
7972 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007973 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007974 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007975 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007976
Bram Moolenaar8502c702014-06-17 12:51:16 +02007977 if (--recurse == 0)
7978 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007979 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007980}
7981
7982/*
7983 * Return a string with the string representation of a variable.
7984 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7985 * "numbuf" is used for a number.
7986 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007987 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007988 */
7989 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007990tv2string(
7991 typval_T *tv,
7992 char_u **tofree,
7993 char_u *numbuf,
7994 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007995{
7996 switch (tv->v_type)
7997 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007998 case VAR_FUNC:
7999 *tofree = string_quote(tv->vval.v_string, TRUE);
8000 return *tofree;
8001 case VAR_STRING:
8002 *tofree = string_quote(tv->vval.v_string, FALSE);
8003 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008004#ifdef FEAT_FLOAT
8005 case VAR_FLOAT:
8006 *tofree = NULL;
8007 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8008 return numbuf;
8009#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008010 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008011 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008012 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008013 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008014 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008015 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008016 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008017 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008018 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008019 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008020}
8021
8022/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008023 * Return string "str" in ' quotes, doubling ' characters.
8024 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008025 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008026 */
8027 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008028string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008029{
Bram Moolenaar33570922005-01-25 22:26:29 +00008030 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008031 char_u *p, *r, *s;
8032
Bram Moolenaar33570922005-01-25 22:26:29 +00008033 len = (function ? 13 : 3);
8034 if (str != NULL)
8035 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008036 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008037 for (p = str; *p != NUL; mb_ptr_adv(p))
8038 if (*p == '\'')
8039 ++len;
8040 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008041 s = r = alloc(len);
8042 if (r != NULL)
8043 {
8044 if (function)
8045 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008046 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008047 r += 10;
8048 }
8049 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008050 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008051 if (str != NULL)
8052 for (p = str; *p != NUL; )
8053 {
8054 if (*p == '\'')
8055 *r++ = '\'';
8056 MB_COPY_CHAR(p, r);
8057 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008058 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008059 if (function)
8060 *r++ = ')';
8061 *r++ = NUL;
8062 }
8063 return s;
8064}
8065
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008066#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008067/*
8068 * Convert the string "text" to a floating point number.
8069 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8070 * this always uses a decimal point.
8071 * Returns the length of the text that was consumed.
8072 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008073 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008074string2float(
8075 char_u *text,
8076 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008077{
8078 char *s = (char *)text;
8079 float_T f;
8080
8081 f = strtod(s, &s);
8082 *value = f;
8083 return (int)((char_u *)s - text);
8084}
8085#endif
8086
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 * Get the value of an environment variable.
8089 * "arg" is pointing to the '$'. It is advanced to after the name.
8090 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008091 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 */
8093 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008094get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095{
8096 char_u *string = NULL;
8097 int len;
8098 int cc;
8099 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008100 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101
8102 ++*arg;
8103 name = *arg;
8104 len = get_env_len(arg);
8105 if (evaluate)
8106 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008107 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008108 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008109
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008110 cc = name[len];
8111 name[len] = NUL;
8112 /* first try vim_getenv(), fast for normal environment vars */
8113 string = vim_getenv(name, &mustfree);
8114 if (string != NULL && *string != NUL)
8115 {
8116 if (!mustfree)
8117 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008119 else
8120 {
8121 if (mustfree)
8122 vim_free(string);
8123
8124 /* next try expanding things like $VIM and ${HOME} */
8125 string = expand_env_save(name - 1);
8126 if (string != NULL && *string == '$')
8127 {
8128 vim_free(string);
8129 string = NULL;
8130 }
8131 }
8132 name[len] = cc;
8133
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008134 rettv->v_type = VAR_STRING;
8135 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 }
8137
8138 return OK;
8139}
8140
8141/*
8142 * Array with names and number of arguments of all internal functions
8143 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8144 */
8145static struct fst
8146{
8147 char *f_name; /* function name */
8148 char f_min_argc; /* minimal number of arguments */
8149 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008150 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008151 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152} functions[] =
8153{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008154#ifdef FEAT_FLOAT
8155 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008156 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008157#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008158 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008159 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008160 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 {"append", 2, 2, f_append},
8162 {"argc", 0, 0, f_argc},
8163 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008164 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008165 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008166#ifdef FEAT_FLOAT
8167 {"asin", 1, 1, f_asin}, /* WJMc */
8168#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008169 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008170 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008171 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008172 {"assert_false", 1, 2, f_assert_false},
8173 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008174#ifdef FEAT_FLOAT
8175 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008176 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008179 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 {"bufexists", 1, 1, f_bufexists},
8181 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8182 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8183 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8184 {"buflisted", 1, 1, f_buflisted},
8185 {"bufloaded", 1, 1, f_bufloaded},
8186 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008187 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 {"bufwinnr", 1, 1, f_bufwinnr},
8189 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008190 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008191 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008192 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008193#ifdef FEAT_FLOAT
8194 {"ceil", 1, 1, f_ceil},
8195#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008196#ifdef FEAT_CHANNEL
8197 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008198 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8199 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008200 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008201# ifdef FEAT_JOB
8202 {"ch_getjob", 1, 1, f_ch_getjob},
8203# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008204 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008205 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008206 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008207 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008208 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008209 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8210 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008211 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008212 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008213#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008214 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008215 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008217 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008219#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008220 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008221 {"complete_add", 1, 1, f_complete_add},
8222 {"complete_check", 0, 0, f_complete_check},
8223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008225 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008226#ifdef FEAT_FLOAT
8227 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008228 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008229#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008230 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008232 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008233 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008234 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008236 {"diff_filler", 1, 1, f_diff_filler},
8237 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008238 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008239 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008241 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 {"eventhandler", 0, 0, f_eventhandler},
8243 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008244 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008246#ifdef FEAT_FLOAT
8247 {"exp", 1, 1, f_exp},
8248#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008249 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008250 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008251 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8253 {"filereadable", 1, 1, f_filereadable},
8254 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008255 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008256 {"finddir", 1, 3, f_finddir},
8257 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008258#ifdef FEAT_FLOAT
8259 {"float2nr", 1, 1, f_float2nr},
8260 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008261 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008262#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008263 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 {"fnamemodify", 2, 2, f_fnamemodify},
8265 {"foldclosed", 1, 1, f_foldclosed},
8266 {"foldclosedend", 1, 1, f_foldclosedend},
8267 {"foldlevel", 1, 1, f_foldlevel},
8268 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008269 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008271 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008272 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008273 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008274 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008275 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 {"getchar", 0, 1, f_getchar},
8277 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008278 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 {"getcmdline", 0, 0, f_getcmdline},
8280 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008281 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008282 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008283 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008284 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008285 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008286 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 {"getfsize", 1, 1, f_getfsize},
8288 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008289 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008290 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008291 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008292 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008293 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008294 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008295 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008296 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008298 {"gettabvar", 2, 3, f_gettabvar},
8299 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 {"getwinposx", 0, 0, f_getwinposx},
8301 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008302 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008303 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008304 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008305 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008307 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008308 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008309 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8311 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8312 {"histadd", 2, 2, f_histadd},
8313 {"histdel", 1, 2, f_histdel},
8314 {"histget", 1, 2, f_histget},
8315 {"histnr", 1, 1, f_histnr},
8316 {"hlID", 1, 1, f_hlID},
8317 {"hlexists", 1, 1, f_hlexists},
8318 {"hostname", 0, 0, f_hostname},
8319 {"iconv", 3, 3, f_iconv},
8320 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008321 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008322 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008324 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 {"inputrestore", 0, 0, f_inputrestore},
8326 {"inputsave", 0, 0, f_inputsave},
8327 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008328 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008329 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008331 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008332#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8333 {"isnan", 1, 1, f_isnan},
8334#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008335 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008336#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008337# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008338 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008339# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +01008340 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008341 {"job_start", 1, 2, f_job_start},
8342 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008343 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008344#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008345 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008346 {"js_decode", 1, 1, f_js_decode},
8347 {"js_encode", 1, 1, f_js_encode},
8348 {"json_decode", 1, 1, f_json_decode},
8349 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008350 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008352 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 {"libcall", 3, 3, f_libcall},
8354 {"libcallnr", 3, 3, f_libcallnr},
8355 {"line", 1, 1, f_line},
8356 {"line2byte", 1, 1, f_line2byte},
8357 {"lispindent", 1, 1, f_lispindent},
8358 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008359#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008360 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008361 {"log10", 1, 1, f_log10},
8362#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008363#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008364 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008365#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008366 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008367 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008368 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008369 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008370 {"matchadd", 2, 5, f_matchadd},
8371 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008372 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008373 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008374 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008375 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008376 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008377 {"max", 1, 1, f_max},
8378 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008379#ifdef vim_mkdir
8380 {"mkdir", 1, 3, f_mkdir},
8381#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008382 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008383#ifdef FEAT_MZSCHEME
8384 {"mzeval", 1, 1, f_mzeval},
8385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008387 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008388 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008389 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008390#ifdef FEAT_PERL
8391 {"perleval", 1, 1, f_perleval},
8392#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008393#ifdef FEAT_FLOAT
8394 {"pow", 2, 2, f_pow},
8395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008397 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008398 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008399#ifdef FEAT_PYTHON3
8400 {"py3eval", 1, 1, f_py3eval},
8401#endif
8402#ifdef FEAT_PYTHON
8403 {"pyeval", 1, 1, f_pyeval},
8404#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008405 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008406 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008407 {"reltime", 0, 2, f_reltime},
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008408 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008409 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 {"remote_expr", 2, 3, f_remote_expr},
8411 {"remote_foreground", 1, 1, f_remote_foreground},
8412 {"remote_peek", 1, 2, f_remote_peek},
8413 {"remote_read", 1, 1, f_remote_read},
8414 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008415 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008417 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008419 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008420#ifdef FEAT_FLOAT
8421 {"round", 1, 1, f_round},
8422#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008423 {"screenattr", 2, 2, f_screenattr},
8424 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008425 {"screencol", 0, 0, f_screencol},
8426 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008427 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008428 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008429 {"searchpair", 3, 7, f_searchpair},
8430 {"searchpairpos", 3, 7, f_searchpairpos},
8431 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 {"server2client", 2, 2, f_server2client},
8433 {"serverlist", 0, 0, f_serverlist},
8434 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008435 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 {"setcmdpos", 1, 1, f_setcmdpos},
8437 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008438 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008439 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008440 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008441 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008443 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008444 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008446#ifdef FEAT_CRYPT
8447 {"sha256", 1, 1, f_sha256},
8448#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008449 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008450 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008452#ifdef FEAT_FLOAT
8453 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008454 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008455#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008456 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008457 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008458 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008459 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008460 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008461#ifdef FEAT_FLOAT
8462 {"sqrt", 1, 1, f_sqrt},
8463 {"str2float", 1, 1, f_str2float},
8464#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008465 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008466 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008467 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468#ifdef HAVE_STRFTIME
8469 {"strftime", 1, 2, f_strftime},
8470#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008471 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008472 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 {"strlen", 1, 1, f_strlen},
8474 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008475 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008477 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008478 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 {"substitute", 4, 4, f_substitute},
8480 {"synID", 3, 3, f_synID},
8481 {"synIDattr", 2, 3, f_synIDattr},
8482 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008483 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008484 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008485 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008486 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008487 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008488 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008489 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008490 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008491 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008492#ifdef FEAT_FLOAT
8493 {"tan", 1, 1, f_tan},
8494 {"tanh", 1, 1, f_tanh},
8495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008497 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 {"tolower", 1, 1, f_tolower},
8499 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008500 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008501#ifdef FEAT_FLOAT
8502 {"trunc", 1, 1, f_trunc},
8503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008505 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008506 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008507 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008508 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 {"virtcol", 1, 1, f_virtcol},
8510 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008511 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 {"winbufnr", 1, 1, f_winbufnr},
8513 {"wincol", 0, 0, f_wincol},
8514 {"winheight", 1, 1, f_winheight},
8515 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008516 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008518 {"winrestview", 1, 1, f_winrestview},
8519 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008521 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008522 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008523 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524};
8525
8526#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8527
8528/*
8529 * Function given to ExpandGeneric() to obtain the list of internal
8530 * or user defined function names.
8531 */
8532 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008533get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534{
8535 static int intidx = -1;
8536 char_u *name;
8537
8538 if (idx == 0)
8539 intidx = -1;
8540 if (intidx < 0)
8541 {
8542 name = get_user_func_name(xp, idx);
8543 if (name != NULL)
8544 return name;
8545 }
8546 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8547 {
8548 STRCPY(IObuff, functions[intidx].f_name);
8549 STRCAT(IObuff, "(");
8550 if (functions[intidx].f_max_argc == 0)
8551 STRCAT(IObuff, ")");
8552 return IObuff;
8553 }
8554
8555 return NULL;
8556}
8557
8558/*
8559 * Function given to ExpandGeneric() to obtain the list of internal or
8560 * user defined variable or function names.
8561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008563get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564{
8565 static int intidx = -1;
8566 char_u *name;
8567
8568 if (idx == 0)
8569 intidx = -1;
8570 if (intidx < 0)
8571 {
8572 name = get_function_name(xp, idx);
8573 if (name != NULL)
8574 return name;
8575 }
8576 return get_user_var_name(xp, ++intidx);
8577}
8578
8579#endif /* FEAT_CMDL_COMPL */
8580
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008581#if defined(EBCDIC) || defined(PROTO)
8582/*
8583 * Compare struct fst by function name.
8584 */
8585 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008586compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008587{
8588 struct fst *p1 = (struct fst *)s1;
8589 struct fst *p2 = (struct fst *)s2;
8590
8591 return STRCMP(p1->f_name, p2->f_name);
8592}
8593
8594/*
8595 * Sort the function table by function name.
8596 * The sorting of the table above is ASCII dependant.
8597 * On machines using EBCDIC we have to sort it.
8598 */
8599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008600sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008601{
8602 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8603
8604 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8605}
8606#endif
8607
8608
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609/*
8610 * Find internal function in table above.
8611 * Return index, or -1 if not found
8612 */
8613 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008614find_internal_func(
8615 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616{
8617 int first = 0;
8618 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8619 int cmp;
8620 int x;
8621
8622 /*
8623 * Find the function name in the table. Binary search.
8624 */
8625 while (first <= last)
8626 {
8627 x = first + ((unsigned)(last - first) >> 1);
8628 cmp = STRCMP(name, functions[x].f_name);
8629 if (cmp < 0)
8630 last = x - 1;
8631 else if (cmp > 0)
8632 first = x + 1;
8633 else
8634 return x;
8635 }
8636 return -1;
8637}
8638
8639/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008640 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8641 * name it contains, otherwise return "name".
8642 */
8643 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008644deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008645{
Bram Moolenaar33570922005-01-25 22:26:29 +00008646 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008647 int cc;
8648
8649 cc = name[*lenp];
8650 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008651 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008652 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008653 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008654 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008655 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008656 {
8657 *lenp = 0;
8658 return (char_u *)""; /* just in case */
8659 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008660 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008661 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008662 }
8663
8664 return name;
8665}
8666
8667/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 * Allocate a variable for the result of a function.
8669 * Return OK or FAIL.
8670 */
8671 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008672get_func_tv(
8673 char_u *name, /* name of the function */
8674 int len, /* length of "name" */
8675 typval_T *rettv,
8676 char_u **arg, /* argument, pointing to the '(' */
8677 linenr_T firstline, /* first line of range */
8678 linenr_T lastline, /* last line of range */
8679 int *doesrange, /* return: function handled range */
8680 int evaluate,
8681 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682{
8683 char_u *argp;
8684 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008685 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 int argcount = 0; /* number of arguments found */
8687
8688 /*
8689 * Get the arguments.
8690 */
8691 argp = *arg;
8692 while (argcount < MAX_FUNC_ARGS)
8693 {
8694 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8695 if (*argp == ')' || *argp == ',' || *argp == NUL)
8696 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8698 {
8699 ret = FAIL;
8700 break;
8701 }
8702 ++argcount;
8703 if (*argp != ',')
8704 break;
8705 }
8706 if (*argp == ')')
8707 ++argp;
8708 else
8709 ret = FAIL;
8710
8711 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008712 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008713 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008715 {
8716 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008717 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008718 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008719 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721
8722 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008723 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724
8725 *arg = skipwhite(argp);
8726 return ret;
8727}
8728
8729
8730/*
8731 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008732 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008733 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008735 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008736call_func(
8737 char_u *funcname, /* name of the function */
8738 int len, /* length of "name" */
8739 typval_T *rettv, /* return value goes here */
8740 int argcount, /* number of "argvars" */
8741 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008742 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008743 linenr_T firstline, /* first line of range */
8744 linenr_T lastline, /* last line of range */
8745 int *doesrange, /* return: function handled range */
8746 int evaluate,
8747 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748{
8749 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750#define ERROR_UNKNOWN 0
8751#define ERROR_TOOMANY 1
8752#define ERROR_TOOFEW 2
8753#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008754#define ERROR_DICT 4
8755#define ERROR_NONE 5
8756#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 int error = ERROR_NONE;
8758 int i;
8759 int llen;
8760 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761#define FLEN_FIXED 40
8762 char_u fname_buf[FLEN_FIXED + 1];
8763 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008764 char_u *name;
8765
8766 /* Make a copy of the name, if it comes from a funcref variable it could
8767 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008768 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008769 if (name == NULL)
8770 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771
8772 /*
8773 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8774 * Change <SNR>123_name() to K_SNR 123_name().
8775 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 llen = eval_fname_script(name);
8778 if (llen > 0)
8779 {
8780 fname_buf[0] = K_SPECIAL;
8781 fname_buf[1] = KS_EXTRA;
8782 fname_buf[2] = (int)KE_SNR;
8783 i = 3;
8784 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8785 {
8786 if (current_SID <= 0)
8787 error = ERROR_SCRIPT;
8788 else
8789 {
8790 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8791 i = (int)STRLEN(fname_buf);
8792 }
8793 }
8794 if (i + STRLEN(name + llen) < FLEN_FIXED)
8795 {
8796 STRCPY(fname_buf + i, name + llen);
8797 fname = fname_buf;
8798 }
8799 else
8800 {
8801 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8802 if (fname == NULL)
8803 error = ERROR_OTHER;
8804 else
8805 {
8806 mch_memmove(fname, fname_buf, (size_t)i);
8807 STRCPY(fname + i, name + llen);
8808 }
8809 }
8810 }
8811 else
8812 fname = name;
8813
8814 *doesrange = FALSE;
8815
8816
8817 /* execute the function if no errors detected and executing */
8818 if (evaluate && error == ERROR_NONE)
8819 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008820 char_u *rfname = fname;
8821
8822 /* Ignore "g:" before a function name. */
8823 if (fname[0] == 'g' && fname[1] == ':')
8824 rfname = fname + 2;
8825
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008826 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8827 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 error = ERROR_UNKNOWN;
8829
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008830 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 {
8832 /*
8833 * User defined function.
8834 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008835 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008836
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008838 /* Trigger FuncUndefined event, may load the function. */
8839 if (fp == NULL
8840 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008841 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008842 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008844 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008845 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 }
8847#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008848 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008849 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008850 {
8851 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008852 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008853 }
8854
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 if (fp != NULL)
8856 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008857 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008859 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008861 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008863 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008864 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 else
8866 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008867 int did_save_redo = FALSE;
8868
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 /*
8870 * Call the user function.
8871 * Save and restore search patterns, script variables and
8872 * redo buffer.
8873 */
8874 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008875#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008876 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008877#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008878 {
8879 saveRedobuff();
8880 did_save_redo = TRUE;
8881 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008882 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008883 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008884 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008885 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8886 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8887 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008888 /* Function was unreferenced while being used, free it
8889 * now. */
8890 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008891 if (did_save_redo)
8892 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 restore_search_patterns();
8894 error = ERROR_NONE;
8895 }
8896 }
8897 }
8898 else
8899 {
8900 /*
8901 * Find the function name in the table, call its implementation.
8902 */
8903 i = find_internal_func(fname);
8904 if (i >= 0)
8905 {
8906 if (argcount < functions[i].f_min_argc)
8907 error = ERROR_TOOFEW;
8908 else if (argcount > functions[i].f_max_argc)
8909 error = ERROR_TOOMANY;
8910 else
8911 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008912 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008913 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 error = ERROR_NONE;
8915 }
8916 }
8917 }
8918 /*
8919 * The function call (or "FuncUndefined" autocommand sequence) might
8920 * have been aborted by an error, an interrupt, or an explicitly thrown
8921 * exception that has not been caught so far. This situation can be
8922 * tested for by calling aborting(). For an error in an internal
8923 * function or for the "E132" error in call_user_func(), however, the
8924 * throw point at which the "force_abort" flag (temporarily reset by
8925 * emsg()) is normally updated has not been reached yet. We need to
8926 * update that flag first to make aborting() reliable.
8927 */
8928 update_force_abort();
8929 }
8930 if (error == ERROR_NONE)
8931 ret = OK;
8932
8933 /*
8934 * Report an error unless the argument evaluation or function call has been
8935 * cancelled due to an aborting error, an interrupt, or an exception.
8936 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008937 if (!aborting())
8938 {
8939 switch (error)
8940 {
8941 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008942 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008943 break;
8944 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008945 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008946 break;
8947 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008948 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008949 name);
8950 break;
8951 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008952 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008953 name);
8954 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008955 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008956 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008957 name);
8958 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008959 }
8960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 if (fname != name && fname != fname_buf)
8963 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008964 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965
8966 return ret;
8967}
8968
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008969/*
8970 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008971 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008972 */
8973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008974emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008975{
8976 char_u *p;
8977
8978 if (*name == K_SPECIAL)
8979 p = concat_str((char_u *)"<SNR>", name + 3);
8980 else
8981 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008982 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008983 if (p != name)
8984 vim_free(p);
8985}
8986
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008987/*
8988 * Return TRUE for a non-zero Number and a non-empty String.
8989 */
8990 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008991non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008992{
8993 return ((argvars[0].v_type == VAR_NUMBER
8994 && argvars[0].vval.v_number != 0)
8995 || (argvars[0].v_type == VAR_STRING
8996 && argvars[0].vval.v_string != NULL
8997 && *argvars[0].vval.v_string != NUL));
8998}
8999
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000/*********************************************
9001 * Implementation of the built-in functions
9002 */
9003
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009004#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009005static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009006
9007/*
9008 * Get the float value of "argvars[0]" into "f".
9009 * Returns FAIL when the argument is not a Number or Float.
9010 */
9011 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009012get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009013{
9014 if (argvars[0].v_type == VAR_FLOAT)
9015 {
9016 *f = argvars[0].vval.v_float;
9017 return OK;
9018 }
9019 if (argvars[0].v_type == VAR_NUMBER)
9020 {
9021 *f = (float_T)argvars[0].vval.v_number;
9022 return OK;
9023 }
9024 EMSG(_("E808: Number or Float required"));
9025 return FAIL;
9026}
9027
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009028/*
9029 * "abs(expr)" function
9030 */
9031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009032f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009033{
9034 if (argvars[0].v_type == VAR_FLOAT)
9035 {
9036 rettv->v_type = VAR_FLOAT;
9037 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9038 }
9039 else
9040 {
9041 varnumber_T n;
9042 int error = FALSE;
9043
9044 n = get_tv_number_chk(&argvars[0], &error);
9045 if (error)
9046 rettv->vval.v_number = -1;
9047 else if (n > 0)
9048 rettv->vval.v_number = n;
9049 else
9050 rettv->vval.v_number = -n;
9051 }
9052}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009053
9054/*
9055 * "acos()" function
9056 */
9057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009058f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009059{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009060 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009061
9062 rettv->v_type = VAR_FLOAT;
9063 if (get_float_arg(argvars, &f) == OK)
9064 rettv->vval.v_float = acos(f);
9065 else
9066 rettv->vval.v_float = 0.0;
9067}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009068#endif
9069
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009071 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 */
9073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009074f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075{
Bram Moolenaar33570922005-01-25 22:26:29 +00009076 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009078 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009079 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009081 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009082 && !tv_check_lock(l->lv_lock,
9083 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009084 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009085 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009086 }
9087 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009088 EMSG(_(e_listreq));
9089}
9090
9091/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009092 * "alloc_fail(id, countdown, repeat)" function
9093 */
9094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009095f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009096{
9097 if (argvars[0].v_type != VAR_NUMBER
9098 || argvars[0].vval.v_number <= 0
9099 || argvars[1].v_type != VAR_NUMBER
9100 || argvars[1].vval.v_number < 0
9101 || argvars[2].v_type != VAR_NUMBER)
9102 EMSG(_(e_invarg));
9103 else
9104 {
9105 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009106 if (alloc_fail_id >= aid_last)
9107 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009108 alloc_fail_countdown = argvars[1].vval.v_number;
9109 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009110 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009111 }
9112}
9113
9114/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009115 * "and(expr, expr)" function
9116 */
9117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009118f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009119{
9120 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9121 & get_tv_number_chk(&argvars[1], NULL);
9122}
9123
9124/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009125 * "append(lnum, string/list)" function
9126 */
9127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009128f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009129{
9130 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009131 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009132 list_T *l = NULL;
9133 listitem_T *li = NULL;
9134 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009135 long added = 0;
9136
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009137 /* When coming here from Insert mode, sync undo, so that this can be
9138 * undone separately from what was previously inserted. */
9139 if (u_sync_once == 2)
9140 {
9141 u_sync_once = 1; /* notify that u_sync() was called */
9142 u_sync(TRUE);
9143 }
9144
Bram Moolenaar0d660222005-01-07 21:51:51 +00009145 lnum = get_tv_lnum(argvars);
9146 if (lnum >= 0
9147 && lnum <= curbuf->b_ml.ml_line_count
9148 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009149 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009150 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009151 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009152 l = argvars[1].vval.v_list;
9153 if (l == NULL)
9154 return;
9155 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009156 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009157 for (;;)
9158 {
9159 if (l == NULL)
9160 tv = &argvars[1]; /* append a string */
9161 else if (li == NULL)
9162 break; /* end of list */
9163 else
9164 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009165 line = get_tv_string_chk(tv);
9166 if (line == NULL) /* type error */
9167 {
9168 rettv->vval.v_number = 1; /* Failed */
9169 break;
9170 }
9171 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009172 ++added;
9173 if (l == NULL)
9174 break;
9175 li = li->li_next;
9176 }
9177
9178 appended_lines_mark(lnum, added);
9179 if (curwin->w_cursor.lnum > lnum)
9180 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009182 else
9183 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184}
9185
9186/*
9187 * "argc()" function
9188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009190f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009192 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193}
9194
9195/*
9196 * "argidx()" function
9197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009199f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009201 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202}
9203
9204/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009205 * "arglistid()" function
9206 */
9207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009208f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009209{
9210 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009211
9212 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009213 wp = find_tabwin(&argvars[0], &argvars[1]);
9214 if (wp != NULL)
9215 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009216}
9217
9218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 * "argv(nr)" function
9220 */
9221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009222f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223{
9224 int idx;
9225
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009226 if (argvars[0].v_type != VAR_UNKNOWN)
9227 {
9228 idx = get_tv_number_chk(&argvars[0], NULL);
9229 if (idx >= 0 && idx < ARGCOUNT)
9230 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9231 else
9232 rettv->vval.v_string = NULL;
9233 rettv->v_type = VAR_STRING;
9234 }
9235 else if (rettv_list_alloc(rettv) == OK)
9236 for (idx = 0; idx < ARGCOUNT; ++idx)
9237 list_append_string(rettv->vval.v_list,
9238 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239}
9240
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009241static void prepare_assert_error(garray_T*gap);
9242static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9243static void assert_error(garray_T *gap);
9244static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009245
9246/*
9247 * Prepare "gap" for an assert error and add the sourcing position.
9248 */
9249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009250prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009251{
9252 char buf[NUMBUFLEN];
9253
9254 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009255 if (sourcing_name != NULL)
9256 {
9257 ga_concat(gap, sourcing_name);
9258 if (sourcing_lnum > 0)
9259 ga_concat(gap, (char_u *)" ");
9260 }
9261 if (sourcing_lnum > 0)
9262 {
9263 sprintf(buf, "line %ld", (long)sourcing_lnum);
9264 ga_concat(gap, (char_u *)buf);
9265 }
9266 if (sourcing_name != NULL || sourcing_lnum > 0)
9267 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009268}
9269
9270/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009271 * Append "str" to "gap", escaping unprintable characters.
9272 * Changes NL to \n, CR to \r, etc.
9273 */
9274 static void
9275ga_concat_esc(garray_T *gap, char_u *str)
9276{
9277 char_u *p;
9278 char_u buf[NUMBUFLEN];
9279
9280 for (p = str; *p != NUL; ++p)
9281 switch (*p)
9282 {
9283 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9284 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9285 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9286 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9287 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9288 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9289 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9290 default:
9291 if (*p < ' ')
9292 {
9293 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9294 ga_concat(gap, buf);
9295 }
9296 else
9297 ga_append(gap, *p);
9298 break;
9299 }
9300}
9301
9302/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009303 * Fill "gap" with information about an assert error.
9304 */
9305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009306fill_assert_error(
9307 garray_T *gap,
9308 typval_T *opt_msg_tv,
9309 char_u *exp_str,
9310 typval_T *exp_tv,
9311 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009312{
9313 char_u numbuf[NUMBUFLEN];
9314 char_u *tofree;
9315
9316 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9317 {
9318 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9319 vim_free(tofree);
9320 }
9321 else
9322 {
9323 ga_concat(gap, (char_u *)"Expected ");
9324 if (exp_str == NULL)
9325 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009326 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009327 vim_free(tofree);
9328 }
9329 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009330 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009331 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009332 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009333 vim_free(tofree);
9334 }
9335}
Bram Moolenaar43345542015-11-29 17:35:35 +01009336
9337/*
9338 * Add an assert error to v:errors.
9339 */
9340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009341assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009342{
9343 struct vimvar *vp = &vimvars[VV_ERRORS];
9344
9345 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9346 /* Make sure v:errors is a list. */
9347 set_vim_var_list(VV_ERRORS, list_alloc());
9348 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9349}
9350
Bram Moolenaar43345542015-11-29 17:35:35 +01009351/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009352 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009353 */
9354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009355f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009356{
9357 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009358
9359 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9360 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009361 prepare_assert_error(&ga);
9362 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9363 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009364 ga_clear(&ga);
9365 }
9366}
9367
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009368/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009369 * "assert_exception(string[, msg])" function
9370 */
9371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009372f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009373{
9374 garray_T ga;
9375 char *error;
9376
9377 error = (char *)get_tv_string_chk(&argvars[0]);
9378 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9379 {
9380 prepare_assert_error(&ga);
9381 ga_concat(&ga, (char_u *)"v:exception is not set");
9382 assert_error(&ga);
9383 ga_clear(&ga);
9384 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009385 else if (error != NULL
9386 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009387 {
9388 prepare_assert_error(&ga);
9389 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9390 &vimvars[VV_EXCEPTION].vv_tv);
9391 assert_error(&ga);
9392 ga_clear(&ga);
9393 }
9394}
9395
9396/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009397 * "assert_fails(cmd [, error])" function
9398 */
9399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009400f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009401{
9402 char_u *cmd = get_tv_string_chk(&argvars[0]);
9403 garray_T ga;
9404
9405 called_emsg = FALSE;
9406 suppress_errthrow = TRUE;
9407 emsg_silent = TRUE;
9408 do_cmdline_cmd(cmd);
9409 if (!called_emsg)
9410 {
9411 prepare_assert_error(&ga);
9412 ga_concat(&ga, (char_u *)"command did not fail: ");
9413 ga_concat(&ga, cmd);
9414 assert_error(&ga);
9415 ga_clear(&ga);
9416 }
9417 else if (argvars[1].v_type != VAR_UNKNOWN)
9418 {
9419 char_u buf[NUMBUFLEN];
9420 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9421
9422 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9423 {
9424 prepare_assert_error(&ga);
9425 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9426 &vimvars[VV_ERRMSG].vv_tv);
9427 assert_error(&ga);
9428 ga_clear(&ga);
9429 }
9430 }
9431
9432 called_emsg = FALSE;
9433 suppress_errthrow = FALSE;
9434 emsg_silent = FALSE;
9435 emsg_on_display = FALSE;
9436 set_vim_var_string(VV_ERRMSG, NULL, 0);
9437}
9438
9439/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009440 * Common for assert_true() and assert_false().
9441 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009443assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009444{
9445 int error = FALSE;
9446 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009447
Bram Moolenaar37127922016-02-06 20:29:28 +01009448 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009449 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009450 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009451 if (argvars[0].v_type != VAR_NUMBER
9452 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9453 || error)
9454 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009455 prepare_assert_error(&ga);
9456 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009457 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009458 NULL, &argvars[0]);
9459 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009460 ga_clear(&ga);
9461 }
9462}
9463
9464/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009465 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009466 */
9467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009468f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009469{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009470 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009471}
9472
9473/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009474 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009475 */
9476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009477f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009478{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009479 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009480}
9481
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009482#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009483/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009484 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009485 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009487f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009488{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009489 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009490
9491 rettv->v_type = VAR_FLOAT;
9492 if (get_float_arg(argvars, &f) == OK)
9493 rettv->vval.v_float = asin(f);
9494 else
9495 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009496}
9497
9498/*
9499 * "atan()" function
9500 */
9501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009502f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009503{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009504 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009505
9506 rettv->v_type = VAR_FLOAT;
9507 if (get_float_arg(argvars, &f) == OK)
9508 rettv->vval.v_float = atan(f);
9509 else
9510 rettv->vval.v_float = 0.0;
9511}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009512
9513/*
9514 * "atan2()" function
9515 */
9516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009517f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009518{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009519 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009520
9521 rettv->v_type = VAR_FLOAT;
9522 if (get_float_arg(argvars, &fx) == OK
9523 && get_float_arg(&argvars[1], &fy) == OK)
9524 rettv->vval.v_float = atan2(fx, fy);
9525 else
9526 rettv->vval.v_float = 0.0;
9527}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009528#endif
9529
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530/*
9531 * "browse(save, title, initdir, default)" function
9532 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009534f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535{
9536#ifdef FEAT_BROWSE
9537 int save;
9538 char_u *title;
9539 char_u *initdir;
9540 char_u *defname;
9541 char_u buf[NUMBUFLEN];
9542 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009543 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009545 save = get_tv_number_chk(&argvars[0], &error);
9546 title = get_tv_string_chk(&argvars[1]);
9547 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9548 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009550 if (error || title == NULL || initdir == NULL || defname == NULL)
9551 rettv->vval.v_string = NULL;
9552 else
9553 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009554 do_browse(save ? BROWSE_SAVE : 0,
9555 title, defname, NULL, initdir, NULL, curbuf);
9556#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009557 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009558#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009559 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009560}
9561
9562/*
9563 * "browsedir(title, initdir)" function
9564 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009566f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009567{
9568#ifdef FEAT_BROWSE
9569 char_u *title;
9570 char_u *initdir;
9571 char_u buf[NUMBUFLEN];
9572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009573 title = get_tv_string_chk(&argvars[0]);
9574 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009575
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009576 if (title == NULL || initdir == NULL)
9577 rettv->vval.v_string = NULL;
9578 else
9579 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009580 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009582 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009584 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585}
9586
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009587static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009588
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589/*
9590 * Find a buffer by number or exact name.
9591 */
9592 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009593find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594{
9595 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009597 if (avar->v_type == VAR_NUMBER)
9598 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009599 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009601 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009602 if (buf == NULL)
9603 {
9604 /* No full path name match, try a match with a URL or a "nofile"
9605 * buffer, these don't use the full path. */
9606 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9607 if (buf->b_fname != NULL
9608 && (path_with_url(buf->b_fname)
9609#ifdef FEAT_QUICKFIX
9610 || bt_nofile(buf)
9611#endif
9612 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009613 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009614 break;
9615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 }
9617 return buf;
9618}
9619
9620/*
9621 * "bufexists(expr)" function
9622 */
9623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009624f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009626 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627}
9628
9629/*
9630 * "buflisted(expr)" function
9631 */
9632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009633f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634{
9635 buf_T *buf;
9636
9637 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639}
9640
9641/*
9642 * "bufloaded(expr)" function
9643 */
9644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009645f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646{
9647 buf_T *buf;
9648
9649 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009650 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651}
9652
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009653static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009654
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655/*
9656 * Get buffer by number or pattern.
9657 */
9658 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009659get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 int save_magic;
9663 char_u *save_cpo;
9664 buf_T *buf;
9665
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009666 if (tv->v_type == VAR_NUMBER)
9667 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009668 if (tv->v_type != VAR_STRING)
9669 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 if (name == NULL || *name == NUL)
9671 return curbuf;
9672 if (name[0] == '$' && name[1] == NUL)
9673 return lastbuf;
9674
9675 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9676 save_magic = p_magic;
9677 p_magic = TRUE;
9678 save_cpo = p_cpo;
9679 p_cpo = (char_u *)"";
9680
9681 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009682 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683
9684 p_magic = save_magic;
9685 p_cpo = save_cpo;
9686
9687 /* If not found, try expanding the name, like done for bufexists(). */
9688 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009689 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690
9691 return buf;
9692}
9693
9694/*
9695 * "bufname(expr)" function
9696 */
9697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009698f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699{
9700 buf_T *buf;
9701
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009702 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009704 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009705 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009707 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009709 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 --emsg_off;
9711}
9712
9713/*
9714 * "bufnr(expr)" function
9715 */
9716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009717f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718{
9719 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009720 int error = FALSE;
9721 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009723 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009725 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009726 --emsg_off;
9727
9728 /* If the buffer isn't found and the second argument is not zero create a
9729 * new buffer. */
9730 if (buf == NULL
9731 && argvars[1].v_type != VAR_UNKNOWN
9732 && get_tv_number_chk(&argvars[1], &error) != 0
9733 && !error
9734 && (name = get_tv_string_chk(&argvars[0])) != NULL
9735 && !error)
9736 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9737
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009739 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009741 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742}
9743
9744/*
9745 * "bufwinnr(nr)" function
9746 */
9747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009748f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749{
9750#ifdef FEAT_WINDOWS
9751 win_T *wp;
9752 int winnr = 0;
9753#endif
9754 buf_T *buf;
9755
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009756 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009758 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759#ifdef FEAT_WINDOWS
9760 for (wp = firstwin; wp; wp = wp->w_next)
9761 {
9762 ++winnr;
9763 if (wp->w_buffer == buf)
9764 break;
9765 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009766 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009768 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769#endif
9770 --emsg_off;
9771}
9772
9773/*
9774 * "byte2line(byte)" function
9775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009777f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778{
9779#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009780 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781#else
9782 long boff = 0;
9783
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009784 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009786 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009788 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 (linenr_T)0, &boff);
9790#endif
9791}
9792
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009794byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009795{
9796#ifdef FEAT_MBYTE
9797 char_u *t;
9798#endif
9799 char_u *str;
9800 long idx;
9801
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009802 str = get_tv_string_chk(&argvars[0]);
9803 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009804 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009805 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009806 return;
9807
9808#ifdef FEAT_MBYTE
9809 t = str;
9810 for ( ; idx > 0; idx--)
9811 {
9812 if (*t == NUL) /* EOL reached */
9813 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009814 if (enc_utf8 && comp)
9815 t += utf_ptr2len(t);
9816 else
9817 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009818 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009819 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009820#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009821 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009822 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009823#endif
9824}
9825
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009826/*
9827 * "byteidx()" function
9828 */
9829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009830f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009831{
9832 byteidx(argvars, rettv, FALSE);
9833}
9834
9835/*
9836 * "byteidxcomp()" function
9837 */
9838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009839f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009840{
9841 byteidx(argvars, rettv, TRUE);
9842}
9843
Bram Moolenaardb913952012-06-29 12:54:53 +02009844 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009845func_call(
9846 char_u *name,
9847 typval_T *args,
9848 dict_T *selfdict,
9849 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009850{
9851 listitem_T *item;
9852 typval_T argv[MAX_FUNC_ARGS + 1];
9853 int argc = 0;
9854 int dummy;
9855 int r = 0;
9856
9857 for (item = args->vval.v_list->lv_first; item != NULL;
9858 item = item->li_next)
9859 {
9860 if (argc == MAX_FUNC_ARGS)
9861 {
9862 EMSG(_("E699: Too many arguments"));
9863 break;
9864 }
9865 /* Make a copy of each argument. This is needed to be able to set
9866 * v_lock to VAR_FIXED in the copy without changing the original list.
9867 */
9868 copy_tv(&item->li_tv, &argv[argc++]);
9869 }
9870
9871 if (item == NULL)
9872 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9873 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9874 &dummy, TRUE, selfdict);
9875
9876 /* Free the arguments. */
9877 while (argc > 0)
9878 clear_tv(&argv[--argc]);
9879
9880 return r;
9881}
9882
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009883/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009884 * "call(func, arglist)" function
9885 */
9886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009887f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009888{
9889 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009890 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009891
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009892 if (argvars[1].v_type != VAR_LIST)
9893 {
9894 EMSG(_(e_listreq));
9895 return;
9896 }
9897 if (argvars[1].vval.v_list == NULL)
9898 return;
9899
9900 if (argvars[0].v_type == VAR_FUNC)
9901 func = argvars[0].vval.v_string;
9902 else
9903 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009904 if (*func == NUL)
9905 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009906
Bram Moolenaare9a41262005-01-15 22:18:47 +00009907 if (argvars[2].v_type != VAR_UNKNOWN)
9908 {
9909 if (argvars[2].v_type != VAR_DICT)
9910 {
9911 EMSG(_(e_dictreq));
9912 return;
9913 }
9914 selfdict = argvars[2].vval.v_dict;
9915 }
9916
Bram Moolenaardb913952012-06-29 12:54:53 +02009917 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009918}
9919
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009920#ifdef FEAT_FLOAT
9921/*
9922 * "ceil({float})" function
9923 */
9924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009925f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009926{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009927 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009928
9929 rettv->v_type = VAR_FLOAT;
9930 if (get_float_arg(argvars, &f) == OK)
9931 rettv->vval.v_float = ceil(f);
9932 else
9933 rettv->vval.v_float = 0.0;
9934}
9935#endif
9936
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009937#if defined(FEAT_CHANNEL) || defined(FEAT_JOB)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009938/*
9939 * Get a callback from "arg". It can be a Funcref or a function name.
9940 * When "arg" is zero return an empty string.
9941 * Return NULL for an invalid argument.
9942 */
9943 static char_u *
9944get_callback(typval_T *arg)
9945{
9946 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9947 return arg->vval.v_string;
9948 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9949 return (char_u *)"";
9950 EMSG(_("E999: Invalid callback argument"));
9951 return NULL;
9952}
9953
Bram Moolenaarb6b52522016-02-20 23:30:07 +01009954 static int
9955handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
9956{
9957 char_u *val = get_tv_string(item);
9958
9959 opt->jo_set |= jo;
9960 if (STRCMP(val, "nl") == 0)
9961 *modep = MODE_NL;
9962 else if (STRCMP(val, "raw") == 0)
9963 *modep = MODE_RAW;
9964 else if (STRCMP(val, "js") == 0)
9965 *modep = MODE_JS;
9966 else if (STRCMP(val, "json") == 0)
9967 *modep = MODE_JSON;
9968 else
9969 {
9970 EMSG2(_(e_invarg2), val);
9971 return FAIL;
9972 }
9973 return OK;
9974}
9975
Bram Moolenaar187db502016-02-27 14:44:26 +01009976 static int
9977handle_io(typval_T *item, int part, jobopt_T *opt)
9978{
9979 char_u *val = get_tv_string(item);
9980
9981 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
9982 if (STRCMP(val, "null") == 0)
9983 opt->jo_io[part] = JIO_NULL;
9984 else if (STRCMP(val, "pipe") == 0)
9985 opt->jo_io[part] = JIO_PIPE;
9986 else if (STRCMP(val, "file") == 0)
9987 opt->jo_io[part] = JIO_FILE;
9988 else if (STRCMP(val, "buffer") == 0)
9989 opt->jo_io[part] = JIO_BUFFER;
9990 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
9991 opt->jo_io[part] = JIO_OUT;
9992 else
9993 {
9994 EMSG2(_(e_invarg2), val);
9995 return FAIL;
9996 }
9997 return OK;
9998}
9999
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010000 static void
10001clear_job_options(jobopt_T *opt)
10002{
10003 vim_memset(opt, 0, sizeof(jobopt_T));
10004}
10005
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010006/*
Bram Moolenaar187db502016-02-27 14:44:26 +010010007 * Get the PART_ number from the first character of an option name.
10008 */
10009 static int
10010part_from_char(int c)
10011{
10012 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
10013}
10014
10015/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010016 * Get the option entries from the dict in "tv", parse them and put the result
10017 * in "opt".
10018 * Only accept options in "supported".
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010019 * If an option value is invalid return FAIL.
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010020 */
10021 static int
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010022get_job_options(typval_T *tv, jobopt_T *opt, int supported)
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010023{
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010024 typval_T *item;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010025 char_u *val;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010026 dict_T *dict;
10027 int todo;
10028 hashitem_T *hi;
Bram Moolenaar187db502016-02-27 14:44:26 +010010029 int part;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010030
Bram Moolenaar8600ace2016-02-19 23:31:40 +010010031 opt->jo_set = 0;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010032 if (tv->v_type == VAR_UNKNOWN)
10033 return OK;
10034 if (tv->v_type != VAR_DICT)
10035 {
10036 EMSG(_(e_invarg));
10037 return FAIL;
10038 }
10039 dict = tv->vval.v_dict;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010040 if (dict == NULL)
10041 return OK;
10042
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010043 todo = (int)dict->dv_hashtab.ht_used;
10044 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
10045 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010046 {
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010047 item = &HI2DI(hi)->di_tv;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010048
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010049 if (STRCMP(hi->hi_key, "mode") == 0)
10050 {
10051 if (!(supported & JO_MODE))
10052 break;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010053 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010054 return FAIL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010055 }
10056 else if (STRCMP(hi->hi_key, "in-mode") == 0)
10057 {
10058 if (!(supported & JO_IN_MODE))
10059 break;
10060 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
10061 == FAIL)
10062 return FAIL;
10063 }
10064 else if (STRCMP(hi->hi_key, "out-mode") == 0)
10065 {
10066 if (!(supported & JO_OUT_MODE))
10067 break;
10068 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
10069 == FAIL)
10070 return FAIL;
10071 }
10072 else if (STRCMP(hi->hi_key, "err-mode") == 0)
10073 {
10074 if (!(supported & JO_ERR_MODE))
10075 break;
10076 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
10077 == FAIL)
10078 return FAIL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010079 }
Bram Moolenaar187db502016-02-27 14:44:26 +010010080 else if (STRCMP(hi->hi_key, "in-io") == 0
10081 || STRCMP(hi->hi_key, "out-io") == 0
10082 || STRCMP(hi->hi_key, "err-io") == 0)
10083 {
10084 if (!(supported & JO_OUT_IO))
10085 break;
10086 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
10087 return FAIL;
10088 }
10089 else if (STRCMP(hi->hi_key, "in-name") == 0
10090 || STRCMP(hi->hi_key, "out-name") == 0
10091 || STRCMP(hi->hi_key, "err-name") == 0)
10092 {
10093 part = part_from_char(*hi->hi_key);
10094
10095 if (!(supported & JO_OUT_IO))
10096 break;
10097 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
10098 opt->jo_io_name[part] =
10099 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
10100 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010101 else if (STRCMP(hi->hi_key, "callback") == 0)
10102 {
10103 if (!(supported & JO_CALLBACK))
10104 break;
10105 opt->jo_set |= JO_CALLBACK;
10106 opt->jo_callback = get_callback(item);
10107 if (opt->jo_callback == NULL)
10108 {
10109 EMSG2(_(e_invarg2), "callback");
10110 return FAIL;
10111 }
10112 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010113 else if (STRCMP(hi->hi_key, "out-cb") == 0)
10114 {
10115 if (!(supported & JO_OUT_CALLBACK))
10116 break;
10117 opt->jo_set |= JO_OUT_CALLBACK;
10118 opt->jo_out_cb = get_callback(item);
10119 if (opt->jo_out_cb == NULL)
10120 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010121 EMSG2(_(e_invarg2), "out-cb");
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010122 return FAIL;
10123 }
10124 }
10125 else if (STRCMP(hi->hi_key, "err-cb") == 0)
10126 {
10127 if (!(supported & JO_ERR_CALLBACK))
10128 break;
10129 opt->jo_set |= JO_ERR_CALLBACK;
10130 opt->jo_err_cb = get_callback(item);
10131 if (opt->jo_err_cb == NULL)
10132 {
10133 EMSG2(_(e_invarg2), "err-cb");
10134 return FAIL;
10135 }
10136 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +010010137 else if (STRCMP(hi->hi_key, "close-cb") == 0)
10138 {
10139 if (!(supported & JO_CLOSE_CALLBACK))
10140 break;
10141 opt->jo_set |= JO_CLOSE_CALLBACK;
10142 opt->jo_close_cb = get_callback(item);
10143 if (opt->jo_close_cb == NULL)
10144 {
10145 EMSG2(_(e_invarg2), "close-cb");
10146 return FAIL;
10147 }
10148 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010149 else if (STRCMP(hi->hi_key, "waittime") == 0)
10150 {
10151 if (!(supported & JO_WAITTIME))
10152 break;
10153 opt->jo_set |= JO_WAITTIME;
10154 opt->jo_waittime = get_tv_number(item);
10155 }
10156 else if (STRCMP(hi->hi_key, "timeout") == 0)
10157 {
10158 if (!(supported & JO_TIMEOUT))
10159 break;
10160 opt->jo_set |= JO_TIMEOUT;
10161 opt->jo_timeout = get_tv_number(item);
10162 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010163 else if (STRCMP(hi->hi_key, "out-timeout") == 0)
10164 {
10165 if (!(supported & JO_OUT_TIMEOUT))
10166 break;
10167 opt->jo_set |= JO_OUT_TIMEOUT;
10168 opt->jo_out_timeout = get_tv_number(item);
10169 }
10170 else if (STRCMP(hi->hi_key, "err-timeout") == 0)
10171 {
10172 if (!(supported & JO_ERR_TIMEOUT))
10173 break;
10174 opt->jo_set |= JO_ERR_TIMEOUT;
10175 opt->jo_err_timeout = get_tv_number(item);
10176 }
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010177 else if (STRCMP(hi->hi_key, "part") == 0)
10178 {
10179 if (!(supported & JO_PART))
10180 break;
10181 opt->jo_set |= JO_PART;
10182 val = get_tv_string(item);
10183 if (STRCMP(val, "err") == 0)
10184 opt->jo_part = PART_ERR;
10185 else
10186 {
10187 EMSG2(_(e_invarg2), val);
10188 return FAIL;
10189 }
10190 }
10191 else if (STRCMP(hi->hi_key, "id") == 0)
10192 {
10193 if (!(supported & JO_ID))
10194 break;
10195 opt->jo_set |= JO_ID;
10196 opt->jo_id = get_tv_number(item);
10197 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010010198 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
10199 {
10200 if (!(supported & JO_STOPONEXIT))
10201 break;
10202 opt->jo_set |= JO_STOPONEXIT;
10203 opt->jo_stoponexit = get_tv_string_buf_chk(item,
10204 opt->jo_soe_buf);
10205 if (opt->jo_stoponexit == NULL)
10206 {
10207 EMSG2(_(e_invarg2), "stoponexit");
10208 return FAIL;
10209 }
10210 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010211 else if (STRCMP(hi->hi_key, "exit-cb") == 0)
10212 {
10213 if (!(supported & JO_EXIT_CB))
10214 break;
10215 opt->jo_set |= JO_EXIT_CB;
10216 opt->jo_exit_cb = get_tv_string_buf_chk(item, opt->jo_ecb_buf);
Bram Moolenaar5e838402016-02-21 23:12:41 +010010217 if (opt->jo_exit_cb == NULL)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010218 {
10219 EMSG2(_(e_invarg2), "exit-cb");
10220 return FAIL;
10221 }
10222 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010223 else
10224 break;
10225 --todo;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010226 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010227 if (todo > 0)
10228 {
10229 EMSG2(_(e_invarg2), hi->hi_key);
10230 return FAIL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010231 }
10232
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010233 return OK;
10234}
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010235#endif
10236
10237#ifdef FEAT_CHANNEL
10238/*
10239 * Get the channel from the argument.
10240 * Returns NULL if the handle is invalid.
10241 */
10242 static channel_T *
10243get_channel_arg(typval_T *tv)
10244{
10245 channel_T *channel;
10246
10247 if (tv->v_type != VAR_CHANNEL)
10248 {
10249 EMSG2(_(e_invarg2), get_tv_string(tv));
10250 return NULL;
10251 }
10252 channel = tv->vval.v_channel;
10253
10254 if (channel == NULL || !channel_is_open(channel))
10255 {
10256 EMSG(_("E906: not an open channel"));
10257 return NULL;
10258 }
10259 return channel;
10260}
10261
10262/*
10263 * "ch_close()" function
10264 */
10265 static void
10266f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10267{
10268 channel_T *channel = get_channel_arg(&argvars[0]);
10269
10270 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010271 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010272 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010273 channel_clear(channel);
10274 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010275}
10276
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010277/*
10278 * "ch_getbufnr()" function
10279 */
10280 static void
10281f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10282{
10283 channel_T *channel = get_channel_arg(&argvars[0]);
10284
10285 rettv->vval.v_number = -1;
10286 if (channel != NULL)
10287 {
10288 char_u *what = get_tv_string(&argvars[1]);
10289 int part;
10290
10291 if (STRCMP(what, "err") == 0)
10292 part = PART_ERR;
10293 else if (STRCMP(what, "out") == 0)
10294 part = PART_OUT;
10295 else if (STRCMP(what, "in") == 0)
10296 part = PART_IN;
10297 else
10298 part = PART_SOCK;
10299 if (channel->ch_part[part].ch_buffer != NULL)
10300 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10301 }
10302}
10303
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010304# ifdef FEAT_JOB
10305/*
10306 * "ch_getjob()" function
10307 */
10308 static void
10309f_ch_getjob(typval_T *argvars, typval_T *rettv)
10310{
10311 channel_T *channel = get_channel_arg(&argvars[0]);
10312
10313 if (channel != NULL)
10314 {
10315 rettv->v_type = VAR_JOB;
10316 rettv->vval.v_job = channel->ch_job;
10317 if (channel->ch_job != NULL)
10318 ++channel->ch_job->jv_refcount;
10319 }
10320}
10321# endif
10322
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010323/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010324 * "ch_log()" function
10325 */
10326 static void
10327f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10328{
10329 char_u *msg = get_tv_string(&argvars[0]);
10330 channel_T *channel = NULL;
10331
10332 if (argvars[1].v_type != VAR_UNKNOWN)
10333 channel = get_channel_arg(&argvars[1]);
10334
10335 ch_log(channel, (char *)msg);
10336}
10337
10338/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010339 * "ch_logfile()" function
10340 */
10341 static void
10342f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10343{
10344 char_u *fname;
10345 char_u *opt = (char_u *)"";
10346 char_u buf[NUMBUFLEN];
10347 FILE *file = NULL;
10348
10349 fname = get_tv_string(&argvars[0]);
10350 if (argvars[1].v_type == VAR_STRING)
10351 opt = get_tv_string_buf(&argvars[1], buf);
10352 if (*fname != NUL)
10353 {
10354 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
10355 if (file == NULL)
10356 {
10357 EMSG2(_(e_notopen), fname);
10358 return;
10359 }
10360 }
10361 ch_logfile(file);
10362}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010363
10364/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010365 * "ch_open()" function
10366 */
10367 static void
10368f_ch_open(typval_T *argvars, typval_T *rettv)
10369{
10370 char_u *address;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010371 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010372 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010373 int port;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010374 jobopt_T opt;
Bram Moolenaar77073442016-02-13 23:23:53 +010010375 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010376
10377 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +010010378 rettv->v_type = VAR_CHANNEL;
10379 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010380
10381 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010382 if (argvars[1].v_type != VAR_UNKNOWN
10383 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010384 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010385 EMSG(_(e_invarg));
10386 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010387 }
10388
10389 /* parse address */
10390 p = vim_strchr(address, ':');
10391 if (p == NULL)
10392 {
10393 EMSG2(_(e_invarg2), address);
10394 return;
10395 }
10396 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010397 port = strtol((char *)p, &rest, 10);
10398 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010399 {
10400 p[-1] = ':';
10401 EMSG2(_(e_invarg2), address);
10402 return;
10403 }
10404
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010405 /* parse options */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010406 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010407 opt.jo_mode = MODE_JSON;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010408 opt.jo_timeout = 2000;
10409 if (get_job_options(&argvars[1], &opt,
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010410 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010411 return;
10412 if (opt.jo_timeout < 0)
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010413 {
10414 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010415 return;
10416 }
10417
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010418 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
Bram Moolenaar77073442016-02-13 23:23:53 +010010419 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010420 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010421 rettv->vval.v_channel = channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010422 opt.jo_set = JO_ALL;
10423 channel_set_options(channel, &opt);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010424 }
10425}
10426
10427/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010428 * Common for ch_read() and ch_readraw().
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010429 */
10430 static void
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010431common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010432{
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010433 channel_T *channel;
10434 int part;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010435 jobopt_T opt;
10436 int mode;
10437 int timeout;
10438 int id = -1;
10439 typval_T *listtv = NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010440
10441 /* return an empty string by default */
10442 rettv->v_type = VAR_STRING;
10443 rettv->vval.v_string = NULL;
10444
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010445 clear_job_options(&opt);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010446 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
10447 == FAIL)
10448 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010449
Bram Moolenaar77073442016-02-13 23:23:53 +010010450 channel = get_channel_arg(&argvars[0]);
10451 if (channel != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010452 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010453 if (opt.jo_set & JO_PART)
10454 part = opt.jo_part;
10455 else
10456 part = channel_part_read(channel);
10457 mode = channel_get_mode(channel, part);
10458 timeout = channel_get_timeout(channel, part);
10459 if (opt.jo_set & JO_TIMEOUT)
10460 timeout = opt.jo_timeout;
10461
10462 if (raw || mode == MODE_RAW || mode == MODE_NL)
10463 rettv->vval.v_string = channel_read_block(channel, part, timeout);
10464 else
10465 {
10466 if (opt.jo_set & JO_ID)
10467 id = opt.jo_id;
10468 channel_read_json_block(channel, part, timeout, id, &listtv);
10469 if (listtv != NULL)
10470 *rettv = *listtv;
10471 else
10472 {
10473 rettv->v_type = VAR_SPECIAL;
10474 rettv->vval.v_number = VVAL_NONE;
10475 }
10476 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010477 }
Bram Moolenaar77073442016-02-13 23:23:53 +010010478}
10479
10480/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010481 * "ch_read()" function
10482 */
10483 static void
10484f_ch_read(typval_T *argvars, typval_T *rettv)
10485{
10486 common_channel_read(argvars, rettv, FALSE);
10487}
10488
10489/*
10490 * "ch_readraw()" function
10491 */
10492 static void
10493f_ch_readraw(typval_T *argvars, typval_T *rettv)
10494{
10495 common_channel_read(argvars, rettv, TRUE);
10496}
10497
10498/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010499 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010500 * Returns the channel if the caller should read the response.
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010501 * Sets "part_read" to the the read fd.
Bram Moolenaar77073442016-02-13 23:23:53 +010010502 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010503 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010504 static channel_T *
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010505send_common(
10506 typval_T *argvars,
10507 char_u *text,
10508 int id,
10509 int eval,
10510 char *fun,
10511 int *part_read)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010512{
Bram Moolenaar77073442016-02-13 23:23:53 +010010513 channel_T *channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010514 jobopt_T opt;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010515 int part_send;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010516
Bram Moolenaar77073442016-02-13 23:23:53 +010010517 channel = get_channel_arg(&argvars[0]);
10518 if (channel == NULL)
10519 return NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010520 part_send = channel_part_send(channel);
10521 *part_read = channel_part_read(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010522
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010523 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010524 if (get_job_options(&argvars[2], &opt, JO_CALLBACK) == FAIL)
10525 return NULL;
10526
Bram Moolenaara07fec92016-02-05 21:04:08 +010010527 /* Set the callback. An empty callback means no callback and not reading
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010528 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
10529 * allowed. */
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010530 if (opt.jo_callback != NULL && *opt.jo_callback != NUL)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010531 {
10532 if (eval)
10533 {
10534 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
10535 return NULL;
10536 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010537 channel_set_req_callback(channel, part_send, opt.jo_callback, id);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010538 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010539
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010540 if (channel_send(channel, part_send, text, fun) == OK
10541 && opt.jo_callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010542 return channel;
10543 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010544}
10545
10546/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010547 * common for "ch_evalexpr()" and "ch_sendexpr()"
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010548 */
10549 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010550ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010551{
10552 char_u *text;
10553 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010554 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010555 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010556 ch_mode_T ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010557 int part_send;
10558 int part_read;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010559 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010560
10561 /* return an empty string by default */
10562 rettv->v_type = VAR_STRING;
10563 rettv->vval.v_string = NULL;
10564
Bram Moolenaar77073442016-02-13 23:23:53 +010010565 channel = get_channel_arg(&argvars[0]);
10566 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010567 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010568 part_send = channel_part_send(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010569
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010570 ch_mode = channel_get_mode(channel, part_send);
10571 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010572 {
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010573 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010574 return;
10575 }
10576
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010577 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010578 text = json_encode_nr_expr(id, &argvars[1],
10579 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010580 if (text == NULL)
10581 return;
10582
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010583 channel = send_common(argvars, text, id, eval,
10584 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010585 vim_free(text);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010586 if (channel != NULL && eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010587 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010588 /* TODO: timeout from options */
10589 timeout = channel_get_timeout(channel, part_read);
10590 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
10591 == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010592 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010593 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010594
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010595 /* Move the item from the list and then change the type to
10596 * avoid the value being freed. */
10597 *rettv = list->lv_last->li_tv;
10598 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010599 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010600 }
10601 }
10602}
10603
10604/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010605 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010606 */
10607 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010608f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10609{
10610 ch_expr_common(argvars, rettv, TRUE);
10611}
10612
10613/*
10614 * "ch_sendexpr()" function
10615 */
10616 static void
10617f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10618{
10619 ch_expr_common(argvars, rettv, FALSE);
10620}
10621
10622/*
10623 * common for "ch_evalraw()" and "ch_sendraw()"
10624 */
10625 static void
10626ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010627{
10628 char_u buf[NUMBUFLEN];
10629 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010630 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010631 int part_read;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010632 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010633
10634 /* return an empty string by default */
10635 rettv->v_type = VAR_STRING;
10636 rettv->vval.v_string = NULL;
10637
10638 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010639 channel = send_common(argvars, text, 0, eval,
10640 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
10641 if (channel != NULL && eval)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010642 {
10643 /* TODO: timeout from options */
10644 timeout = channel_get_timeout(channel, part_read);
10645 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
10646 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010647}
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010648
10649/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010650 * "ch_evalraw()" function
10651 */
10652 static void
10653f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10654{
10655 ch_raw_common(argvars, rettv, TRUE);
10656}
10657
10658/*
10659 * "ch_sendraw()" function
10660 */
10661 static void
10662f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10663{
10664 ch_raw_common(argvars, rettv, FALSE);
10665}
10666
10667/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010668 * "ch_setoptions()" function
10669 */
10670 static void
10671f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10672{
10673 channel_T *channel;
10674 jobopt_T opt;
10675
10676 channel = get_channel_arg(&argvars[0]);
10677 if (channel == NULL)
10678 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010679 clear_job_options(&opt);
10680 if (get_job_options(&argvars[1], &opt,
10681 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010682 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010683 channel_set_options(channel, &opt);
10684}
10685
10686/*
10687 * "ch_status()" function
10688 */
10689 static void
10690f_ch_status(typval_T *argvars, typval_T *rettv)
10691{
10692 /* return an empty string by default */
10693 rettv->v_type = VAR_STRING;
10694
10695 if (argvars[0].v_type != VAR_CHANNEL)
10696 {
10697 EMSG2(_(e_invarg2), get_tv_string(&argvars[0]));
10698 rettv->vval.v_string = NULL;
10699 }
10700 else
10701 rettv->vval.v_string = vim_strsave(
10702 (char_u *)channel_status(argvars[0].vval.v_channel));
10703}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010704#endif
10705
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010706/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010707 * "changenr()" function
10708 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010710f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010711{
10712 rettv->vval.v_number = curbuf->b_u_seq_cur;
10713}
10714
10715/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716 * "char2nr(string)" function
10717 */
10718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010719f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720{
10721#ifdef FEAT_MBYTE
10722 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010723 {
10724 int utf8 = 0;
10725
10726 if (argvars[1].v_type != VAR_UNKNOWN)
10727 utf8 = get_tv_number_chk(&argvars[1], NULL);
10728
10729 if (utf8)
10730 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10731 else
10732 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 else
10735#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010736 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737}
10738
10739/*
10740 * "cindent(lnum)" function
10741 */
10742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010743f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744{
10745#ifdef FEAT_CINDENT
10746 pos_T pos;
10747 linenr_T lnum;
10748
10749 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010750 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10752 {
10753 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010754 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755 curwin->w_cursor = pos;
10756 }
10757 else
10758#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010759 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760}
10761
10762/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010763 * "clearmatches()" function
10764 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010766f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010767{
10768#ifdef FEAT_SEARCH_EXTRA
10769 clear_matches(curwin);
10770#endif
10771}
10772
10773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 * "col(string)" function
10775 */
10776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010777f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778{
10779 colnr_T col = 0;
10780 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010781 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010783 fp = var2fpos(&argvars[0], FALSE, &fnum);
10784 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 {
10786 if (fp->col == MAXCOL)
10787 {
10788 /* '> can be MAXCOL, get the length of the line then */
10789 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010790 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791 else
10792 col = MAXCOL;
10793 }
10794 else
10795 {
10796 col = fp->col + 1;
10797#ifdef FEAT_VIRTUALEDIT
10798 /* col(".") when the cursor is on the NUL at the end of the line
10799 * because of "coladd" can be seen as an extra column. */
10800 if (virtual_active() && fp == &curwin->w_cursor)
10801 {
10802 char_u *p = ml_get_cursor();
10803
10804 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10805 curwin->w_virtcol - curwin->w_cursor.coladd))
10806 {
10807# ifdef FEAT_MBYTE
10808 int l;
10809
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010810 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811 col += l;
10812# else
10813 if (*p != NUL && p[1] == NUL)
10814 ++col;
10815# endif
10816 }
10817 }
10818#endif
10819 }
10820 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010821 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822}
10823
Bram Moolenaar572cb562005-08-05 21:35:02 +000010824#if defined(FEAT_INS_EXPAND)
10825/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010826 * "complete()" function
10827 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010829f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010830{
10831 int startcol;
10832
10833 if ((State & INSERT) == 0)
10834 {
10835 EMSG(_("E785: complete() can only be used in Insert mode"));
10836 return;
10837 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010838
10839 /* Check for undo allowed here, because if something was already inserted
10840 * the line was already saved for undo and this check isn't done. */
10841 if (!undo_allowed())
10842 return;
10843
Bram Moolenaarade00832006-03-10 21:46:58 +000010844 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10845 {
10846 EMSG(_(e_invarg));
10847 return;
10848 }
10849
10850 startcol = get_tv_number_chk(&argvars[0], NULL);
10851 if (startcol <= 0)
10852 return;
10853
10854 set_completion(startcol - 1, argvars[1].vval.v_list);
10855}
10856
10857/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010858 * "complete_add()" function
10859 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010861f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010862{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010863 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010864}
10865
10866/*
10867 * "complete_check()" function
10868 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010870f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010871{
10872 int saved = RedrawingDisabled;
10873
10874 RedrawingDisabled = 0;
10875 ins_compl_check_keys(0);
10876 rettv->vval.v_number = compl_interrupted;
10877 RedrawingDisabled = saved;
10878}
10879#endif
10880
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881/*
10882 * "confirm(message, buttons[, default [, type]])" function
10883 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010885f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886{
10887#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10888 char_u *message;
10889 char_u *buttons = NULL;
10890 char_u buf[NUMBUFLEN];
10891 char_u buf2[NUMBUFLEN];
10892 int def = 1;
10893 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010894 char_u *typestr;
10895 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010897 message = get_tv_string_chk(&argvars[0]);
10898 if (message == NULL)
10899 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010900 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010902 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10903 if (buttons == NULL)
10904 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010905 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010907 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010908 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010910 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10911 if (typestr == NULL)
10912 error = TRUE;
10913 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010915 switch (TOUPPER_ASC(*typestr))
10916 {
10917 case 'E': type = VIM_ERROR; break;
10918 case 'Q': type = VIM_QUESTION; break;
10919 case 'I': type = VIM_INFO; break;
10920 case 'W': type = VIM_WARNING; break;
10921 case 'G': type = VIM_GENERIC; break;
10922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923 }
10924 }
10925 }
10926 }
10927
10928 if (buttons == NULL || *buttons == NUL)
10929 buttons = (char_u *)_("&Ok");
10930
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010931 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010932 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010933 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934#endif
10935}
10936
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010937/*
10938 * "copy()" function
10939 */
10940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010941f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010942{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010943 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010944}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010946#ifdef FEAT_FLOAT
10947/*
10948 * "cos()" function
10949 */
10950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010951f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010952{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010953 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010954
10955 rettv->v_type = VAR_FLOAT;
10956 if (get_float_arg(argvars, &f) == OK)
10957 rettv->vval.v_float = cos(f);
10958 else
10959 rettv->vval.v_float = 0.0;
10960}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010961
10962/*
10963 * "cosh()" function
10964 */
10965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010966f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010967{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010968 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010969
10970 rettv->v_type = VAR_FLOAT;
10971 if (get_float_arg(argvars, &f) == OK)
10972 rettv->vval.v_float = cosh(f);
10973 else
10974 rettv->vval.v_float = 0.0;
10975}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010976#endif
10977
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010979 * "count()" function
10980 */
10981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010982f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010983{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010984 long n = 0;
10985 int ic = FALSE;
10986
Bram Moolenaare9a41262005-01-15 22:18:47 +000010987 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010988 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010989 listitem_T *li;
10990 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010991 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010992
Bram Moolenaare9a41262005-01-15 22:18:47 +000010993 if ((l = argvars[0].vval.v_list) != NULL)
10994 {
10995 li = l->lv_first;
10996 if (argvars[2].v_type != VAR_UNKNOWN)
10997 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010998 int error = FALSE;
10999
11000 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011001 if (argvars[3].v_type != VAR_UNKNOWN)
11002 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011003 idx = get_tv_number_chk(&argvars[3], &error);
11004 if (!error)
11005 {
11006 li = list_find(l, idx);
11007 if (li == NULL)
11008 EMSGN(_(e_listidx), idx);
11009 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011010 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011011 if (error)
11012 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011013 }
11014
11015 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011016 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011017 ++n;
11018 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011019 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011020 else if (argvars[0].v_type == VAR_DICT)
11021 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011022 int todo;
11023 dict_T *d;
11024 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011025
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011026 if ((d = argvars[0].vval.v_dict) != NULL)
11027 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011028 int error = FALSE;
11029
Bram Moolenaare9a41262005-01-15 22:18:47 +000011030 if (argvars[2].v_type != VAR_UNKNOWN)
11031 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011032 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011033 if (argvars[3].v_type != VAR_UNKNOWN)
11034 EMSG(_(e_invarg));
11035 }
11036
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011037 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011038 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011039 {
11040 if (!HASHITEM_EMPTY(hi))
11041 {
11042 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011043 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011044 ++n;
11045 }
11046 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011047 }
11048 }
11049 else
11050 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011051 rettv->vval.v_number = n;
11052}
11053
11054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
11056 *
11057 * Checks the existence of a cscope connection.
11058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011060f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061{
11062#ifdef FEAT_CSCOPE
11063 int num = 0;
11064 char_u *dbpath = NULL;
11065 char_u *prepend = NULL;
11066 char_u buf[NUMBUFLEN];
11067
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011068 if (argvars[0].v_type != VAR_UNKNOWN
11069 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011071 num = (int)get_tv_number(&argvars[0]);
11072 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011073 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011074 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 }
11076
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011077 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078#endif
11079}
11080
11081/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011082 * "cursor(lnum, col)" function, or
11083 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011085 * Moves the cursor to the specified line and column.
11086 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011089f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090{
11091 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011092#ifdef FEAT_VIRTUALEDIT
11093 long coladd = 0;
11094#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011095 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011097 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011098 if (argvars[1].v_type == VAR_UNKNOWN)
11099 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011100 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011101 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011102
Bram Moolenaar493c1782014-05-28 14:34:46 +020011103 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011104 {
11105 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011106 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011107 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011108 line = pos.lnum;
11109 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011110#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011111 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011112#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011113 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011114 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011115 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011116 set_curswant = FALSE;
11117 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011118 }
11119 else
11120 {
11121 line = get_tv_lnum(argvars);
11122 col = get_tv_number_chk(&argvars[1], NULL);
11123#ifdef FEAT_VIRTUALEDIT
11124 if (argvars[2].v_type != VAR_UNKNOWN)
11125 coladd = get_tv_number_chk(&argvars[2], NULL);
11126#endif
11127 }
11128 if (line < 0 || col < 0
11129#ifdef FEAT_VIRTUALEDIT
11130 || coladd < 0
11131#endif
11132 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011133 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134 if (line > 0)
11135 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136 if (col > 0)
11137 curwin->w_cursor.col = col - 1;
11138#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011139 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140#endif
11141
11142 /* Make sure the cursor is in a valid position. */
11143 check_cursor();
11144#ifdef FEAT_MBYTE
11145 /* Correct cursor for multi-byte character. */
11146 if (has_mbyte)
11147 mb_adjust_cursor();
11148#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011149
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011150 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011151 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152}
11153
11154/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011155 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156 */
11157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011158f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011160 int noref = 0;
11161
11162 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011163 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011164 if (noref < 0 || noref > 1)
11165 EMSG(_(e_invarg));
11166 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011167 {
11168 current_copyID += COPYID_INC;
11169 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171}
11172
11173/*
11174 * "delete()" function
11175 */
11176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011177f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011179 char_u nbuf[NUMBUFLEN];
11180 char_u *name;
11181 char_u *flags;
11182
11183 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011185 return;
11186
11187 name = get_tv_string(&argvars[0]);
11188 if (name == NULL || *name == NUL)
11189 {
11190 EMSG(_(e_invarg));
11191 return;
11192 }
11193
11194 if (argvars[1].v_type != VAR_UNKNOWN)
11195 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011197 flags = (char_u *)"";
11198
11199 if (*flags == NUL)
11200 /* delete a file */
11201 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11202 else if (STRCMP(flags, "d") == 0)
11203 /* delete an empty directory */
11204 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11205 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011206 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011207 rettv->vval.v_number = delete_recursive(name);
11208 else
11209 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210}
11211
11212/*
11213 * "did_filetype()" function
11214 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011216f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217{
11218#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011219 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220#endif
11221}
11222
11223/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011224 * "diff_filler()" function
11225 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011227f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011228{
11229#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011230 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011231#endif
11232}
11233
11234/*
11235 * "diff_hlID()" function
11236 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011238f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011239{
11240#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011241 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011242 static linenr_T prev_lnum = 0;
11243 static int changedtick = 0;
11244 static int fnum = 0;
11245 static int change_start = 0;
11246 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011247 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011248 int filler_lines;
11249 int col;
11250
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011251 if (lnum < 0) /* ignore type error in {lnum} arg */
11252 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011253 if (lnum != prev_lnum
11254 || changedtick != curbuf->b_changedtick
11255 || fnum != curbuf->b_fnum)
11256 {
11257 /* New line, buffer, change: need to get the values. */
11258 filler_lines = diff_check(curwin, lnum);
11259 if (filler_lines < 0)
11260 {
11261 if (filler_lines == -1)
11262 {
11263 change_start = MAXCOL;
11264 change_end = -1;
11265 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11266 hlID = HLF_ADD; /* added line */
11267 else
11268 hlID = HLF_CHD; /* changed line */
11269 }
11270 else
11271 hlID = HLF_ADD; /* added line */
11272 }
11273 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011274 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011275 prev_lnum = lnum;
11276 changedtick = curbuf->b_changedtick;
11277 fnum = curbuf->b_fnum;
11278 }
11279
11280 if (hlID == HLF_CHD || hlID == HLF_TXD)
11281 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011282 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011283 if (col >= change_start && col <= change_end)
11284 hlID = HLF_TXD; /* changed text */
11285 else
11286 hlID = HLF_CHD; /* changed line */
11287 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011288 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011289#endif
11290}
11291
11292/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011293 * "disable_char_avail_for_testing({expr})" function
11294 */
11295 static void
11296f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11297{
11298 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11299}
11300
11301/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011302 * "empty({expr})" function
11303 */
11304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011305f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011306{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011307 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011308
11309 switch (argvars[0].v_type)
11310 {
11311 case VAR_STRING:
11312 case VAR_FUNC:
11313 n = argvars[0].vval.v_string == NULL
11314 || *argvars[0].vval.v_string == NUL;
11315 break;
11316 case VAR_NUMBER:
11317 n = argvars[0].vval.v_number == 0;
11318 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011319 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011320#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011321 n = argvars[0].vval.v_float == 0.0;
11322 break;
11323#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011324 case VAR_LIST:
11325 n = argvars[0].vval.v_list == NULL
11326 || argvars[0].vval.v_list->lv_first == NULL;
11327 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011328 case VAR_DICT:
11329 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011330 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011331 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011332 case VAR_SPECIAL:
11333 n = argvars[0].vval.v_number != VVAL_TRUE;
11334 break;
11335
Bram Moolenaar835dc632016-02-07 14:27:38 +010011336 case VAR_JOB:
11337#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010011338 n = argvars[0].vval.v_job == NULL
11339 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11340 break;
11341#endif
11342 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010011343#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011344 n = argvars[0].vval.v_channel == NULL
11345 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011346 break;
11347#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011348 case VAR_UNKNOWN:
11349 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11350 n = TRUE;
11351 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011352 }
11353
11354 rettv->vval.v_number = n;
11355}
11356
11357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358 * "escape({string}, {chars})" function
11359 */
11360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011361f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362{
11363 char_u buf[NUMBUFLEN];
11364
Bram Moolenaar758711c2005-02-02 23:11:38 +000011365 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11366 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011367 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368}
11369
11370/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011371 * "eval()" function
11372 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011374f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011375{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011376 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011377
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011378 s = get_tv_string_chk(&argvars[0]);
11379 if (s != NULL)
11380 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011381
Bram Moolenaar615b9972015-01-14 17:15:05 +010011382 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011383 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11384 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011385 if (p != NULL && !aborting())
11386 EMSG2(_(e_invexpr2), p);
11387 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011388 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011389 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011390 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011391 else if (*s != NUL)
11392 EMSG(_(e_trailing));
11393}
11394
11395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396 * "eventhandler()" function
11397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011399f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011401 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402}
11403
11404/*
11405 * "executable()" function
11406 */
11407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011408f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011409{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011410 char_u *name = get_tv_string(&argvars[0]);
11411
11412 /* Check in $PATH and also check directly if there is a directory name. */
11413 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11414 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011415}
11416
11417/*
11418 * "exepath()" function
11419 */
11420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011421f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011422{
11423 char_u *p = NULL;
11424
Bram Moolenaarb5971142015-03-21 17:32:19 +010011425 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011426 rettv->v_type = VAR_STRING;
11427 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428}
11429
11430/*
11431 * "exists()" function
11432 */
11433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011434f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435{
11436 char_u *p;
11437 char_u *name;
11438 int n = FALSE;
11439 int len = 0;
11440
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011441 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442 if (*p == '$') /* environment variable */
11443 {
11444 /* first try "normal" environment variables (fast) */
11445 if (mch_getenv(p + 1) != NULL)
11446 n = TRUE;
11447 else
11448 {
11449 /* try expanding things like $VIM and ${HOME} */
11450 p = expand_env_save(p);
11451 if (p != NULL && *p != '$')
11452 n = TRUE;
11453 vim_free(p);
11454 }
11455 }
11456 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011457 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011458 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011459 if (*skipwhite(p) != NUL)
11460 n = FALSE; /* trailing garbage */
11461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462 else if (*p == '*') /* internal or user defined function */
11463 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011464 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465 }
11466 else if (*p == ':')
11467 {
11468 n = cmd_exists(p + 1);
11469 }
11470 else if (*p == '#')
11471 {
11472#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011473 if (p[1] == '#')
11474 n = autocmd_supported(p + 2);
11475 else
11476 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477#endif
11478 }
11479 else /* internal variable */
11480 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011481 char_u *tofree;
11482 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011484 /* get_name_len() takes care of expanding curly braces */
11485 name = p;
11486 len = get_name_len(&p, &tofree, TRUE, FALSE);
11487 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011489 if (tofree != NULL)
11490 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011491 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011492 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011494 /* handle d.key, l[idx], f(expr) */
11495 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11496 if (n)
11497 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011498 }
11499 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011500 if (*p != NUL)
11501 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011503 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011504 }
11505
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011506 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011507}
11508
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011509#ifdef FEAT_FLOAT
11510/*
11511 * "exp()" function
11512 */
11513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011514f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011515{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011516 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011517
11518 rettv->v_type = VAR_FLOAT;
11519 if (get_float_arg(argvars, &f) == OK)
11520 rettv->vval.v_float = exp(f);
11521 else
11522 rettv->vval.v_float = 0.0;
11523}
11524#endif
11525
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526/*
11527 * "expand()" function
11528 */
11529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011530f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011531{
11532 char_u *s;
11533 int len;
11534 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011535 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011537 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011538 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011540 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011541 if (argvars[1].v_type != VAR_UNKNOWN
11542 && argvars[2].v_type != VAR_UNKNOWN
11543 && get_tv_number_chk(&argvars[2], &error)
11544 && !error)
11545 {
11546 rettv->v_type = VAR_LIST;
11547 rettv->vval.v_list = NULL;
11548 }
11549
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011550 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551 if (*s == '%' || *s == '#' || *s == '<')
11552 {
11553 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011554 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011556 if (rettv->v_type == VAR_LIST)
11557 {
11558 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11559 list_append_string(rettv->vval.v_list, result, -1);
11560 else
11561 vim_free(result);
11562 }
11563 else
11564 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565 }
11566 else
11567 {
11568 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011569 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011570 if (argvars[1].v_type != VAR_UNKNOWN
11571 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011572 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011573 if (!error)
11574 {
11575 ExpandInit(&xpc);
11576 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011577 if (p_wic)
11578 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011579 if (rettv->v_type == VAR_STRING)
11580 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11581 options, WILD_ALL);
11582 else if (rettv_list_alloc(rettv) != FAIL)
11583 {
11584 int i;
11585
11586 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11587 for (i = 0; i < xpc.xp_numfiles; i++)
11588 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11589 ExpandCleanup(&xpc);
11590 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011591 }
11592 else
11593 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594 }
11595}
11596
11597/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011598 * Go over all entries in "d2" and add them to "d1".
11599 * When "action" is "error" then a duplicate key is an error.
11600 * When "action" is "force" then a duplicate key is overwritten.
11601 * Otherwise duplicate keys are ignored ("action" is "keep").
11602 */
11603 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011604dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011605{
11606 dictitem_T *di1;
11607 hashitem_T *hi2;
11608 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011609 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011610
11611 todo = (int)d2->dv_hashtab.ht_used;
11612 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11613 {
11614 if (!HASHITEM_EMPTY(hi2))
11615 {
11616 --todo;
11617 di1 = dict_find(d1, hi2->hi_key, -1);
11618 if (d1->dv_scope != 0)
11619 {
11620 /* Disallow replacing a builtin function in l: and g:.
11621 * Check the key to be valid when adding to any
11622 * scope. */
11623 if (d1->dv_scope == VAR_DEF_SCOPE
11624 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11625 && var_check_func_name(hi2->hi_key,
11626 di1 == NULL))
11627 break;
11628 if (!valid_varname(hi2->hi_key))
11629 break;
11630 }
11631 if (di1 == NULL)
11632 {
11633 di1 = dictitem_copy(HI2DI(hi2));
11634 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11635 dictitem_free(di1);
11636 }
11637 else if (*action == 'e')
11638 {
11639 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11640 break;
11641 }
11642 else if (*action == 'f' && HI2DI(hi2) != di1)
11643 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011644 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11645 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011646 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011647 clear_tv(&di1->di_tv);
11648 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11649 }
11650 }
11651 }
11652}
11653
11654/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011655 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011656 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011657 */
11658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011659f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011660{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011661 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011662
Bram Moolenaare9a41262005-01-15 22:18:47 +000011663 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011664 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011665 list_T *l1, *l2;
11666 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011667 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011668 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011669
Bram Moolenaare9a41262005-01-15 22:18:47 +000011670 l1 = argvars[0].vval.v_list;
11671 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011672 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011673 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011674 {
11675 if (argvars[2].v_type != VAR_UNKNOWN)
11676 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011677 before = get_tv_number_chk(&argvars[2], &error);
11678 if (error)
11679 return; /* type error; errmsg already given */
11680
Bram Moolenaar758711c2005-02-02 23:11:38 +000011681 if (before == l1->lv_len)
11682 item = NULL;
11683 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011684 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011685 item = list_find(l1, before);
11686 if (item == NULL)
11687 {
11688 EMSGN(_(e_listidx), before);
11689 return;
11690 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011691 }
11692 }
11693 else
11694 item = NULL;
11695 list_extend(l1, l2, item);
11696
Bram Moolenaare9a41262005-01-15 22:18:47 +000011697 copy_tv(&argvars[0], rettv);
11698 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011699 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011700 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11701 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011702 dict_T *d1, *d2;
11703 char_u *action;
11704 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011705
11706 d1 = argvars[0].vval.v_dict;
11707 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011708 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011709 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011710 {
11711 /* Check the third argument. */
11712 if (argvars[2].v_type != VAR_UNKNOWN)
11713 {
11714 static char *(av[]) = {"keep", "force", "error"};
11715
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011716 action = get_tv_string_chk(&argvars[2]);
11717 if (action == NULL)
11718 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011719 for (i = 0; i < 3; ++i)
11720 if (STRCMP(action, av[i]) == 0)
11721 break;
11722 if (i == 3)
11723 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011724 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011725 return;
11726 }
11727 }
11728 else
11729 action = (char_u *)"force";
11730
Bram Moolenaara9922d62013-05-30 13:01:18 +020011731 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011732
Bram Moolenaare9a41262005-01-15 22:18:47 +000011733 copy_tv(&argvars[0], rettv);
11734 }
11735 }
11736 else
11737 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011738}
11739
11740/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011741 * "feedkeys()" function
11742 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011744f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011745{
11746 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011747 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011748 char_u *keys, *flags;
11749 char_u nbuf[NUMBUFLEN];
11750 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011751 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011752 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011753
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011754 /* This is not allowed in the sandbox. If the commands would still be
11755 * executed in the sandbox it would be OK, but it probably happens later,
11756 * when "sandbox" is no longer set. */
11757 if (check_secure())
11758 return;
11759
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011760 keys = get_tv_string(&argvars[0]);
11761 if (*keys != NUL)
11762 {
11763 if (argvars[1].v_type != VAR_UNKNOWN)
11764 {
11765 flags = get_tv_string_buf(&argvars[1], nbuf);
11766 for ( ; *flags != NUL; ++flags)
11767 {
11768 switch (*flags)
11769 {
11770 case 'n': remap = FALSE; break;
11771 case 'm': remap = TRUE; break;
11772 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011773 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011774 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011775 }
11776 }
11777 }
11778
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011779 /* Need to escape K_SPECIAL and CSI before putting the string in the
11780 * typeahead buffer. */
11781 keys_esc = vim_strsave_escape_csi(keys);
11782 if (keys_esc != NULL)
11783 {
11784 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011785 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011786 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011787 if (vgetc_busy)
11788 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011789 if (execute)
11790 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011791 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011792 }
11793}
11794
11795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796 * "filereadable()" function
11797 */
11798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011799f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011801 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802 char_u *p;
11803 int n;
11804
Bram Moolenaarc236c162008-07-13 17:41:49 +000011805#ifndef O_NONBLOCK
11806# define O_NONBLOCK 0
11807#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011808 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011809 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11810 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811 {
11812 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011813 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011814 }
11815 else
11816 n = FALSE;
11817
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011818 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819}
11820
11821/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011822 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823 * rights to write into.
11824 */
11825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011826f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011828 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829}
11830
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011831 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011832findfilendir(
11833 typval_T *argvars UNUSED,
11834 typval_T *rettv,
11835 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011836{
11837#ifdef FEAT_SEARCHPATH
11838 char_u *fname;
11839 char_u *fresult = NULL;
11840 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11841 char_u *p;
11842 char_u pathbuf[NUMBUFLEN];
11843 int count = 1;
11844 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011845 int error = FALSE;
11846#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011847
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011848 rettv->vval.v_string = NULL;
11849 rettv->v_type = VAR_STRING;
11850
11851#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011852 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011853
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011854 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011855 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011856 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11857 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011858 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011859 else
11860 {
11861 if (*p != NUL)
11862 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011863
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011864 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011865 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011866 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011867 }
11868
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011869 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11870 error = TRUE;
11871
11872 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011874 do
11875 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011876 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011877 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011878 fresult = find_file_in_path_option(first ? fname : NULL,
11879 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011880 0, first, path,
11881 find_what,
11882 curbuf->b_ffname,
11883 find_what == FINDFILE_DIR
11884 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011885 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011886
11887 if (fresult != NULL && rettv->v_type == VAR_LIST)
11888 list_append_string(rettv->vval.v_list, fresult, -1);
11889
11890 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011891 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011892
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011893 if (rettv->v_type == VAR_STRING)
11894 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011895#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011896}
11897
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011898static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11899static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011900
11901/*
11902 * Implementation of map() and filter().
11903 */
11904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011905filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011906{
11907 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011908 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011909 listitem_T *li, *nli;
11910 list_T *l = NULL;
11911 dictitem_T *di;
11912 hashtab_T *ht;
11913 hashitem_T *hi;
11914 dict_T *d = NULL;
11915 typval_T save_val;
11916 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011917 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011918 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011919 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011920 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011921 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011922 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011923 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011924
Bram Moolenaare9a41262005-01-15 22:18:47 +000011925 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011926 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011927 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011928 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011929 return;
11930 }
11931 else if (argvars[0].v_type == VAR_DICT)
11932 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011933 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011934 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011935 return;
11936 }
11937 else
11938 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011939 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011940 return;
11941 }
11942
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011943 expr = get_tv_string_buf_chk(&argvars[1], buf);
11944 /* On type errors, the preceding call has already displayed an error
11945 * message. Avoid a misleading error message for an empty string that
11946 * was not passed as argument. */
11947 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011948 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011949 prepare_vimvar(VV_VAL, &save_val);
11950 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011951
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011952 /* We reset "did_emsg" to be able to detect whether an error
11953 * occurred during evaluation of the expression. */
11954 save_did_emsg = did_emsg;
11955 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011956
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011957 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011958 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011959 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011960 vimvars[VV_KEY].vv_type = VAR_STRING;
11961
11962 ht = &d->dv_hashtab;
11963 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011964 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011965 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011966 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011967 if (!HASHITEM_EMPTY(hi))
11968 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011969 int r;
11970
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011971 --todo;
11972 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011973 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011974 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11975 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011976 break;
11977 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011978 r = filter_map_one(&di->di_tv, expr, map, &rem);
11979 clear_tv(&vimvars[VV_KEY].vv_tv);
11980 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011981 break;
11982 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011983 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011984 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11985 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011986 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011987 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011988 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011989 }
11990 }
11991 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011992 }
11993 else
11994 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011995 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11996
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011997 for (li = l->lv_first; li != NULL; li = nli)
11998 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011999 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012000 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012001 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012002 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012003 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012004 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012005 break;
12006 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012007 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012008 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012009 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012010 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012011
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012012 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012013 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012014
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012015 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012016 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012017
12018 copy_tv(&argvars[0], rettv);
12019}
12020
12021 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010012022filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012023{
Bram Moolenaar33570922005-01-25 22:26:29 +000012024 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012025 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012026 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012027
Bram Moolenaar33570922005-01-25 22:26:29 +000012028 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012029 s = expr;
12030 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012031 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012032 if (*s != NUL) /* check for trailing chars after expr */
12033 {
12034 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012035 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012036 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012037 }
12038 if (map)
12039 {
12040 /* map(): replace the list item value */
12041 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000012042 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012043 *tv = rettv;
12044 }
12045 else
12046 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012047 int error = FALSE;
12048
Bram Moolenaare9a41262005-01-15 22:18:47 +000012049 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012050 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012051 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012052 /* On type error, nothing has been removed; return FAIL to stop the
12053 * loop. The error message was given by get_tv_number_chk(). */
12054 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012055 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012056 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012057 retval = OK;
12058theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000012059 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012060 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012061}
12062
12063/*
12064 * "filter()" function
12065 */
12066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012067f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012068{
12069 filter_map(argvars, rettv, FALSE);
12070}
12071
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012072/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012073 * "finddir({fname}[, {path}[, {count}]])" function
12074 */
12075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012076f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012077{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012078 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012079}
12080
12081/*
12082 * "findfile({fname}[, {path}[, {count}]])" function
12083 */
12084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012085f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012086{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012087 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012088}
12089
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012090#ifdef FEAT_FLOAT
12091/*
12092 * "float2nr({float})" function
12093 */
12094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012095f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012096{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012097 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012098
12099 if (get_float_arg(argvars, &f) == OK)
12100 {
12101 if (f < -0x7fffffff)
12102 rettv->vval.v_number = -0x7fffffff;
12103 else if (f > 0x7fffffff)
12104 rettv->vval.v_number = 0x7fffffff;
12105 else
12106 rettv->vval.v_number = (varnumber_T)f;
12107 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012108}
12109
12110/*
12111 * "floor({float})" function
12112 */
12113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012114f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012115{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012116 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012117
12118 rettv->v_type = VAR_FLOAT;
12119 if (get_float_arg(argvars, &f) == OK)
12120 rettv->vval.v_float = floor(f);
12121 else
12122 rettv->vval.v_float = 0.0;
12123}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012124
12125/*
12126 * "fmod()" function
12127 */
12128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012129f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012130{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012131 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012132
12133 rettv->v_type = VAR_FLOAT;
12134 if (get_float_arg(argvars, &fx) == OK
12135 && get_float_arg(&argvars[1], &fy) == OK)
12136 rettv->vval.v_float = fmod(fx, fy);
12137 else
12138 rettv->vval.v_float = 0.0;
12139}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012140#endif
12141
Bram Moolenaar0d660222005-01-07 21:51:51 +000012142/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012143 * "fnameescape({string})" function
12144 */
12145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012146f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012147{
12148 rettv->vval.v_string = vim_strsave_fnameescape(
12149 get_tv_string(&argvars[0]), FALSE);
12150 rettv->v_type = VAR_STRING;
12151}
12152
12153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154 * "fnamemodify({fname}, {mods})" function
12155 */
12156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012157f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158{
12159 char_u *fname;
12160 char_u *mods;
12161 int usedlen = 0;
12162 int len;
12163 char_u *fbuf = NULL;
12164 char_u buf[NUMBUFLEN];
12165
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012166 fname = get_tv_string_chk(&argvars[0]);
12167 mods = get_tv_string_buf_chk(&argvars[1], buf);
12168 if (fname == NULL || mods == NULL)
12169 fname = NULL;
12170 else
12171 {
12172 len = (int)STRLEN(fname);
12173 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012176 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012178 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012180 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181 vim_free(fbuf);
12182}
12183
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012184static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185
12186/*
12187 * "foldclosed()" function
12188 */
12189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012190foldclosed_both(
12191 typval_T *argvars UNUSED,
12192 typval_T *rettv,
12193 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194{
12195#ifdef FEAT_FOLDING
12196 linenr_T lnum;
12197 linenr_T first, last;
12198
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012199 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12201 {
12202 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12203 {
12204 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012205 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012207 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208 return;
12209 }
12210 }
12211#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012212 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012213}
12214
12215/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012216 * "foldclosed()" function
12217 */
12218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012219f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012220{
12221 foldclosed_both(argvars, rettv, FALSE);
12222}
12223
12224/*
12225 * "foldclosedend()" function
12226 */
12227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012228f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012229{
12230 foldclosed_both(argvars, rettv, TRUE);
12231}
12232
12233/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234 * "foldlevel()" function
12235 */
12236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012237f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238{
12239#ifdef FEAT_FOLDING
12240 linenr_T lnum;
12241
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012242 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012243 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012244 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246}
12247
12248/*
12249 * "foldtext()" function
12250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012252f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253{
12254#ifdef FEAT_FOLDING
12255 linenr_T lnum;
12256 char_u *s;
12257 char_u *r;
12258 int len;
12259 char *txt;
12260#endif
12261
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012262 rettv->v_type = VAR_STRING;
12263 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012265 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12266 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12267 <= curbuf->b_ml.ml_line_count
12268 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269 {
12270 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012271 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12272 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012273 {
12274 if (!linewhite(lnum))
12275 break;
12276 ++lnum;
12277 }
12278
12279 /* Find interesting text in this line. */
12280 s = skipwhite(ml_get(lnum));
12281 /* skip C comment-start */
12282 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012283 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012285 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012286 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012287 {
12288 s = skipwhite(ml_get(lnum + 1));
12289 if (*s == '*')
12290 s = skipwhite(s + 1);
12291 }
12292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293 txt = _("+-%s%3ld lines: ");
12294 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012295 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296 + 20 /* for %3ld */
12297 + STRLEN(s))); /* concatenated */
12298 if (r != NULL)
12299 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012300 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12301 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12302 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303 len = (int)STRLEN(r);
12304 STRCAT(r, s);
12305 /* remove 'foldmarker' and 'commentstring' */
12306 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012307 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012308 }
12309 }
12310#endif
12311}
12312
12313/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012314 * "foldtextresult(lnum)" function
12315 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012317f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012318{
12319#ifdef FEAT_FOLDING
12320 linenr_T lnum;
12321 char_u *text;
12322 char_u buf[51];
12323 foldinfo_T foldinfo;
12324 int fold_count;
12325#endif
12326
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012327 rettv->v_type = VAR_STRING;
12328 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012329#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012330 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012331 /* treat illegal types and illegal string values for {lnum} the same */
12332 if (lnum < 0)
12333 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012334 fold_count = foldedCount(curwin, lnum, &foldinfo);
12335 if (fold_count > 0)
12336 {
12337 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12338 &foldinfo, buf);
12339 if (text == buf)
12340 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012341 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012342 }
12343#endif
12344}
12345
12346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347 * "foreground()" function
12348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012350f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352#ifdef FEAT_GUI
12353 if (gui.in_use)
12354 gui_mch_set_foreground();
12355#else
12356# ifdef WIN32
12357 win32_set_foreground();
12358# endif
12359#endif
12360}
12361
12362/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012363 * "function()" function
12364 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012366f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012367{
12368 char_u *s;
12369
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012370 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012371 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012372 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012373 /* Don't check an autoload name for existence here. */
12374 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012375 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012376 else
12377 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012378 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012379 {
12380 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012381 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012382
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012383 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12384 * also be called from another script. Using trans_function_name()
12385 * would also work, but some plugins depend on the name being
12386 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012387 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020012388 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012389 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012390 if (rettv->vval.v_string != NULL)
12391 {
12392 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012393 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012394 }
12395 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012396 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012397 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012398 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012399 }
12400}
12401
12402/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012403 * "garbagecollect()" function
12404 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012406f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012407{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012408 /* This is postponed until we are back at the toplevel, because we may be
12409 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12410 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012411
12412 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12413 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012414}
12415
12416/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012417 * "get()" function
12418 */
12419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012420f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012421{
Bram Moolenaar33570922005-01-25 22:26:29 +000012422 listitem_T *li;
12423 list_T *l;
12424 dictitem_T *di;
12425 dict_T *d;
12426 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012427
Bram Moolenaare9a41262005-01-15 22:18:47 +000012428 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012429 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012430 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012431 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012432 int error = FALSE;
12433
12434 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12435 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012436 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012437 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012438 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012439 else if (argvars[0].v_type == VAR_DICT)
12440 {
12441 if ((d = argvars[0].vval.v_dict) != NULL)
12442 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012443 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012444 if (di != NULL)
12445 tv = &di->di_tv;
12446 }
12447 }
12448 else
12449 EMSG2(_(e_listdictarg), "get()");
12450
12451 if (tv == NULL)
12452 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012453 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012454 copy_tv(&argvars[2], rettv);
12455 }
12456 else
12457 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012458}
12459
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012460static 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 +000012461
12462/*
12463 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012464 * Return a range (from start to end) of lines in rettv from the specified
12465 * buffer.
12466 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012467 */
12468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012469get_buffer_lines(
12470 buf_T *buf,
12471 linenr_T start,
12472 linenr_T end,
12473 int retlist,
12474 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012475{
12476 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012477
Bram Moolenaar959a1432013-12-14 12:17:38 +010012478 rettv->v_type = VAR_STRING;
12479 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012480 if (retlist && rettv_list_alloc(rettv) == FAIL)
12481 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012482
12483 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12484 return;
12485
12486 if (!retlist)
12487 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012488 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12489 p = ml_get_buf(buf, start, FALSE);
12490 else
12491 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012492 rettv->vval.v_string = vim_strsave(p);
12493 }
12494 else
12495 {
12496 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012497 return;
12498
12499 if (start < 1)
12500 start = 1;
12501 if (end > buf->b_ml.ml_line_count)
12502 end = buf->b_ml.ml_line_count;
12503 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012504 if (list_append_string(rettv->vval.v_list,
12505 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012506 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012507 }
12508}
12509
12510/*
12511 * "getbufline()" function
12512 */
12513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012514f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012515{
12516 linenr_T lnum;
12517 linenr_T end;
12518 buf_T *buf;
12519
12520 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12521 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012522 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012523 --emsg_off;
12524
Bram Moolenaar661b1822005-07-28 22:36:45 +000012525 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012526 if (argvars[2].v_type == VAR_UNKNOWN)
12527 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012528 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012529 end = get_tv_lnum_buf(&argvars[2], buf);
12530
Bram Moolenaar342337a2005-07-21 21:11:17 +000012531 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012532}
12533
Bram Moolenaar0d660222005-01-07 21:51:51 +000012534/*
12535 * "getbufvar()" function
12536 */
12537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012538f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012539{
12540 buf_T *buf;
12541 buf_T *save_curbuf;
12542 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012543 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012544 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012545
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012546 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12547 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012548 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012549 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012550
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012551 rettv->v_type = VAR_STRING;
12552 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012553
12554 if (buf != NULL && varname != NULL)
12555 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012556 /* set curbuf to be our buf, temporarily */
12557 save_curbuf = curbuf;
12558 curbuf = buf;
12559
Bram Moolenaar0d660222005-01-07 21:51:51 +000012560 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012561 {
12562 if (get_option_tv(&varname, rettv, TRUE) == OK)
12563 done = TRUE;
12564 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012565 else if (STRCMP(varname, "changedtick") == 0)
12566 {
12567 rettv->v_type = VAR_NUMBER;
12568 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012569 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012570 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012571 else
12572 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012573 /* Look up the variable. */
12574 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12575 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12576 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012577 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012578 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012579 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012580 done = TRUE;
12581 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012582 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012583
12584 /* restore previous notion of curbuf */
12585 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012586 }
12587
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012588 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12589 /* use the default value */
12590 copy_tv(&argvars[2], rettv);
12591
Bram Moolenaar0d660222005-01-07 21:51:51 +000012592 --emsg_off;
12593}
12594
12595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596 * "getchar()" function
12597 */
12598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012599f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600{
12601 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012602 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012603
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012604 /* Position the cursor. Needed after a message that ends in a space. */
12605 windgoto(msg_row, msg_col);
12606
Bram Moolenaar071d4272004-06-13 20:20:40 +000012607 ++no_mapping;
12608 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012609 for (;;)
12610 {
12611 if (argvars[0].v_type == VAR_UNKNOWN)
12612 /* getchar(): blocking wait. */
12613 n = safe_vgetc();
12614 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12615 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012616 n = vpeekc_any();
12617 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012618 /* illegal argument or getchar(0) and no char avail: return zero */
12619 n = 0;
12620 else
12621 /* getchar(0) and char avail: return char */
12622 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012623
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012624 if (n == K_IGNORE)
12625 continue;
12626 break;
12627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012628 --no_mapping;
12629 --allow_keys;
12630
Bram Moolenaar219b8702006-11-01 14:32:36 +000012631 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12632 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12633 vimvars[VV_MOUSE_COL].vv_nr = 0;
12634
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012635 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012636 if (IS_SPECIAL(n) || mod_mask != 0)
12637 {
12638 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12639 int i = 0;
12640
12641 /* Turn a special key into three bytes, plus modifier. */
12642 if (mod_mask != 0)
12643 {
12644 temp[i++] = K_SPECIAL;
12645 temp[i++] = KS_MODIFIER;
12646 temp[i++] = mod_mask;
12647 }
12648 if (IS_SPECIAL(n))
12649 {
12650 temp[i++] = K_SPECIAL;
12651 temp[i++] = K_SECOND(n);
12652 temp[i++] = K_THIRD(n);
12653 }
12654#ifdef FEAT_MBYTE
12655 else if (has_mbyte)
12656 i += (*mb_char2bytes)(n, temp + i);
12657#endif
12658 else
12659 temp[i++] = n;
12660 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012661 rettv->v_type = VAR_STRING;
12662 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012663
12664#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012665 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012666 {
12667 int row = mouse_row;
12668 int col = mouse_col;
12669 win_T *win;
12670 linenr_T lnum;
12671# ifdef FEAT_WINDOWS
12672 win_T *wp;
12673# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012674 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012675
12676 if (row >= 0 && col >= 0)
12677 {
12678 /* Find the window at the mouse coordinates and compute the
12679 * text position. */
12680 win = mouse_find_win(&row, &col);
12681 (void)mouse_comp_pos(win, &row, &col, &lnum);
12682# ifdef FEAT_WINDOWS
12683 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012684 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012685# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012686 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012687 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12688 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12689 }
12690 }
12691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692 }
12693}
12694
12695/*
12696 * "getcharmod()" function
12697 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012699f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012701 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702}
12703
12704/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012705 * "getcharsearch()" function
12706 */
12707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012708f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012709{
12710 if (rettv_dict_alloc(rettv) != FAIL)
12711 {
12712 dict_T *dict = rettv->vval.v_dict;
12713
12714 dict_add_nr_str(dict, "char", 0L, last_csearch());
12715 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12716 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12717 }
12718}
12719
12720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012721 * "getcmdline()" function
12722 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012724f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012725{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012726 rettv->v_type = VAR_STRING;
12727 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728}
12729
12730/*
12731 * "getcmdpos()" function
12732 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012734f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012735{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012736 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737}
12738
12739/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012740 * "getcmdtype()" function
12741 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012743f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012744{
12745 rettv->v_type = VAR_STRING;
12746 rettv->vval.v_string = alloc(2);
12747 if (rettv->vval.v_string != NULL)
12748 {
12749 rettv->vval.v_string[0] = get_cmdline_type();
12750 rettv->vval.v_string[1] = NUL;
12751 }
12752}
12753
12754/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012755 * "getcmdwintype()" function
12756 */
12757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012758f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012759{
12760 rettv->v_type = VAR_STRING;
12761 rettv->vval.v_string = NULL;
12762#ifdef FEAT_CMDWIN
12763 rettv->vval.v_string = alloc(2);
12764 if (rettv->vval.v_string != NULL)
12765 {
12766 rettv->vval.v_string[0] = cmdwin_type;
12767 rettv->vval.v_string[1] = NUL;
12768 }
12769#endif
12770}
12771
12772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773 * "getcwd()" function
12774 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012776f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012778 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012779 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012781 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012782 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012783
12784 wp = find_tabwin(&argvars[0], &argvars[1]);
12785 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012786 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012787 if (wp->w_localdir != NULL)
12788 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12789 else if(globaldir != NULL)
12790 rettv->vval.v_string = vim_strsave(globaldir);
12791 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012792 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012793 cwd = alloc(MAXPATHL);
12794 if (cwd != NULL)
12795 {
12796 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12797 rettv->vval.v_string = vim_strsave(cwd);
12798 vim_free(cwd);
12799 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012800 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012801#ifdef BACKSLASH_IN_FILENAME
12802 if (rettv->vval.v_string != NULL)
12803 slash_adjust(rettv->vval.v_string);
12804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805 }
12806}
12807
12808/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012809 * "getfontname()" function
12810 */
12811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012812f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012813{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012814 rettv->v_type = VAR_STRING;
12815 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012816#ifdef FEAT_GUI
12817 if (gui.in_use)
12818 {
12819 GuiFont font;
12820 char_u *name = NULL;
12821
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012822 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012823 {
12824 /* Get the "Normal" font. Either the name saved by
12825 * hl_set_font_name() or from the font ID. */
12826 font = gui.norm_font;
12827 name = hl_get_font_name();
12828 }
12829 else
12830 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012831 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012832 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12833 return;
12834 font = gui_mch_get_font(name, FALSE);
12835 if (font == NOFONT)
12836 return; /* Invalid font name, return empty string. */
12837 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012838 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012839 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012840 gui_mch_free_font(font);
12841 }
12842#endif
12843}
12844
12845/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012846 * "getfperm({fname})" function
12847 */
12848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012849f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012850{
12851 char_u *fname;
12852 struct stat st;
12853 char_u *perm = NULL;
12854 char_u flags[] = "rwx";
12855 int i;
12856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012857 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012858
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012859 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012860 if (mch_stat((char *)fname, &st) >= 0)
12861 {
12862 perm = vim_strsave((char_u *)"---------");
12863 if (perm != NULL)
12864 {
12865 for (i = 0; i < 9; i++)
12866 {
12867 if (st.st_mode & (1 << (8 - i)))
12868 perm[i] = flags[i % 3];
12869 }
12870 }
12871 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012872 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012873}
12874
12875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876 * "getfsize({fname})" function
12877 */
12878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012879f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880{
12881 char_u *fname;
12882 struct stat st;
12883
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012884 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012885
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012886 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887
12888 if (mch_stat((char *)fname, &st) >= 0)
12889 {
12890 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012891 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012893 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012894 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012895
12896 /* non-perfect check for overflow */
12897 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12898 rettv->vval.v_number = -2;
12899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012900 }
12901 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012902 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012903}
12904
12905/*
12906 * "getftime({fname})" function
12907 */
12908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012909f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910{
12911 char_u *fname;
12912 struct stat st;
12913
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012914 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012915
12916 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012917 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012918 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012919 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012920}
12921
12922/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012923 * "getftype({fname})" function
12924 */
12925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012926f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012927{
12928 char_u *fname;
12929 struct stat st;
12930 char_u *type = NULL;
12931 char *t;
12932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012933 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012935 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012936 if (mch_lstat((char *)fname, &st) >= 0)
12937 {
12938#ifdef S_ISREG
12939 if (S_ISREG(st.st_mode))
12940 t = "file";
12941 else if (S_ISDIR(st.st_mode))
12942 t = "dir";
12943# ifdef S_ISLNK
12944 else if (S_ISLNK(st.st_mode))
12945 t = "link";
12946# endif
12947# ifdef S_ISBLK
12948 else if (S_ISBLK(st.st_mode))
12949 t = "bdev";
12950# endif
12951# ifdef S_ISCHR
12952 else if (S_ISCHR(st.st_mode))
12953 t = "cdev";
12954# endif
12955# ifdef S_ISFIFO
12956 else if (S_ISFIFO(st.st_mode))
12957 t = "fifo";
12958# endif
12959# ifdef S_ISSOCK
12960 else if (S_ISSOCK(st.st_mode))
12961 t = "fifo";
12962# endif
12963 else
12964 t = "other";
12965#else
12966# ifdef S_IFMT
12967 switch (st.st_mode & S_IFMT)
12968 {
12969 case S_IFREG: t = "file"; break;
12970 case S_IFDIR: t = "dir"; break;
12971# ifdef S_IFLNK
12972 case S_IFLNK: t = "link"; break;
12973# endif
12974# ifdef S_IFBLK
12975 case S_IFBLK: t = "bdev"; break;
12976# endif
12977# ifdef S_IFCHR
12978 case S_IFCHR: t = "cdev"; break;
12979# endif
12980# ifdef S_IFIFO
12981 case S_IFIFO: t = "fifo"; break;
12982# endif
12983# ifdef S_IFSOCK
12984 case S_IFSOCK: t = "socket"; break;
12985# endif
12986 default: t = "other";
12987 }
12988# else
12989 if (mch_isdir(fname))
12990 t = "dir";
12991 else
12992 t = "file";
12993# endif
12994#endif
12995 type = vim_strsave((char_u *)t);
12996 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012997 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012998}
12999
13000/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013001 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013002 */
13003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013004f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013005{
13006 linenr_T lnum;
13007 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013008 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013009
13010 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013011 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013012 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013013 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013014 retlist = FALSE;
13015 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013016 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013017 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013018 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013019 retlist = TRUE;
13020 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013021
Bram Moolenaar342337a2005-07-21 21:11:17 +000013022 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013023}
13024
13025/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013026 * "getmatches()" function
13027 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013029f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013030{
13031#ifdef FEAT_SEARCH_EXTRA
13032 dict_T *dict;
13033 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013034 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013035
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013036 if (rettv_list_alloc(rettv) == OK)
13037 {
13038 while (cur != NULL)
13039 {
13040 dict = dict_alloc();
13041 if (dict == NULL)
13042 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013043 if (cur->match.regprog == NULL)
13044 {
13045 /* match added with matchaddpos() */
13046 for (i = 0; i < MAXPOSMATCH; ++i)
13047 {
13048 llpos_T *llpos;
13049 char buf[6];
13050 list_T *l;
13051
13052 llpos = &cur->pos.pos[i];
13053 if (llpos->lnum == 0)
13054 break;
13055 l = list_alloc();
13056 if (l == NULL)
13057 break;
13058 list_append_number(l, (varnumber_T)llpos->lnum);
13059 if (llpos->col > 0)
13060 {
13061 list_append_number(l, (varnumber_T)llpos->col);
13062 list_append_number(l, (varnumber_T)llpos->len);
13063 }
13064 sprintf(buf, "pos%d", i + 1);
13065 dict_add_list(dict, buf, l);
13066 }
13067 }
13068 else
13069 {
13070 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13071 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013072 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013073 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13074 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020013075# ifdef FEAT_CONCEAL
13076 if (cur->conceal_char)
13077 {
13078 char_u buf[MB_MAXBYTES + 1];
13079
13080 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13081 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13082 }
13083# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013084 list_append_dict(rettv->vval.v_list, dict);
13085 cur = cur->next;
13086 }
13087 }
13088#endif
13089}
13090
13091/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013092 * "getpid()" function
13093 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013095f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013096{
13097 rettv->vval.v_number = mch_get_pid();
13098}
13099
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013100static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013101
13102/*
13103 * "getcurpos()" function
13104 */
13105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013106f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013107{
13108 getpos_both(argvars, rettv, TRUE);
13109}
13110
Bram Moolenaar18081e32008-02-20 19:11:07 +000013111/*
Bram Moolenaara5525202006-03-02 22:52:09 +000013112 * "getpos(string)" function
13113 */
13114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013115f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000013116{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013117 getpos_both(argvars, rettv, FALSE);
13118}
13119
13120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013121getpos_both(
13122 typval_T *argvars,
13123 typval_T *rettv,
13124 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013125{
Bram Moolenaara5525202006-03-02 22:52:09 +000013126 pos_T *fp;
13127 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013128 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013129
13130 if (rettv_list_alloc(rettv) == OK)
13131 {
13132 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013133 if (getcurpos)
13134 fp = &curwin->w_cursor;
13135 else
13136 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013137 if (fnum != -1)
13138 list_append_number(l, (varnumber_T)fnum);
13139 else
13140 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013141 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13142 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013143 list_append_number(l, (fp != NULL)
13144 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013145 : (varnumber_T)0);
13146 list_append_number(l,
13147#ifdef FEAT_VIRTUALEDIT
13148 (fp != NULL) ? (varnumber_T)fp->coladd :
13149#endif
13150 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013151 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013152 {
13153 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013154 list_append_number(l, curwin->w_curswant == MAXCOL ?
13155 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013156 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013157 }
13158 else
13159 rettv->vval.v_number = FALSE;
13160}
13161
13162/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013163 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013164 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013166f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013167{
13168#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013169 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013170#endif
13171
Bram Moolenaar2641f772005-03-25 21:58:17 +000013172#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013173 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013174 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013175 wp = NULL;
13176 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13177 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013178 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013179 if (wp == NULL)
13180 return;
13181 }
13182
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013183 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013184 }
13185#endif
13186}
13187
13188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013189 * "getreg()" function
13190 */
13191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013192f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193{
13194 char_u *strregname;
13195 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013196 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013197 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013198 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013200 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013201 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013202 strregname = get_tv_string_chk(&argvars[0]);
13203 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013204 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013205 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013206 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013207 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13208 return_list = get_tv_number_chk(&argvars[2], &error);
13209 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013211 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013212 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013213
13214 if (error)
13215 return;
13216
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217 regname = (strregname == NULL ? '"' : *strregname);
13218 if (regname == 0)
13219 regname = '"';
13220
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013221 if (return_list)
13222 {
13223 rettv->v_type = VAR_LIST;
13224 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13225 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013226 if (rettv->vval.v_list != NULL)
13227 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013228 }
13229 else
13230 {
13231 rettv->v_type = VAR_STRING;
13232 rettv->vval.v_string = get_reg_contents(regname,
13233 arg2 ? GREG_EXPR_SRC : 0);
13234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235}
13236
13237/*
13238 * "getregtype()" function
13239 */
13240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013241f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242{
13243 char_u *strregname;
13244 int regname;
13245 char_u buf[NUMBUFLEN + 2];
13246 long reglen = 0;
13247
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013248 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013249 {
13250 strregname = get_tv_string_chk(&argvars[0]);
13251 if (strregname == NULL) /* type error; errmsg already given */
13252 {
13253 rettv->v_type = VAR_STRING;
13254 rettv->vval.v_string = NULL;
13255 return;
13256 }
13257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258 else
13259 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013260 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013261
13262 regname = (strregname == NULL ? '"' : *strregname);
13263 if (regname == 0)
13264 regname = '"';
13265
13266 buf[0] = NUL;
13267 buf[1] = NUL;
13268 switch (get_reg_type(regname, &reglen))
13269 {
13270 case MLINE: buf[0] = 'V'; break;
13271 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272 case MBLOCK:
13273 buf[0] = Ctrl_V;
13274 sprintf((char *)buf + 1, "%ld", reglen + 1);
13275 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013276 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013277 rettv->v_type = VAR_STRING;
13278 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279}
13280
13281/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013282 * "gettabvar()" function
13283 */
13284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013285f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013286{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013287 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013288 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013289 dictitem_T *v;
13290 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013291 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013292
13293 rettv->v_type = VAR_STRING;
13294 rettv->vval.v_string = NULL;
13295
13296 varname = get_tv_string_chk(&argvars[1]);
13297 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13298 if (tp != NULL && varname != NULL)
13299 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013300 /* Set tp to be our tabpage, temporarily. Also set the window to the
13301 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013302 if (switch_win(&oldcurwin, &oldtabpage,
13303 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013304 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013305 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013306 /* look up the variable */
13307 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13308 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13309 if (v != NULL)
13310 {
13311 copy_tv(&v->di_tv, rettv);
13312 done = TRUE;
13313 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013314 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013315
13316 /* restore previous notion of curwin */
13317 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013318 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013319
13320 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013321 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013322}
13323
13324/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013325 * "gettabwinvar()" function
13326 */
13327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013328f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013329{
13330 getwinvar(argvars, rettv, 1);
13331}
13332
13333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013334 * "getwinposx()" function
13335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013337f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013339 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340#ifdef FEAT_GUI
13341 if (gui.in_use)
13342 {
13343 int x, y;
13344
13345 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013346 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347 }
13348#endif
13349}
13350
13351/*
13352 * "getwinposy()" function
13353 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013355f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013356{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013357 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358#ifdef FEAT_GUI
13359 if (gui.in_use)
13360 {
13361 int x, y;
13362
13363 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013364 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013365 }
13366#endif
13367}
13368
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013369/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013370 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013371 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013372 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013373find_win_by_nr(
13374 typval_T *vp,
13375 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013376{
13377#ifdef FEAT_WINDOWS
13378 win_T *wp;
13379#endif
13380 int nr;
13381
13382 nr = get_tv_number_chk(vp, NULL);
13383
13384#ifdef FEAT_WINDOWS
13385 if (nr < 0)
13386 return NULL;
13387 if (nr == 0)
13388 return curwin;
13389
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013390 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13391 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013392 if (--nr <= 0)
13393 break;
13394 return wp;
13395#else
13396 if (nr == 0 || nr == 1)
13397 return curwin;
13398 return NULL;
13399#endif
13400}
13401
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013403 * Find window specified by "wvp" in tabpage "tvp".
13404 */
13405 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013406find_tabwin(
13407 typval_T *wvp, /* VAR_UNKNOWN for current window */
13408 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013409{
13410 win_T *wp = NULL;
13411 tabpage_T *tp = NULL;
13412 long n;
13413
13414 if (wvp->v_type != VAR_UNKNOWN)
13415 {
13416 if (tvp->v_type != VAR_UNKNOWN)
13417 {
13418 n = get_tv_number(tvp);
13419 if (n >= 0)
13420 tp = find_tabpage(n);
13421 }
13422 else
13423 tp = curtab;
13424
13425 if (tp != NULL)
13426 wp = find_win_by_nr(wvp, tp);
13427 }
13428 else
13429 wp = curwin;
13430
13431 return wp;
13432}
13433
13434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435 * "getwinvar()" function
13436 */
13437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013438f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013440 getwinvar(argvars, rettv, 0);
13441}
13442
13443/*
13444 * getwinvar() and gettabwinvar()
13445 */
13446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013447getwinvar(
13448 typval_T *argvars,
13449 typval_T *rettv,
13450 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013451{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013452 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013453 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013454 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013455 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013456 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013457#ifdef FEAT_WINDOWS
13458 win_T *oldcurwin;
13459 tabpage_T *oldtabpage;
13460 int need_switch_win;
13461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013463#ifdef FEAT_WINDOWS
13464 if (off == 1)
13465 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13466 else
13467 tp = curtab;
13468#endif
13469 win = find_win_by_nr(&argvars[off], tp);
13470 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013471 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013472
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013473 rettv->v_type = VAR_STRING;
13474 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475
13476 if (win != NULL && varname != NULL)
13477 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013478#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013479 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013480 * otherwise the window is not valid. Only do this when needed,
13481 * autocommands get blocked. */
13482 need_switch_win = !(tp == curtab && win == curwin);
13483 if (!need_switch_win
13484 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13485#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013486 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013487 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013488 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013489 if (get_option_tv(&varname, rettv, 1) == OK)
13490 done = TRUE;
13491 }
13492 else
13493 {
13494 /* Look up the variable. */
13495 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13496 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13497 varname, FALSE);
13498 if (v != NULL)
13499 {
13500 copy_tv(&v->di_tv, rettv);
13501 done = TRUE;
13502 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013505
Bram Moolenaarba117c22015-09-29 16:53:22 +020013506#ifdef FEAT_WINDOWS
13507 if (need_switch_win)
13508 /* restore previous notion of curwin */
13509 restore_win(oldcurwin, oldtabpage, TRUE);
13510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013511 }
13512
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013513 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13514 /* use the default return value */
13515 copy_tv(&argvars[off + 2], rettv);
13516
Bram Moolenaar071d4272004-06-13 20:20:40 +000013517 --emsg_off;
13518}
13519
13520/*
13521 * "glob()" function
13522 */
13523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013524f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013526 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013528 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013530 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013531 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013532 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013533 if (argvars[1].v_type != VAR_UNKNOWN)
13534 {
13535 if (get_tv_number_chk(&argvars[1], &error))
13536 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013537 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013538 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013539 if (get_tv_number_chk(&argvars[2], &error))
13540 {
13541 rettv->v_type = VAR_LIST;
13542 rettv->vval.v_list = NULL;
13543 }
13544 if (argvars[3].v_type != VAR_UNKNOWN
13545 && get_tv_number_chk(&argvars[3], &error))
13546 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013547 }
13548 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013549 if (!error)
13550 {
13551 ExpandInit(&xpc);
13552 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013553 if (p_wic)
13554 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013555 if (rettv->v_type == VAR_STRING)
13556 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013557 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013558 else if (rettv_list_alloc(rettv) != FAIL)
13559 {
13560 int i;
13561
13562 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13563 NULL, options, WILD_ALL_KEEP);
13564 for (i = 0; i < xpc.xp_numfiles; i++)
13565 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13566
13567 ExpandCleanup(&xpc);
13568 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013569 }
13570 else
13571 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572}
13573
13574/*
13575 * "globpath()" function
13576 */
13577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013578f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013580 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013582 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013583 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013584 garray_T ga;
13585 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013587 /* When the optional second argument is non-zero, don't remove matches
13588 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013589 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013590 if (argvars[2].v_type != VAR_UNKNOWN)
13591 {
13592 if (get_tv_number_chk(&argvars[2], &error))
13593 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013594 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013595 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013596 if (get_tv_number_chk(&argvars[3], &error))
13597 {
13598 rettv->v_type = VAR_LIST;
13599 rettv->vval.v_list = NULL;
13600 }
13601 if (argvars[4].v_type != VAR_UNKNOWN
13602 && get_tv_number_chk(&argvars[4], &error))
13603 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013604 }
13605 }
13606 if (file != NULL && !error)
13607 {
13608 ga_init2(&ga, (int)sizeof(char_u *), 10);
13609 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13610 if (rettv->v_type == VAR_STRING)
13611 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13612 else if (rettv_list_alloc(rettv) != FAIL)
13613 for (i = 0; i < ga.ga_len; ++i)
13614 list_append_string(rettv->vval.v_list,
13615 ((char_u **)(ga.ga_data))[i], -1);
13616 ga_clear_strings(&ga);
13617 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013618 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013619 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620}
13621
13622/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013623 * "glob2regpat()" function
13624 */
13625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013626f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013627{
13628 char_u *pat = get_tv_string_chk(&argvars[0]);
13629
13630 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013631 rettv->vval.v_string = (pat == NULL)
13632 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013633}
13634
13635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636 * "has()" function
13637 */
13638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013639f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640{
13641 int i;
13642 char_u *name;
13643 int n = FALSE;
13644 static char *(has_list[]) =
13645 {
13646#ifdef AMIGA
13647 "amiga",
13648# ifdef FEAT_ARP
13649 "arp",
13650# endif
13651#endif
13652#ifdef __BEOS__
13653 "beos",
13654#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013655#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656 "mac",
13657#endif
13658#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013659 "macunix", /* built with 'darwin' enabled */
13660#endif
13661#if defined(__APPLE__) && __APPLE__ == 1
13662 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664#ifdef __QNX__
13665 "qnx",
13666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667#ifdef UNIX
13668 "unix",
13669#endif
13670#ifdef VMS
13671 "vms",
13672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673#ifdef WIN32
13674 "win32",
13675#endif
13676#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13677 "win32unix",
13678#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013679#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680 "win64",
13681#endif
13682#ifdef EBCDIC
13683 "ebcdic",
13684#endif
13685#ifndef CASE_INSENSITIVE_FILENAME
13686 "fname_case",
13687#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013688#ifdef HAVE_ACL
13689 "acl",
13690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691#ifdef FEAT_ARABIC
13692 "arabic",
13693#endif
13694#ifdef FEAT_AUTOCMD
13695 "autocmd",
13696#endif
13697#ifdef FEAT_BEVAL
13698 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013699# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13700 "balloon_multiline",
13701# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702#endif
13703#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13704 "builtin_terms",
13705# ifdef ALL_BUILTIN_TCAPS
13706 "all_builtin_terms",
13707# endif
13708#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013709#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13710 || defined(FEAT_GUI_W32) \
13711 || defined(FEAT_GUI_MOTIF))
13712 "browsefilter",
13713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714#ifdef FEAT_BYTEOFF
13715 "byte_offset",
13716#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013717#ifdef FEAT_CHANNEL
13718 "channel",
13719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720#ifdef FEAT_CINDENT
13721 "cindent",
13722#endif
13723#ifdef FEAT_CLIENTSERVER
13724 "clientserver",
13725#endif
13726#ifdef FEAT_CLIPBOARD
13727 "clipboard",
13728#endif
13729#ifdef FEAT_CMDL_COMPL
13730 "cmdline_compl",
13731#endif
13732#ifdef FEAT_CMDHIST
13733 "cmdline_hist",
13734#endif
13735#ifdef FEAT_COMMENTS
13736 "comments",
13737#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013738#ifdef FEAT_CONCEAL
13739 "conceal",
13740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741#ifdef FEAT_CRYPT
13742 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013743 "crypt-blowfish",
13744 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745#endif
13746#ifdef FEAT_CSCOPE
13747 "cscope",
13748#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013749#ifdef FEAT_CURSORBIND
13750 "cursorbind",
13751#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013752#ifdef CURSOR_SHAPE
13753 "cursorshape",
13754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755#ifdef DEBUG
13756 "debug",
13757#endif
13758#ifdef FEAT_CON_DIALOG
13759 "dialog_con",
13760#endif
13761#ifdef FEAT_GUI_DIALOG
13762 "dialog_gui",
13763#endif
13764#ifdef FEAT_DIFF
13765 "diff",
13766#endif
13767#ifdef FEAT_DIGRAPHS
13768 "digraphs",
13769#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013770#ifdef FEAT_DIRECTX
13771 "directx",
13772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773#ifdef FEAT_DND
13774 "dnd",
13775#endif
13776#ifdef FEAT_EMACS_TAGS
13777 "emacs_tags",
13778#endif
13779 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013780 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781#ifdef FEAT_SEARCH_EXTRA
13782 "extra_search",
13783#endif
13784#ifdef FEAT_FKMAP
13785 "farsi",
13786#endif
13787#ifdef FEAT_SEARCHPATH
13788 "file_in_path",
13789#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013790#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013791 "filterpipe",
13792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013793#ifdef FEAT_FIND_ID
13794 "find_in_path",
13795#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013796#ifdef FEAT_FLOAT
13797 "float",
13798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799#ifdef FEAT_FOLDING
13800 "folding",
13801#endif
13802#ifdef FEAT_FOOTER
13803 "footer",
13804#endif
13805#if !defined(USE_SYSTEM) && defined(UNIX)
13806 "fork",
13807#endif
13808#ifdef FEAT_GETTEXT
13809 "gettext",
13810#endif
13811#ifdef FEAT_GUI
13812 "gui",
13813#endif
13814#ifdef FEAT_GUI_ATHENA
13815# ifdef FEAT_GUI_NEXTAW
13816 "gui_neXtaw",
13817# else
13818 "gui_athena",
13819# endif
13820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821#ifdef FEAT_GUI_GTK
13822 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013823# ifdef USE_GTK3
13824 "gui_gtk3",
13825# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013826 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013827# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013829#ifdef FEAT_GUI_GNOME
13830 "gui_gnome",
13831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832#ifdef FEAT_GUI_MAC
13833 "gui_mac",
13834#endif
13835#ifdef FEAT_GUI_MOTIF
13836 "gui_motif",
13837#endif
13838#ifdef FEAT_GUI_PHOTON
13839 "gui_photon",
13840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841#ifdef FEAT_GUI_W32
13842 "gui_win32",
13843#endif
13844#ifdef FEAT_HANGULIN
13845 "hangul_input",
13846#endif
13847#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13848 "iconv",
13849#endif
13850#ifdef FEAT_INS_EXPAND
13851 "insert_expand",
13852#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013853#ifdef FEAT_JOB
13854 "job",
13855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856#ifdef FEAT_JUMPLIST
13857 "jumplist",
13858#endif
13859#ifdef FEAT_KEYMAP
13860 "keymap",
13861#endif
13862#ifdef FEAT_LANGMAP
13863 "langmap",
13864#endif
13865#ifdef FEAT_LIBCALL
13866 "libcall",
13867#endif
13868#ifdef FEAT_LINEBREAK
13869 "linebreak",
13870#endif
13871#ifdef FEAT_LISP
13872 "lispindent",
13873#endif
13874#ifdef FEAT_LISTCMDS
13875 "listcmds",
13876#endif
13877#ifdef FEAT_LOCALMAP
13878 "localmap",
13879#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013880#ifdef FEAT_LUA
13881# ifndef DYNAMIC_LUA
13882 "lua",
13883# endif
13884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885#ifdef FEAT_MENU
13886 "menu",
13887#endif
13888#ifdef FEAT_SESSION
13889 "mksession",
13890#endif
13891#ifdef FEAT_MODIFY_FNAME
13892 "modify_fname",
13893#endif
13894#ifdef FEAT_MOUSE
13895 "mouse",
13896#endif
13897#ifdef FEAT_MOUSESHAPE
13898 "mouseshape",
13899#endif
13900#if defined(UNIX) || defined(VMS)
13901# ifdef FEAT_MOUSE_DEC
13902 "mouse_dec",
13903# endif
13904# ifdef FEAT_MOUSE_GPM
13905 "mouse_gpm",
13906# endif
13907# ifdef FEAT_MOUSE_JSB
13908 "mouse_jsbterm",
13909# endif
13910# ifdef FEAT_MOUSE_NET
13911 "mouse_netterm",
13912# endif
13913# ifdef FEAT_MOUSE_PTERM
13914 "mouse_pterm",
13915# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013916# ifdef FEAT_MOUSE_SGR
13917 "mouse_sgr",
13918# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013919# ifdef FEAT_SYSMOUSE
13920 "mouse_sysmouse",
13921# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013922# ifdef FEAT_MOUSE_URXVT
13923 "mouse_urxvt",
13924# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925# ifdef FEAT_MOUSE_XTERM
13926 "mouse_xterm",
13927# endif
13928#endif
13929#ifdef FEAT_MBYTE
13930 "multi_byte",
13931#endif
13932#ifdef FEAT_MBYTE_IME
13933 "multi_byte_ime",
13934#endif
13935#ifdef FEAT_MULTI_LANG
13936 "multi_lang",
13937#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013938#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013939#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013940 "mzscheme",
13941#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943#ifdef FEAT_OLE
13944 "ole",
13945#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013946 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947#ifdef FEAT_PATH_EXTRA
13948 "path_extra",
13949#endif
13950#ifdef FEAT_PERL
13951#ifndef DYNAMIC_PERL
13952 "perl",
13953#endif
13954#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013955#ifdef FEAT_PERSISTENT_UNDO
13956 "persistent_undo",
13957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958#ifdef FEAT_PYTHON
13959#ifndef DYNAMIC_PYTHON
13960 "python",
13961#endif
13962#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013963#ifdef FEAT_PYTHON3
13964#ifndef DYNAMIC_PYTHON3
13965 "python3",
13966#endif
13967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968#ifdef FEAT_POSTSCRIPT
13969 "postscript",
13970#endif
13971#ifdef FEAT_PRINTER
13972 "printer",
13973#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013974#ifdef FEAT_PROFILE
13975 "profile",
13976#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013977#ifdef FEAT_RELTIME
13978 "reltime",
13979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013980#ifdef FEAT_QUICKFIX
13981 "quickfix",
13982#endif
13983#ifdef FEAT_RIGHTLEFT
13984 "rightleft",
13985#endif
13986#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13987 "ruby",
13988#endif
13989#ifdef FEAT_SCROLLBIND
13990 "scrollbind",
13991#endif
13992#ifdef FEAT_CMDL_INFO
13993 "showcmd",
13994 "cmdline_info",
13995#endif
13996#ifdef FEAT_SIGNS
13997 "signs",
13998#endif
13999#ifdef FEAT_SMARTINDENT
14000 "smartindent",
14001#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014002#ifdef STARTUPTIME
14003 "startuptime",
14004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005#ifdef FEAT_STL_OPT
14006 "statusline",
14007#endif
14008#ifdef FEAT_SUN_WORKSHOP
14009 "sun_workshop",
14010#endif
14011#ifdef FEAT_NETBEANS_INTG
14012 "netbeans_intg",
14013#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014014#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014015 "spell",
14016#endif
14017#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018 "syntax",
14019#endif
14020#if defined(USE_SYSTEM) || !defined(UNIX)
14021 "system",
14022#endif
14023#ifdef FEAT_TAG_BINS
14024 "tag_binary",
14025#endif
14026#ifdef FEAT_TAG_OLDSTATIC
14027 "tag_old_static",
14028#endif
14029#ifdef FEAT_TAG_ANYWHITE
14030 "tag_any_white",
14031#endif
14032#ifdef FEAT_TCL
14033# ifndef DYNAMIC_TCL
14034 "tcl",
14035# endif
14036#endif
14037#ifdef TERMINFO
14038 "terminfo",
14039#endif
14040#ifdef FEAT_TERMRESPONSE
14041 "termresponse",
14042#endif
14043#ifdef FEAT_TEXTOBJ
14044 "textobjects",
14045#endif
14046#ifdef HAVE_TGETENT
14047 "tgetent",
14048#endif
14049#ifdef FEAT_TITLE
14050 "title",
14051#endif
14052#ifdef FEAT_TOOLBAR
14053 "toolbar",
14054#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014055#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14056 "unnamedplus",
14057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058#ifdef FEAT_USR_CMDS
14059 "user-commands", /* was accidentally included in 5.4 */
14060 "user_commands",
14061#endif
14062#ifdef FEAT_VIMINFO
14063 "viminfo",
14064#endif
14065#ifdef FEAT_VERTSPLIT
14066 "vertsplit",
14067#endif
14068#ifdef FEAT_VIRTUALEDIT
14069 "virtualedit",
14070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014072#ifdef FEAT_VISUALEXTRA
14073 "visualextra",
14074#endif
14075#ifdef FEAT_VREPLACE
14076 "vreplace",
14077#endif
14078#ifdef FEAT_WILDIGN
14079 "wildignore",
14080#endif
14081#ifdef FEAT_WILDMENU
14082 "wildmenu",
14083#endif
14084#ifdef FEAT_WINDOWS
14085 "windows",
14086#endif
14087#ifdef FEAT_WAK
14088 "winaltkeys",
14089#endif
14090#ifdef FEAT_WRITEBACKUP
14091 "writebackup",
14092#endif
14093#ifdef FEAT_XIM
14094 "xim",
14095#endif
14096#ifdef FEAT_XFONTSET
14097 "xfontset",
14098#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014099#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014100 "xpm",
14101 "xpm_w32", /* for backward compatibility */
14102#else
14103# if defined(HAVE_XPM)
14104 "xpm",
14105# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107#ifdef USE_XSMP
14108 "xsmp",
14109#endif
14110#ifdef USE_XSMP_INTERACT
14111 "xsmp_interact",
14112#endif
14113#ifdef FEAT_XCLIPBOARD
14114 "xterm_clipboard",
14115#endif
14116#ifdef FEAT_XTERM_SAVE
14117 "xterm_save",
14118#endif
14119#if defined(UNIX) && defined(FEAT_X11)
14120 "X11",
14121#endif
14122 NULL
14123 };
14124
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014125 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126 for (i = 0; has_list[i] != NULL; ++i)
14127 if (STRICMP(name, has_list[i]) == 0)
14128 {
14129 n = TRUE;
14130 break;
14131 }
14132
14133 if (n == FALSE)
14134 {
14135 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014136 {
14137 if (name[5] == '-'
14138 && STRLEN(name) > 11
14139 && vim_isdigit(name[6])
14140 && vim_isdigit(name[8])
14141 && vim_isdigit(name[10]))
14142 {
14143 int major = atoi((char *)name + 6);
14144 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014145
14146 /* Expect "patch-9.9.01234". */
14147 n = (major < VIM_VERSION_MAJOR
14148 || (major == VIM_VERSION_MAJOR
14149 && (minor < VIM_VERSION_MINOR
14150 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014151 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014152 }
14153 else
14154 n = has_patch(atoi((char *)name + 5));
14155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014156 else if (STRICMP(name, "vim_starting") == 0)
14157 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014158#ifdef FEAT_MBYTE
14159 else if (STRICMP(name, "multi_byte_encoding") == 0)
14160 n = has_mbyte;
14161#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014162#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14163 else if (STRICMP(name, "balloon_multiline") == 0)
14164 n = multiline_balloon_available();
14165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014166#ifdef DYNAMIC_TCL
14167 else if (STRICMP(name, "tcl") == 0)
14168 n = tcl_enabled(FALSE);
14169#endif
14170#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14171 else if (STRICMP(name, "iconv") == 0)
14172 n = iconv_enabled(FALSE);
14173#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014174#ifdef DYNAMIC_LUA
14175 else if (STRICMP(name, "lua") == 0)
14176 n = lua_enabled(FALSE);
14177#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014178#ifdef DYNAMIC_MZSCHEME
14179 else if (STRICMP(name, "mzscheme") == 0)
14180 n = mzscheme_enabled(FALSE);
14181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014182#ifdef DYNAMIC_RUBY
14183 else if (STRICMP(name, "ruby") == 0)
14184 n = ruby_enabled(FALSE);
14185#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014186#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014187#ifdef DYNAMIC_PYTHON
14188 else if (STRICMP(name, "python") == 0)
14189 n = python_enabled(FALSE);
14190#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014191#endif
14192#ifdef FEAT_PYTHON3
14193#ifdef DYNAMIC_PYTHON3
14194 else if (STRICMP(name, "python3") == 0)
14195 n = python3_enabled(FALSE);
14196#endif
14197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198#ifdef DYNAMIC_PERL
14199 else if (STRICMP(name, "perl") == 0)
14200 n = perl_enabled(FALSE);
14201#endif
14202#ifdef FEAT_GUI
14203 else if (STRICMP(name, "gui_running") == 0)
14204 n = (gui.in_use || gui.starting);
14205# ifdef FEAT_GUI_W32
14206 else if (STRICMP(name, "gui_win32s") == 0)
14207 n = gui_is_win32s();
14208# endif
14209# ifdef FEAT_BROWSE
14210 else if (STRICMP(name, "browse") == 0)
14211 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14212# endif
14213#endif
14214#ifdef FEAT_SYN_HL
14215 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014216 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217#endif
14218#if defined(WIN3264)
14219 else if (STRICMP(name, "win95") == 0)
14220 n = mch_windows95();
14221#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014222#ifdef FEAT_NETBEANS_INTG
14223 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014224 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014226 }
14227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014228 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229}
14230
14231/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014232 * "has_key()" function
14233 */
14234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014235f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014236{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014237 if (argvars[0].v_type != VAR_DICT)
14238 {
14239 EMSG(_(e_dictreq));
14240 return;
14241 }
14242 if (argvars[0].vval.v_dict == NULL)
14243 return;
14244
14245 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014246 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014247}
14248
14249/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014250 * "haslocaldir()" function
14251 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014253f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014254{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014255 win_T *wp = NULL;
14256
14257 wp = find_tabwin(&argvars[0], &argvars[1]);
14258 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014259}
14260
14261/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014262 * "hasmapto()" function
14263 */
14264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014265f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014266{
14267 char_u *name;
14268 char_u *mode;
14269 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014270 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014271
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014272 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014273 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274 mode = (char_u *)"nvo";
14275 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014276 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014277 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014278 if (argvars[2].v_type != VAR_UNKNOWN)
14279 abbr = get_tv_number(&argvars[2]);
14280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014281
Bram Moolenaar2c932302006-03-18 21:42:09 +000014282 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014283 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014284 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014285 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286}
14287
14288/*
14289 * "histadd()" function
14290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014292f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293{
14294#ifdef FEAT_CMDHIST
14295 int histype;
14296 char_u *str;
14297 char_u buf[NUMBUFLEN];
14298#endif
14299
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014300 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301 if (check_restricted() || check_secure())
14302 return;
14303#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014304 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14305 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014306 if (histype >= 0)
14307 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014308 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309 if (*str != NUL)
14310 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014311 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014313 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014314 return;
14315 }
14316 }
14317#endif
14318}
14319
14320/*
14321 * "histdel()" function
14322 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014324f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014325{
14326#ifdef FEAT_CMDHIST
14327 int n;
14328 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014329 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014331 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14332 if (str == NULL)
14333 n = 0;
14334 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014335 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014336 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014337 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014338 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014339 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014340 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014341 else
14342 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014343 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014344 get_tv_string_buf(&argvars[1], buf));
14345 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346#endif
14347}
14348
14349/*
14350 * "histget()" function
14351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014353f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354{
14355#ifdef FEAT_CMDHIST
14356 int type;
14357 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014358 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014359
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014360 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14361 if (str == NULL)
14362 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014364 {
14365 type = get_histtype(str);
14366 if (argvars[1].v_type == VAR_UNKNOWN)
14367 idx = get_history_idx(type);
14368 else
14369 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14370 /* -1 on type error */
14371 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014374 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014376 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377}
14378
14379/*
14380 * "histnr()" function
14381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014383f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384{
14385 int i;
14386
14387#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014388 char_u *history = get_tv_string_chk(&argvars[0]);
14389
14390 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391 if (i >= HIST_CMD && i < HIST_COUNT)
14392 i = get_history_idx(i);
14393 else
14394#endif
14395 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014396 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397}
14398
14399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400 * "highlightID(name)" function
14401 */
14402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014403f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014404{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014405 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406}
14407
14408/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014409 * "highlight_exists()" function
14410 */
14411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014412f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014413{
14414 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14415}
14416
14417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418 * "hostname()" function
14419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014421f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014422{
14423 char_u hostname[256];
14424
14425 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014426 rettv->v_type = VAR_STRING;
14427 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014428}
14429
14430/*
14431 * iconv() function
14432 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014434f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435{
14436#ifdef FEAT_MBYTE
14437 char_u buf1[NUMBUFLEN];
14438 char_u buf2[NUMBUFLEN];
14439 char_u *from, *to, *str;
14440 vimconv_T vimconv;
14441#endif
14442
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014443 rettv->v_type = VAR_STRING;
14444 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445
14446#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014447 str = get_tv_string(&argvars[0]);
14448 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14449 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014450 vimconv.vc_type = CONV_NONE;
14451 convert_setup(&vimconv, from, to);
14452
14453 /* If the encodings are equal, no conversion needed. */
14454 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014455 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014457 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458
14459 convert_setup(&vimconv, NULL, NULL);
14460 vim_free(from);
14461 vim_free(to);
14462#endif
14463}
14464
14465/*
14466 * "indent()" function
14467 */
14468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014469f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014470{
14471 linenr_T lnum;
14472
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014473 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014475 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014477 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478}
14479
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014480/*
14481 * "index()" function
14482 */
14483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014484f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014485{
Bram Moolenaar33570922005-01-25 22:26:29 +000014486 list_T *l;
14487 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014488 long idx = 0;
14489 int ic = FALSE;
14490
14491 rettv->vval.v_number = -1;
14492 if (argvars[0].v_type != VAR_LIST)
14493 {
14494 EMSG(_(e_listreq));
14495 return;
14496 }
14497 l = argvars[0].vval.v_list;
14498 if (l != NULL)
14499 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014500 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014501 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014502 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014503 int error = FALSE;
14504
Bram Moolenaar758711c2005-02-02 23:11:38 +000014505 /* Start at specified item. Use the cached index that list_find()
14506 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014507 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014508 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014509 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014510 ic = get_tv_number_chk(&argvars[3], &error);
14511 if (error)
14512 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014513 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014514
Bram Moolenaar758711c2005-02-02 23:11:38 +000014515 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014516 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014517 {
14518 rettv->vval.v_number = idx;
14519 break;
14520 }
14521 }
14522}
14523
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524static int inputsecret_flag = 0;
14525
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014526static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014527
Bram Moolenaar071d4272004-06-13 20:20:40 +000014528/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014529 * This function is used by f_input() and f_inputdialog() functions. The third
14530 * argument to f_input() specifies the type of completion to use at the
14531 * prompt. The third argument to f_inputdialog() specifies the value to return
14532 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014533 */
14534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014535get_user_input(
14536 typval_T *argvars,
14537 typval_T *rettv,
14538 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014540 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014541 char_u *p = NULL;
14542 int c;
14543 char_u buf[NUMBUFLEN];
14544 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014545 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014546 int xp_type = EXPAND_NOTHING;
14547 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014548
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014549 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014550 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551
14552#ifdef NO_CONSOLE_INPUT
14553 /* While starting up, there is no place to enter text. */
14554 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014556#endif
14557
14558 cmd_silent = FALSE; /* Want to see the prompt. */
14559 if (prompt != NULL)
14560 {
14561 /* Only the part of the message after the last NL is considered as
14562 * prompt for the command line */
14563 p = vim_strrchr(prompt, '\n');
14564 if (p == NULL)
14565 p = prompt;
14566 else
14567 {
14568 ++p;
14569 c = *p;
14570 *p = NUL;
14571 msg_start();
14572 msg_clr_eos();
14573 msg_puts_attr(prompt, echo_attr);
14574 msg_didout = FALSE;
14575 msg_starthere();
14576 *p = c;
14577 }
14578 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014579
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014580 if (argvars[1].v_type != VAR_UNKNOWN)
14581 {
14582 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14583 if (defstr != NULL)
14584 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014586 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014587 {
14588 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014589 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014590 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014591
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014592 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014593 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014594
Bram Moolenaar4463f292005-09-25 22:20:24 +000014595 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14596 if (xp_name == NULL)
14597 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014598
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014599 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014600
Bram Moolenaar4463f292005-09-25 22:20:24 +000014601 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14602 &xp_arg) == FAIL)
14603 return;
14604 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014605 }
14606
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014607 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014608 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014609 int save_ex_normal_busy = ex_normal_busy;
14610 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014611 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014612 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14613 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014614 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014615 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014616 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014617 && argvars[1].v_type != VAR_UNKNOWN
14618 && argvars[2].v_type != VAR_UNKNOWN)
14619 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14620 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014621
14622 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014623
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014624 /* since the user typed this, no need to wait for return */
14625 need_wait_return = FALSE;
14626 msg_didout = FALSE;
14627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014628 cmd_silent = cmd_silent_save;
14629}
14630
14631/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014632 * "input()" function
14633 * Also handles inputsecret() when inputsecret is set.
14634 */
14635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014636f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014637{
14638 get_user_input(argvars, rettv, FALSE);
14639}
14640
14641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014642 * "inputdialog()" function
14643 */
14644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014645f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014646{
14647#if defined(FEAT_GUI_TEXTDIALOG)
14648 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14649 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14650 {
14651 char_u *message;
14652 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014653 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014654
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014655 message = get_tv_string_chk(&argvars[0]);
14656 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014657 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014658 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014659 else
14660 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014661 if (message != NULL && defstr != NULL
14662 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014663 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014664 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014665 else
14666 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014667 if (message != NULL && defstr != NULL
14668 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014669 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014670 rettv->vval.v_string = vim_strsave(
14671 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014672 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014673 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014675 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014676 }
14677 else
14678#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014679 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014680}
14681
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014682/*
14683 * "inputlist()" function
14684 */
14685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014686f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014687{
14688 listitem_T *li;
14689 int selected;
14690 int mouse_used;
14691
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014692#ifdef NO_CONSOLE_INPUT
14693 /* While starting up, there is no place to enter text. */
14694 if (no_console_input())
14695 return;
14696#endif
14697 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14698 {
14699 EMSG2(_(e_listarg), "inputlist()");
14700 return;
14701 }
14702
14703 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014704 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014705 lines_left = Rows; /* avoid more prompt */
14706 msg_scroll = TRUE;
14707 msg_clr_eos();
14708
14709 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14710 {
14711 msg_puts(get_tv_string(&li->li_tv));
14712 msg_putchar('\n');
14713 }
14714
14715 /* Ask for choice. */
14716 selected = prompt_for_number(&mouse_used);
14717 if (mouse_used)
14718 selected -= lines_left;
14719
14720 rettv->vval.v_number = selected;
14721}
14722
14723
Bram Moolenaar071d4272004-06-13 20:20:40 +000014724static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14725
14726/*
14727 * "inputrestore()" function
14728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014730f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731{
14732 if (ga_userinput.ga_len > 0)
14733 {
14734 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014735 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14736 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014737 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014738 }
14739 else if (p_verbose > 1)
14740 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014741 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014742 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014743 }
14744}
14745
14746/*
14747 * "inputsave()" function
14748 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014750f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014752 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014753 if (ga_grow(&ga_userinput, 1) == OK)
14754 {
14755 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14756 + ga_userinput.ga_len);
14757 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014758 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014759 }
14760 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014761 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014762}
14763
14764/*
14765 * "inputsecret()" function
14766 */
14767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014768f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769{
14770 ++cmdline_star;
14771 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014772 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014773 --cmdline_star;
14774 --inputsecret_flag;
14775}
14776
14777/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014778 * "insert()" function
14779 */
14780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014781f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014782{
14783 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014784 listitem_T *item;
14785 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014786 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014787
14788 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014789 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014790 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014791 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014792 {
14793 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014794 before = get_tv_number_chk(&argvars[2], &error);
14795 if (error)
14796 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014797
Bram Moolenaar758711c2005-02-02 23:11:38 +000014798 if (before == l->lv_len)
14799 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014800 else
14801 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014802 item = list_find(l, before);
14803 if (item == NULL)
14804 {
14805 EMSGN(_(e_listidx), before);
14806 l = NULL;
14807 }
14808 }
14809 if (l != NULL)
14810 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014811 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014812 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014813 }
14814 }
14815}
14816
14817/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014818 * "invert(expr)" function
14819 */
14820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014821f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014822{
14823 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14824}
14825
14826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014827 * "isdirectory()" function
14828 */
14829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014830f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014831{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014832 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014833}
14834
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014835/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014836 * "islocked()" function
14837 */
14838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014839f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014840{
14841 lval_T lv;
14842 char_u *end;
14843 dictitem_T *di;
14844
14845 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014846 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14847 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014848 if (end != NULL && lv.ll_name != NULL)
14849 {
14850 if (*end != NUL)
14851 EMSG(_(e_trailing));
14852 else
14853 {
14854 if (lv.ll_tv == NULL)
14855 {
14856 if (check_changedtick(lv.ll_name))
14857 rettv->vval.v_number = 1; /* always locked */
14858 else
14859 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014860 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014861 if (di != NULL)
14862 {
14863 /* Consider a variable locked when:
14864 * 1. the variable itself is locked
14865 * 2. the value of the variable is locked.
14866 * 3. the List or Dict value is locked.
14867 */
14868 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14869 || tv_islocked(&di->di_tv));
14870 }
14871 }
14872 }
14873 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014874 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014875 else if (lv.ll_newkey != NULL)
14876 EMSG2(_(e_dictkey), lv.ll_newkey);
14877 else if (lv.ll_list != NULL)
14878 /* List item. */
14879 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14880 else
14881 /* Dictionary item. */
14882 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14883 }
14884 }
14885
14886 clear_lval(&lv);
14887}
14888
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014889#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14890/*
14891 * "isnan()" function
14892 */
14893 static void
14894f_isnan(typval_T *argvars, typval_T *rettv)
14895{
14896 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14897 && isnan(argvars[0].vval.v_float);
14898}
14899#endif
14900
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014901static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014902
14903/*
14904 * Turn a dict into a list:
14905 * "what" == 0: list of keys
14906 * "what" == 1: list of values
14907 * "what" == 2: list of items
14908 */
14909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014910dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014911{
Bram Moolenaar33570922005-01-25 22:26:29 +000014912 list_T *l2;
14913 dictitem_T *di;
14914 hashitem_T *hi;
14915 listitem_T *li;
14916 listitem_T *li2;
14917 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014918 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014919
Bram Moolenaar8c711452005-01-14 21:53:12 +000014920 if (argvars[0].v_type != VAR_DICT)
14921 {
14922 EMSG(_(e_dictreq));
14923 return;
14924 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014925 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014926 return;
14927
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014928 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014929 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014930
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014931 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014932 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014933 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014934 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014935 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014936 --todo;
14937 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014938
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014939 li = listitem_alloc();
14940 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014941 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014942 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014943
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014944 if (what == 0)
14945 {
14946 /* keys() */
14947 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014948 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014949 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14950 }
14951 else if (what == 1)
14952 {
14953 /* values() */
14954 copy_tv(&di->di_tv, &li->li_tv);
14955 }
14956 else
14957 {
14958 /* items() */
14959 l2 = list_alloc();
14960 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014961 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014962 li->li_tv.vval.v_list = l2;
14963 if (l2 == NULL)
14964 break;
14965 ++l2->lv_refcount;
14966
14967 li2 = listitem_alloc();
14968 if (li2 == NULL)
14969 break;
14970 list_append(l2, li2);
14971 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014972 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014973 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14974
14975 li2 = listitem_alloc();
14976 if (li2 == NULL)
14977 break;
14978 list_append(l2, li2);
14979 copy_tv(&di->di_tv, &li2->li_tv);
14980 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014981 }
14982 }
14983}
14984
14985/*
14986 * "items(dict)" function
14987 */
14988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014989f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014990{
14991 dict_list(argvars, rettv, 2);
14992}
14993
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014994#if defined(FEAT_JOB) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014995/*
14996 * Get the job from the argument.
14997 * Returns NULL if the job is invalid.
14998 */
14999 static job_T *
15000get_job_arg(typval_T *tv)
15001{
15002 job_T *job;
15003
15004 if (tv->v_type != VAR_JOB)
15005 {
15006 EMSG2(_(e_invarg2), get_tv_string(tv));
15007 return NULL;
15008 }
15009 job = tv->vval.v_job;
15010
15011 if (job == NULL)
15012 EMSG(_("E916: not a valid job"));
15013 return job;
15014}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015015
15016# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010015017/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015018 * "job_getchannel()" function
15019 */
15020 static void
15021f_job_getchannel(typval_T *argvars, typval_T *rettv)
15022{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015023 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015024
Bram Moolenaar65edff82016-02-21 16:40:11 +010015025 if (job != NULL)
15026 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015027 rettv->v_type = VAR_CHANNEL;
15028 rettv->vval.v_channel = job->jv_channel;
15029 if (job->jv_channel != NULL)
15030 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015031 }
15032}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015033# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015034
15035/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015036 * "job_setoptions()" function
15037 */
15038 static void
15039f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15040{
15041 job_T *job = get_job_arg(&argvars[0]);
15042 jobopt_T opt;
15043
15044 if (job == NULL)
15045 return;
15046 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015047 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015048 return;
15049 job_set_options(job, &opt);
15050}
15051
15052/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015053 * "job_start()" function
15054 */
15055 static void
15056f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
15057{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015058 job_T *job;
15059 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015060#if defined(UNIX)
15061# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015062 char **argv = NULL;
15063 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015064#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015065 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015066#endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015067 jobopt_T opt;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015068
15069 rettv->v_type = VAR_JOB;
15070 job = job_alloc();
15071 rettv->vval.v_job = job;
15072 if (job == NULL)
15073 return;
15074
15075 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015076
15077 /* Default mode is NL. */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015078 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015079 opt.jo_mode = MODE_NL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015080 if (get_job_options(&argvars[1], &opt,
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015081 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL
Bram Moolenaar187db502016-02-27 14:44:26 +010015082 + JO_STOPONEXIT + JO_EXIT_CB + JO_OUT_IO) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015083 return;
Bram Moolenaar65edff82016-02-21 16:40:11 +010015084 job_set_options(job, &opt);
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015085
Bram Moolenaar835dc632016-02-07 14:27:38 +010015086#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010015087 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015088#endif
15089
15090 if (argvars[0].v_type == VAR_STRING)
15091 {
15092 /* Command is a string. */
15093 cmd = argvars[0].vval.v_string;
15094#ifdef USE_ARGV
15095 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
15096 return;
15097 argv[argc] = NULL;
15098#endif
15099 }
15100 else if (argvars[0].v_type != VAR_LIST
15101 || argvars[0].vval.v_list == NULL
15102 || argvars[0].vval.v_list->lv_len < 1)
15103 {
15104 EMSG(_(e_invarg));
15105 return;
15106 }
15107 else
15108 {
15109 list_T *l = argvars[0].vval.v_list;
15110 listitem_T *li;
15111 char_u *s;
15112
15113#ifdef USE_ARGV
15114 /* Pass argv[] to mch_call_shell(). */
15115 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
15116 if (argv == NULL)
15117 return;
15118#endif
15119 for (li = l->lv_first; li != NULL; li = li->li_next)
15120 {
15121 s = get_tv_string_chk(&li->li_tv);
15122 if (s == NULL)
15123 goto theend;
15124#ifdef USE_ARGV
15125 argv[argc++] = (char *)s;
15126#else
15127 if (li != l->lv_first)
15128 {
15129 s = vim_strsave_shellescape(s, FALSE, TRUE);
15130 if (s == NULL)
15131 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010015132 ga_concat(&ga, s);
15133 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015134 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010015135 else
15136 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015137 if (li->li_next != NULL)
15138 ga_append(&ga, ' ');
15139#endif
15140 }
15141#ifdef USE_ARGV
15142 argv[argc] = NULL;
15143#else
15144 cmd = ga.ga_data;
15145#endif
15146 }
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015147
Bram Moolenaar835dc632016-02-07 14:27:38 +010015148#ifdef USE_ARGV
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015149# ifdef FEAT_CHANNEL
15150 if (ch_log_active())
15151 {
15152 garray_T ga;
15153 int i;
15154
15155 ga_init2(&ga, (int)sizeof(char), 200);
15156 for (i = 0; i < argc; ++i)
15157 {
15158 if (i > 0)
15159 ga_concat(&ga, (char_u *)" ");
15160 ga_concat(&ga, (char_u *)argv[i]);
15161 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015162 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015163 ga_clear(&ga);
15164 }
15165# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015166 mch_start_job(argv, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015167#else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015168# ifdef FEAT_CHANNEL
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015169 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015170# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015171 mch_start_job((char *)cmd, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015172#endif
15173
15174theend:
15175#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010015176 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015177#else
15178 vim_free(ga.ga_data);
15179#endif
15180}
15181
15182/*
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015183 * Get the status of "job" and invoke the exit callback when needed.
15184 * The returned string is not allocated.
15185 */
15186 static char *
15187job_status(job_T *job)
15188{
15189 char *result;
15190
15191 if (job->jv_status == JOB_ENDED)
15192 /* No need to check, dead is dead. */
15193 result = "dead";
15194 else if (job->jv_status == JOB_FAILED)
15195 result = "fail";
15196 else
15197 {
15198 result = mch_job_status(job);
15199# ifdef FEAT_CHANNEL
15200 if (job->jv_status == JOB_ENDED)
15201 ch_log(job->jv_channel, "Job ended");
15202# endif
15203 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
15204 {
15205 typval_T argv[3];
15206 typval_T rettv;
15207 int dummy;
15208
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015209 /* invoke the exit callback; make sure the refcount is > 0 */
15210 ++job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015211 argv[0].v_type = VAR_JOB;
15212 argv[0].vval.v_job = job;
15213 argv[1].v_type = VAR_NUMBER;
15214 argv[1].vval.v_number = job->jv_exitval;
15215 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
15216 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
15217 clear_tv(&rettv);
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015218 --job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015219 }
15220 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
15221 {
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015222 /* The job was already unreferenced, now that it ended it can be
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015223 * freed. Careful: caller must not use "job" after this! */
15224 job_free(job);
15225 }
15226 }
15227 return result;
15228}
15229
15230/*
15231 * Called once in a while: check if any jobs with an "exit-cb" have ended.
15232 */
15233 void
Bram Moolenaarb2bd6a02016-02-22 20:20:25 +010015234job_check_ended(void)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015235{
15236 static time_t last_check = 0;
15237 time_t now;
15238 job_T *job;
15239 job_T *next;
15240
15241 /* Only do this once in 10 seconds. */
15242 now = time(NULL);
15243 if (last_check + 10 < now)
15244 {
15245 last_check = now;
15246 for (job = first_job; job != NULL; job = next)
15247 {
15248 next = job->jv_next;
15249 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
15250 job_status(job); /* may free "job" */
15251 }
15252 }
15253}
15254
15255/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015256 * "job_status()" function
15257 */
15258 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015259f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015260{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015261 job_T *job = get_job_arg(&argvars[0]);
15262 char *result;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015263
Bram Moolenaar65edff82016-02-21 16:40:11 +010015264 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015265 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015266 result = job_status(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015267 rettv->v_type = VAR_STRING;
15268 rettv->vval.v_string = vim_strsave((char_u *)result);
15269 }
15270}
15271
15272/*
15273 * "job_stop()" function
15274 */
15275 static void
15276f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15277{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015278 job_T *job = get_job_arg(&argvars[0]);
15279
15280 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015281 {
15282 char_u *arg;
15283
15284 if (argvars[1].v_type == VAR_UNKNOWN)
15285 arg = (char_u *)"";
15286 else
15287 {
15288 arg = get_tv_string_chk(&argvars[1]);
15289 if (arg == NULL)
15290 {
15291 EMSG(_(e_invarg));
15292 return;
15293 }
15294 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010015295 if (mch_stop_job(job, arg) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015296 rettv->vval.v_number = 0;
15297 else
Bram Moolenaar46c85432016-02-26 11:17:46 +010015298 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015299 rettv->vval.v_number = 1;
Bram Moolenaar46c85432016-02-26 11:17:46 +010015300 /* Assume that "hup" does not kill the job. */
15301 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
15302 job->jv_channel->ch_job_killed = TRUE;
15303 }
15304 /* We don't try freeing the job, obviously the caller still has a
15305 * reference to it. */
Bram Moolenaar835dc632016-02-07 14:27:38 +010015306 }
15307}
15308#endif
15309
Bram Moolenaar071d4272004-06-13 20:20:40 +000015310/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015311 * "join()" function
15312 */
15313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015314f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015315{
15316 garray_T ga;
15317 char_u *sep;
15318
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015319 if (argvars[0].v_type != VAR_LIST)
15320 {
15321 EMSG(_(e_listreq));
15322 return;
15323 }
15324 if (argvars[0].vval.v_list == NULL)
15325 return;
15326 if (argvars[1].v_type == VAR_UNKNOWN)
15327 sep = (char_u *)" ";
15328 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015329 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015330
15331 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015332
15333 if (sep != NULL)
15334 {
15335 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015336 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015337 ga_append(&ga, NUL);
15338 rettv->vval.v_string = (char_u *)ga.ga_data;
15339 }
15340 else
15341 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015342}
15343
15344/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015345 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015346 */
15347 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015348f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015349{
15350 js_read_T reader;
15351
15352 reader.js_buf = get_tv_string(&argvars[0]);
15353 reader.js_fill = NULL;
15354 reader.js_used = 0;
15355 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15356 EMSG(_(e_invarg));
15357}
15358
15359/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015360 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015361 */
15362 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015363f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015364{
15365 rettv->v_type = VAR_STRING;
15366 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15367}
15368
15369/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015370 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015371 */
15372 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015373f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015374{
15375 js_read_T reader;
15376
15377 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015378 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015379 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015380 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015381 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015382}
15383
15384/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015385 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015386 */
15387 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015388f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015389{
15390 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015391 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015392}
15393
15394/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015395 * "keys()" function
15396 */
15397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015398f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015399{
15400 dict_list(argvars, rettv, 0);
15401}
15402
15403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015404 * "last_buffer_nr()" function.
15405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015407f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408{
15409 int n = 0;
15410 buf_T *buf;
15411
15412 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15413 if (n < buf->b_fnum)
15414 n = buf->b_fnum;
15415
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015416 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015417}
15418
15419/*
15420 * "len()" function
15421 */
15422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015423f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015424{
15425 switch (argvars[0].v_type)
15426 {
15427 case VAR_STRING:
15428 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015429 rettv->vval.v_number = (varnumber_T)STRLEN(
15430 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015431 break;
15432 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015433 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015434 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015435 case VAR_DICT:
15436 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15437 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015438 case VAR_UNKNOWN:
15439 case VAR_SPECIAL:
15440 case VAR_FLOAT:
15441 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015442 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015443 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015444 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015445 break;
15446 }
15447}
15448
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015449static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015450
15451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015452libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015453{
15454#ifdef FEAT_LIBCALL
15455 char_u *string_in;
15456 char_u **string_result;
15457 int nr_result;
15458#endif
15459
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015460 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015461 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015462 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015463
15464 if (check_restricted() || check_secure())
15465 return;
15466
15467#ifdef FEAT_LIBCALL
15468 /* The first two args must be strings, otherwise its meaningless */
15469 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15470 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015471 string_in = NULL;
15472 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015473 string_in = argvars[2].vval.v_string;
15474 if (type == VAR_NUMBER)
15475 string_result = NULL;
15476 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015477 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015478 if (mch_libcall(argvars[0].vval.v_string,
15479 argvars[1].vval.v_string,
15480 string_in,
15481 argvars[2].vval.v_number,
15482 string_result,
15483 &nr_result) == OK
15484 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015485 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015486 }
15487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015488}
15489
15490/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015491 * "libcall()" function
15492 */
15493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015494f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015495{
15496 libcall_common(argvars, rettv, VAR_STRING);
15497}
15498
15499/*
15500 * "libcallnr()" function
15501 */
15502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015503f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015504{
15505 libcall_common(argvars, rettv, VAR_NUMBER);
15506}
15507
15508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509 * "line(string)" function
15510 */
15511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015512f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513{
15514 linenr_T lnum = 0;
15515 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015516 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015517
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015518 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519 if (fp != NULL)
15520 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015521 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015522}
15523
15524/*
15525 * "line2byte(lnum)" function
15526 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015528f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529{
15530#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015531 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532#else
15533 linenr_T lnum;
15534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015535 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015536 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015537 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015539 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15540 if (rettv->vval.v_number >= 0)
15541 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542#endif
15543}
15544
15545/*
15546 * "lispindent(lnum)" function
15547 */
15548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015549f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015550{
15551#ifdef FEAT_LISP
15552 pos_T pos;
15553 linenr_T lnum;
15554
15555 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015556 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15558 {
15559 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015560 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561 curwin->w_cursor = pos;
15562 }
15563 else
15564#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015565 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566}
15567
15568/*
15569 * "localtime()" function
15570 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015572f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015573{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015574 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015575}
15576
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015577static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015578
15579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015580get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581{
15582 char_u *keys;
15583 char_u *which;
15584 char_u buf[NUMBUFLEN];
15585 char_u *keys_buf = NULL;
15586 char_u *rhs;
15587 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015588 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015589 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015590 mapblock_T *mp;
15591 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592
15593 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015594 rettv->v_type = VAR_STRING;
15595 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015596
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015597 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598 if (*keys == NUL)
15599 return;
15600
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015601 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015602 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015603 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015604 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015605 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015606 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015607 if (argvars[3].v_type != VAR_UNKNOWN)
15608 get_dict = get_tv_number(&argvars[3]);
15609 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015611 else
15612 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015613 if (which == NULL)
15614 return;
15615
Bram Moolenaar071d4272004-06-13 20:20:40 +000015616 mode = get_map_mode(&which, 0);
15617
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015618 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015619 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015621
15622 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015624 /* Return a string. */
15625 if (rhs != NULL)
15626 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627
Bram Moolenaarbd743252010-10-20 21:23:33 +020015628 }
15629 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15630 {
15631 /* Return a dictionary. */
15632 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15633 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15634 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015635
Bram Moolenaarbd743252010-10-20 21:23:33 +020015636 dict_add_nr_str(dict, "lhs", 0L, lhs);
15637 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15638 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15639 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15640 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15641 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15642 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015643 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015644 dict_add_nr_str(dict, "mode", 0L, mapmode);
15645
15646 vim_free(lhs);
15647 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648 }
15649}
15650
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015651#ifdef FEAT_FLOAT
15652/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015653 * "log()" function
15654 */
15655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015656f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015657{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015658 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015659
15660 rettv->v_type = VAR_FLOAT;
15661 if (get_float_arg(argvars, &f) == OK)
15662 rettv->vval.v_float = log(f);
15663 else
15664 rettv->vval.v_float = 0.0;
15665}
15666
15667/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015668 * "log10()" function
15669 */
15670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015671f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015672{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015673 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015674
15675 rettv->v_type = VAR_FLOAT;
15676 if (get_float_arg(argvars, &f) == OK)
15677 rettv->vval.v_float = log10(f);
15678 else
15679 rettv->vval.v_float = 0.0;
15680}
15681#endif
15682
Bram Moolenaar1dced572012-04-05 16:54:08 +020015683#ifdef FEAT_LUA
15684/*
15685 * "luaeval()" function
15686 */
15687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015688f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015689{
15690 char_u *str;
15691 char_u buf[NUMBUFLEN];
15692
15693 str = get_tv_string_buf(&argvars[0], buf);
15694 do_luaeval(str, argvars + 1, rettv);
15695}
15696#endif
15697
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015699 * "map()" function
15700 */
15701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015702f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015703{
15704 filter_map(argvars, rettv, TRUE);
15705}
15706
15707/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015708 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709 */
15710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015711f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015712{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015713 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015714}
15715
15716/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015717 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015718 */
15719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015720f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015722 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015723}
15724
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015725static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015726
15727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015728find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015729{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015730 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015731 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015732 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015733 char_u *pat;
15734 regmatch_T regmatch;
15735 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015736 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015737 char_u *save_cpo;
15738 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015739 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015740 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015741 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015742 list_T *l = NULL;
15743 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015744 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015745 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746
15747 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15748 save_cpo = p_cpo;
15749 p_cpo = (char_u *)"";
15750
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015751 rettv->vval.v_number = -1;
15752 if (type == 3)
15753 {
15754 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015755 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015756 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015757 }
15758 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015759 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015760 rettv->v_type = VAR_STRING;
15761 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015764 if (argvars[0].v_type == VAR_LIST)
15765 {
15766 if ((l = argvars[0].vval.v_list) == NULL)
15767 goto theend;
15768 li = l->lv_first;
15769 }
15770 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015771 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015772 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015773 len = (long)STRLEN(str);
15774 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015775
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015776 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15777 if (pat == NULL)
15778 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015779
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015780 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015781 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015782 int error = FALSE;
15783
15784 start = get_tv_number_chk(&argvars[2], &error);
15785 if (error)
15786 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015787 if (l != NULL)
15788 {
15789 li = list_find(l, start);
15790 if (li == NULL)
15791 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015792 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015793 }
15794 else
15795 {
15796 if (start < 0)
15797 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015798 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015799 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015800 /* When "count" argument is there ignore matches before "start",
15801 * otherwise skip part of the string. Differs when pattern is "^"
15802 * or "\<". */
15803 if (argvars[3].v_type != VAR_UNKNOWN)
15804 startcol = start;
15805 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015806 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015807 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015808 len -= start;
15809 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015810 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015811
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015812 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015813 nth = get_tv_number_chk(&argvars[3], &error);
15814 if (error)
15815 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816 }
15817
15818 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15819 if (regmatch.regprog != NULL)
15820 {
15821 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015822
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015823 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015824 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015825 if (l != NULL)
15826 {
15827 if (li == NULL)
15828 {
15829 match = FALSE;
15830 break;
15831 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015832 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015833 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015834 if (str == NULL)
15835 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015836 }
15837
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015838 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015839
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015840 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015841 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015842 if (l == NULL && !match)
15843 break;
15844
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015845 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015846 if (l != NULL)
15847 {
15848 li = li->li_next;
15849 ++idx;
15850 }
15851 else
15852 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015853#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015854 startcol = (colnr_T)(regmatch.startp[0]
15855 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015856#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015857 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015858#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015859 if (startcol > (colnr_T)len
15860 || str + startcol <= regmatch.startp[0])
15861 {
15862 match = FALSE;
15863 break;
15864 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015865 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015866 }
15867
15868 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015870 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015871 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015872 int i;
15873
15874 /* return list with matched string and submatches */
15875 for (i = 0; i < NSUBEXP; ++i)
15876 {
15877 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015878 {
15879 if (list_append_string(rettv->vval.v_list,
15880 (char_u *)"", 0) == FAIL)
15881 break;
15882 }
15883 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015884 regmatch.startp[i],
15885 (int)(regmatch.endp[i] - regmatch.startp[i]))
15886 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015887 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015888 }
15889 }
15890 else if (type == 2)
15891 {
15892 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015893 if (l != NULL)
15894 copy_tv(&li->li_tv, rettv);
15895 else
15896 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015897 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015898 }
15899 else if (l != NULL)
15900 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015901 else
15902 {
15903 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015904 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015905 (varnumber_T)(regmatch.startp[0] - str);
15906 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015907 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015908 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015909 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015910 }
15911 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015912 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015913 }
15914
15915theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015916 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015917 p_cpo = save_cpo;
15918}
15919
15920/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015921 * "match()" function
15922 */
15923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015924f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015925{
15926 find_some_match(argvars, rettv, 1);
15927}
15928
15929/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015930 * "matchadd()" function
15931 */
15932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015933f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015934{
15935#ifdef FEAT_SEARCH_EXTRA
15936 char_u buf[NUMBUFLEN];
15937 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15938 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15939 int prio = 10; /* default priority */
15940 int id = -1;
15941 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015942 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015943
15944 rettv->vval.v_number = -1;
15945
15946 if (grp == NULL || pat == NULL)
15947 return;
15948 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015949 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015950 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015951 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015952 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015953 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015954 if (argvars[4].v_type != VAR_UNKNOWN)
15955 {
15956 if (argvars[4].v_type != VAR_DICT)
15957 {
15958 EMSG(_(e_dictreq));
15959 return;
15960 }
15961 if (dict_find(argvars[4].vval.v_dict,
15962 (char_u *)"conceal", -1) != NULL)
15963 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15964 (char_u *)"conceal", FALSE);
15965 }
15966 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015967 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015968 if (error == TRUE)
15969 return;
15970 if (id >= 1 && id <= 3)
15971 {
15972 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15973 return;
15974 }
15975
Bram Moolenaar6561d522015-07-21 15:48:27 +020015976 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15977 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015978#endif
15979}
15980
15981/*
15982 * "matchaddpos()" function
15983 */
15984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015985f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015986{
15987#ifdef FEAT_SEARCH_EXTRA
15988 char_u buf[NUMBUFLEN];
15989 char_u *group;
15990 int prio = 10;
15991 int id = -1;
15992 int error = FALSE;
15993 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015994 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015995
15996 rettv->vval.v_number = -1;
15997
15998 group = get_tv_string_buf_chk(&argvars[0], buf);
15999 if (group == NULL)
16000 return;
16001
16002 if (argvars[1].v_type != VAR_LIST)
16003 {
16004 EMSG2(_(e_listarg), "matchaddpos()");
16005 return;
16006 }
16007 l = argvars[1].vval.v_list;
16008 if (l == NULL)
16009 return;
16010
16011 if (argvars[2].v_type != VAR_UNKNOWN)
16012 {
16013 prio = get_tv_number_chk(&argvars[2], &error);
16014 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016015 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020016016 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016017 if (argvars[4].v_type != VAR_UNKNOWN)
16018 {
16019 if (argvars[4].v_type != VAR_DICT)
16020 {
16021 EMSG(_(e_dictreq));
16022 return;
16023 }
16024 if (dict_find(argvars[4].vval.v_dict,
16025 (char_u *)"conceal", -1) != NULL)
16026 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16027 (char_u *)"conceal", FALSE);
16028 }
16029 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016030 }
16031 if (error == TRUE)
16032 return;
16033
16034 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16035 if (id == 1 || id == 2)
16036 {
16037 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16038 return;
16039 }
16040
Bram Moolenaar6561d522015-07-21 15:48:27 +020016041 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16042 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016043#endif
16044}
16045
16046/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016047 * "matcharg()" function
16048 */
16049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016050f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016051{
16052 if (rettv_list_alloc(rettv) == OK)
16053 {
16054#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016055 int id = get_tv_number(&argvars[0]);
16056 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016057
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016058 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016059 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016060 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16061 {
16062 list_append_string(rettv->vval.v_list,
16063 syn_id2name(m->hlg_id), -1);
16064 list_append_string(rettv->vval.v_list, m->pattern, -1);
16065 }
16066 else
16067 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016068 list_append_string(rettv->vval.v_list, NULL, -1);
16069 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016070 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016071 }
16072#endif
16073 }
16074}
16075
16076/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016077 * "matchdelete()" function
16078 */
16079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016080f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016081{
16082#ifdef FEAT_SEARCH_EXTRA
16083 rettv->vval.v_number = match_delete(curwin,
16084 (int)get_tv_number(&argvars[0]), TRUE);
16085#endif
16086}
16087
16088/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016089 * "matchend()" function
16090 */
16091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016092f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016093{
16094 find_some_match(argvars, rettv, 0);
16095}
16096
16097/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016098 * "matchlist()" function
16099 */
16100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016101f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016102{
16103 find_some_match(argvars, rettv, 3);
16104}
16105
16106/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016107 * "matchstr()" function
16108 */
16109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016110f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016111{
16112 find_some_match(argvars, rettv, 2);
16113}
16114
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016115static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016116
16117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016118max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016119{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016120 long n = 0;
16121 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016122 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016123
16124 if (argvars[0].v_type == VAR_LIST)
16125 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016126 list_T *l;
16127 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016128
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016129 l = argvars[0].vval.v_list;
16130 if (l != NULL)
16131 {
16132 li = l->lv_first;
16133 if (li != NULL)
16134 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016135 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016136 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016137 {
16138 li = li->li_next;
16139 if (li == NULL)
16140 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016141 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016142 if (domax ? i > n : i < n)
16143 n = i;
16144 }
16145 }
16146 }
16147 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016148 else if (argvars[0].v_type == VAR_DICT)
16149 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016150 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016151 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016152 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016153 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016154
16155 d = argvars[0].vval.v_dict;
16156 if (d != NULL)
16157 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016158 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016159 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016160 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016161 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016162 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016163 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016164 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016165 if (first)
16166 {
16167 n = i;
16168 first = FALSE;
16169 }
16170 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016171 n = i;
16172 }
16173 }
16174 }
16175 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016176 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016177 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016178 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016179}
16180
16181/*
16182 * "max()" function
16183 */
16184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016185f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016186{
16187 max_min(argvars, rettv, TRUE);
16188}
16189
16190/*
16191 * "min()" function
16192 */
16193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016194f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016195{
16196 max_min(argvars, rettv, FALSE);
16197}
16198
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016199static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016200
16201/*
16202 * Create the directory in which "dir" is located, and higher levels when
16203 * needed.
16204 */
16205 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016206mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016207{
16208 char_u *p;
16209 char_u *updir;
16210 int r = FAIL;
16211
16212 /* Get end of directory name in "dir".
16213 * We're done when it's "/" or "c:/". */
16214 p = gettail_sep(dir);
16215 if (p <= get_past_head(dir))
16216 return OK;
16217
16218 /* If the directory exists we're done. Otherwise: create it.*/
16219 updir = vim_strnsave(dir, (int)(p - dir));
16220 if (updir == NULL)
16221 return FAIL;
16222 if (mch_isdir(updir))
16223 r = OK;
16224 else if (mkdir_recurse(updir, prot) == OK)
16225 r = vim_mkdir_emsg(updir, prot);
16226 vim_free(updir);
16227 return r;
16228}
16229
16230#ifdef vim_mkdir
16231/*
16232 * "mkdir()" function
16233 */
16234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016235f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016236{
16237 char_u *dir;
16238 char_u buf[NUMBUFLEN];
16239 int prot = 0755;
16240
16241 rettv->vval.v_number = FAIL;
16242 if (check_restricted() || check_secure())
16243 return;
16244
16245 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016246 if (*dir == NUL)
16247 rettv->vval.v_number = FAIL;
16248 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016249 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016250 if (*gettail(dir) == NUL)
16251 /* remove trailing slashes */
16252 *gettail_sep(dir) = NUL;
16253
16254 if (argvars[1].v_type != VAR_UNKNOWN)
16255 {
16256 if (argvars[2].v_type != VAR_UNKNOWN)
16257 prot = get_tv_number_chk(&argvars[2], NULL);
16258 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16259 mkdir_recurse(dir, prot);
16260 }
16261 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016262 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016263}
16264#endif
16265
Bram Moolenaar0d660222005-01-07 21:51:51 +000016266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016267 * "mode()" function
16268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016270f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016272 char_u buf[3];
16273
16274 buf[1] = NUL;
16275 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276
Bram Moolenaar071d4272004-06-13 20:20:40 +000016277 if (VIsual_active)
16278 {
16279 if (VIsual_select)
16280 buf[0] = VIsual_mode + 's' - 'v';
16281 else
16282 buf[0] = VIsual_mode;
16283 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016284 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016285 || State == CONFIRM)
16286 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016288 if (State == ASKMORE)
16289 buf[1] = 'm';
16290 else if (State == CONFIRM)
16291 buf[1] = '?';
16292 }
16293 else if (State == EXTERNCMD)
16294 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016295 else if (State & INSERT)
16296 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016297#ifdef FEAT_VREPLACE
16298 if (State & VREPLACE_FLAG)
16299 {
16300 buf[0] = 'R';
16301 buf[1] = 'v';
16302 }
16303 else
16304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305 if (State & REPLACE_FLAG)
16306 buf[0] = 'R';
16307 else
16308 buf[0] = 'i';
16309 }
16310 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016311 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016313 if (exmode_active)
16314 buf[1] = 'v';
16315 }
16316 else if (exmode_active)
16317 {
16318 buf[0] = 'c';
16319 buf[1] = 'e';
16320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016322 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016323 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016324 if (finish_op)
16325 buf[1] = 'o';
16326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016327
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016328 /* Clear out the minor mode when the argument is not a non-zero number or
16329 * non-empty string. */
16330 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016331 buf[1] = NUL;
16332
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016333 rettv->vval.v_string = vim_strsave(buf);
16334 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016335}
16336
Bram Moolenaar429fa852013-04-15 12:27:36 +020016337#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016338/*
16339 * "mzeval()" function
16340 */
16341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016342f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016343{
16344 char_u *str;
16345 char_u buf[NUMBUFLEN];
16346
16347 str = get_tv_string_buf(&argvars[0], buf);
16348 do_mzeval(str, rettv);
16349}
Bram Moolenaar75676462013-01-30 14:55:42 +010016350
16351 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016352mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016353{
16354 typval_T argvars[3];
16355
16356 argvars[0].v_type = VAR_STRING;
16357 argvars[0].vval.v_string = name;
16358 copy_tv(args, &argvars[1]);
16359 argvars[2].v_type = VAR_UNKNOWN;
16360 f_call(argvars, rettv);
16361 clear_tv(&argvars[1]);
16362}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016363#endif
16364
Bram Moolenaar071d4272004-06-13 20:20:40 +000016365/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016366 * "nextnonblank()" function
16367 */
16368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016369f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016370{
16371 linenr_T lnum;
16372
16373 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16374 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016375 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016376 {
16377 lnum = 0;
16378 break;
16379 }
16380 if (*skipwhite(ml_get(lnum)) != NUL)
16381 break;
16382 }
16383 rettv->vval.v_number = lnum;
16384}
16385
16386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016387 * "nr2char()" function
16388 */
16389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016390f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016391{
16392 char_u buf[NUMBUFLEN];
16393
16394#ifdef FEAT_MBYTE
16395 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016396 {
16397 int utf8 = 0;
16398
16399 if (argvars[1].v_type != VAR_UNKNOWN)
16400 utf8 = get_tv_number_chk(&argvars[1], NULL);
16401 if (utf8)
16402 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16403 else
16404 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016406 else
16407#endif
16408 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016409 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410 buf[1] = NUL;
16411 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016412 rettv->v_type = VAR_STRING;
16413 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016414}
16415
16416/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016417 * "or(expr, expr)" function
16418 */
16419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016420f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016421{
16422 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16423 | get_tv_number_chk(&argvars[1], NULL);
16424}
16425
16426/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016427 * "pathshorten()" function
16428 */
16429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016430f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016431{
16432 char_u *p;
16433
16434 rettv->v_type = VAR_STRING;
16435 p = get_tv_string_chk(&argvars[0]);
16436 if (p == NULL)
16437 rettv->vval.v_string = NULL;
16438 else
16439 {
16440 p = vim_strsave(p);
16441 rettv->vval.v_string = p;
16442 if (p != NULL)
16443 shorten_dir(p);
16444 }
16445}
16446
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016447#ifdef FEAT_PERL
16448/*
16449 * "perleval()" function
16450 */
16451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016452f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016453{
16454 char_u *str;
16455 char_u buf[NUMBUFLEN];
16456
16457 str = get_tv_string_buf(&argvars[0], buf);
16458 do_perleval(str, rettv);
16459}
16460#endif
16461
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016462#ifdef FEAT_FLOAT
16463/*
16464 * "pow()" function
16465 */
16466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016467f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016468{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016469 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016470
16471 rettv->v_type = VAR_FLOAT;
16472 if (get_float_arg(argvars, &fx) == OK
16473 && get_float_arg(&argvars[1], &fy) == OK)
16474 rettv->vval.v_float = pow(fx, fy);
16475 else
16476 rettv->vval.v_float = 0.0;
16477}
16478#endif
16479
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016480/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016481 * "prevnonblank()" function
16482 */
16483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016484f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016485{
16486 linenr_T lnum;
16487
16488 lnum = get_tv_lnum(argvars);
16489 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16490 lnum = 0;
16491 else
16492 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16493 --lnum;
16494 rettv->vval.v_number = lnum;
16495}
16496
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016497/* This dummy va_list is here because:
16498 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16499 * - locally in the function results in a "used before set" warning
16500 * - using va_start() to initialize it gives "function with fixed args" error */
16501static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016502
Bram Moolenaar8c711452005-01-14 21:53:12 +000016503/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016504 * "printf()" function
16505 */
16506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016507f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016508{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016509 char_u buf[NUMBUFLEN];
16510 int len;
16511 char_u *s;
16512 int saved_did_emsg = did_emsg;
16513 char *fmt;
16514
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016515 rettv->v_type = VAR_STRING;
16516 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016517
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016518 /* Get the required length, allocate the buffer and do it for real. */
16519 did_emsg = FALSE;
16520 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16521 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16522 if (!did_emsg)
16523 {
16524 s = alloc(len + 1);
16525 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016526 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016527 rettv->vval.v_string = s;
16528 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016529 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016530 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016531 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016532}
16533
16534/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016535 * "pumvisible()" function
16536 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016538f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016539{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016540#ifdef FEAT_INS_EXPAND
16541 if (pum_visible())
16542 rettv->vval.v_number = 1;
16543#endif
16544}
16545
Bram Moolenaardb913952012-06-29 12:54:53 +020016546#ifdef FEAT_PYTHON3
16547/*
16548 * "py3eval()" function
16549 */
16550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016551f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016552{
16553 char_u *str;
16554 char_u buf[NUMBUFLEN];
16555
16556 str = get_tv_string_buf(&argvars[0], buf);
16557 do_py3eval(str, rettv);
16558}
16559#endif
16560
16561#ifdef FEAT_PYTHON
16562/*
16563 * "pyeval()" function
16564 */
16565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016566f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016567{
16568 char_u *str;
16569 char_u buf[NUMBUFLEN];
16570
16571 str = get_tv_string_buf(&argvars[0], buf);
16572 do_pyeval(str, rettv);
16573}
16574#endif
16575
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016576/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016577 * "range()" function
16578 */
16579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016580f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016581{
16582 long start;
16583 long end;
16584 long stride = 1;
16585 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016586 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016587
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016588 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016589 if (argvars[1].v_type == VAR_UNKNOWN)
16590 {
16591 end = start - 1;
16592 start = 0;
16593 }
16594 else
16595 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016596 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016597 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016598 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016599 }
16600
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016601 if (error)
16602 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016603 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016604 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016605 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016606 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016607 else
16608 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016609 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016610 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016611 if (list_append_number(rettv->vval.v_list,
16612 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016613 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016614 }
16615}
16616
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016617/*
16618 * "readfile()" function
16619 */
16620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016621f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016622{
16623 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016624 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016625 char_u *fname;
16626 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016627 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16628 int io_size = sizeof(buf);
16629 int readlen; /* size of last fread() */
16630 char_u *prev = NULL; /* previously read bytes, if any */
16631 long prevlen = 0; /* length of data in prev */
16632 long prevsize = 0; /* size of prev buffer */
16633 long maxline = MAXLNUM;
16634 long cnt = 0;
16635 char_u *p; /* position in buf */
16636 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016637
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016638 if (argvars[1].v_type != VAR_UNKNOWN)
16639 {
16640 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16641 binary = TRUE;
16642 if (argvars[2].v_type != VAR_UNKNOWN)
16643 maxline = get_tv_number(&argvars[2]);
16644 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016645
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016646 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016647 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016648
16649 /* Always open the file in binary mode, library functions have a mind of
16650 * their own about CR-LF conversion. */
16651 fname = get_tv_string(&argvars[0]);
16652 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16653 {
16654 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16655 return;
16656 }
16657
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016658 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016659 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016660 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016661
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016662 /* This for loop processes what was read, but is also entered at end
16663 * of file so that either:
16664 * - an incomplete line gets written
16665 * - a "binary" file gets an empty line at the end if it ends in a
16666 * newline. */
16667 for (p = buf, start = buf;
16668 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16669 ++p)
16670 {
16671 if (*p == '\n' || readlen <= 0)
16672 {
16673 listitem_T *li;
16674 char_u *s = NULL;
16675 long_u len = p - start;
16676
16677 /* Finished a line. Remove CRs before NL. */
16678 if (readlen > 0 && !binary)
16679 {
16680 while (len > 0 && start[len - 1] == '\r')
16681 --len;
16682 /* removal may cross back to the "prev" string */
16683 if (len == 0)
16684 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16685 --prevlen;
16686 }
16687 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016688 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016689 else
16690 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016691 /* Change "prev" buffer to be the right size. This way
16692 * the bytes are only copied once, and very long lines are
16693 * allocated only once. */
16694 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016695 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016696 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016697 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016698 prev = NULL; /* the list will own the string */
16699 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016700 }
16701 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016702 if (s == NULL)
16703 {
16704 do_outofmem_msg((long_u) prevlen + len + 1);
16705 failed = TRUE;
16706 break;
16707 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016708
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016709 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016710 {
16711 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016712 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016713 break;
16714 }
16715 li->li_tv.v_type = VAR_STRING;
16716 li->li_tv.v_lock = 0;
16717 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016718 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016719
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016720 start = p + 1; /* step over newline */
16721 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016722 break;
16723 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016724 else if (*p == NUL)
16725 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016726#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016727 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16728 * when finding the BF and check the previous two bytes. */
16729 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016730 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016731 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16732 * + 1, these may be in the "prev" string. */
16733 char_u back1 = p >= buf + 1 ? p[-1]
16734 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16735 char_u back2 = p >= buf + 2 ? p[-2]
16736 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16737 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016738
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016739 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016740 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016741 char_u *dest = p - 2;
16742
16743 /* Usually a BOM is at the beginning of a file, and so at
16744 * the beginning of a line; then we can just step over it.
16745 */
16746 if (start == dest)
16747 start = p + 1;
16748 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016749 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016750 /* have to shuffle buf to close gap */
16751 int adjust_prevlen = 0;
16752
16753 if (dest < buf)
16754 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016755 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016756 dest = buf;
16757 }
16758 if (readlen > p - buf + 1)
16759 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16760 readlen -= 3 - adjust_prevlen;
16761 prevlen -= adjust_prevlen;
16762 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016763 }
16764 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016765 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016766#endif
16767 } /* for */
16768
16769 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16770 break;
16771 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016772 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016773 /* There's part of a line in buf, store it in "prev". */
16774 if (p - start + prevlen >= prevsize)
16775 {
16776 /* need bigger "prev" buffer */
16777 char_u *newprev;
16778
16779 /* A common use case is ordinary text files and "prev" gets a
16780 * fragment of a line, so the first allocation is made
16781 * small, to avoid repeatedly 'allocing' large and
16782 * 'reallocing' small. */
16783 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016784 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016785 else
16786 {
16787 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016788 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016789 prevsize = grow50pc > growmin ? grow50pc : growmin;
16790 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016791 newprev = prev == NULL ? alloc(prevsize)
16792 : vim_realloc(prev, prevsize);
16793 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016794 {
16795 do_outofmem_msg((long_u)prevsize);
16796 failed = TRUE;
16797 break;
16798 }
16799 prev = newprev;
16800 }
16801 /* Add the line part to end of "prev". */
16802 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016803 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016804 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016805 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016806
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016807 /*
16808 * For a negative line count use only the lines at the end of the file,
16809 * free the rest.
16810 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016811 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016812 while (cnt > -maxline)
16813 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016814 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016815 --cnt;
16816 }
16817
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016818 if (failed)
16819 {
16820 list_free(rettv->vval.v_list, TRUE);
16821 /* readfile doc says an empty list is returned on error */
16822 rettv->vval.v_list = list_alloc();
16823 }
16824
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016825 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016826 fclose(fd);
16827}
16828
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016829#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016830static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016831
16832/*
16833 * Convert a List to proftime_T.
16834 * Return FAIL when there is something wrong.
16835 */
16836 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016837list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016838{
16839 long n1, n2;
16840 int error = FALSE;
16841
16842 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16843 || arg->vval.v_list->lv_len != 2)
16844 return FAIL;
16845 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16846 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16847# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016848 tm->HighPart = n1;
16849 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016850# else
16851 tm->tv_sec = n1;
16852 tm->tv_usec = n2;
16853# endif
16854 return error ? FAIL : OK;
16855}
16856#endif /* FEAT_RELTIME */
16857
16858/*
16859 * "reltime()" function
16860 */
16861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016862f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016863{
16864#ifdef FEAT_RELTIME
16865 proftime_T res;
16866 proftime_T start;
16867
16868 if (argvars[0].v_type == VAR_UNKNOWN)
16869 {
16870 /* No arguments: get current time. */
16871 profile_start(&res);
16872 }
16873 else if (argvars[1].v_type == VAR_UNKNOWN)
16874 {
16875 if (list2proftime(&argvars[0], &res) == FAIL)
16876 return;
16877 profile_end(&res);
16878 }
16879 else
16880 {
16881 /* Two arguments: compute the difference. */
16882 if (list2proftime(&argvars[0], &start) == FAIL
16883 || list2proftime(&argvars[1], &res) == FAIL)
16884 return;
16885 profile_sub(&res, &start);
16886 }
16887
16888 if (rettv_list_alloc(rettv) == OK)
16889 {
16890 long n1, n2;
16891
16892# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016893 n1 = res.HighPart;
16894 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016895# else
16896 n1 = res.tv_sec;
16897 n2 = res.tv_usec;
16898# endif
16899 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16900 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16901 }
16902#endif
16903}
16904
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016905#ifdef FEAT_FLOAT
16906/*
16907 * "reltimefloat()" function
16908 */
16909 static void
16910f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16911{
16912# ifdef FEAT_RELTIME
16913 proftime_T tm;
16914# endif
16915
16916 rettv->v_type = VAR_FLOAT;
16917 rettv->vval.v_float = 0;
16918# ifdef FEAT_RELTIME
16919 if (list2proftime(&argvars[0], &tm) == OK)
16920 rettv->vval.v_float = profile_float(&tm);
16921# endif
16922}
16923#endif
16924
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016925/*
16926 * "reltimestr()" function
16927 */
16928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016929f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016930{
16931#ifdef FEAT_RELTIME
16932 proftime_T tm;
16933#endif
16934
16935 rettv->v_type = VAR_STRING;
16936 rettv->vval.v_string = NULL;
16937#ifdef FEAT_RELTIME
16938 if (list2proftime(&argvars[0], &tm) == OK)
16939 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16940#endif
16941}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016942
Bram Moolenaar0d660222005-01-07 21:51:51 +000016943#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016944static void make_connection(void);
16945static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016946
16947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016948make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016949{
16950 if (X_DISPLAY == NULL
16951# ifdef FEAT_GUI
16952 && !gui.in_use
16953# endif
16954 )
16955 {
16956 x_force_connect = TRUE;
16957 setup_term_clip();
16958 x_force_connect = FALSE;
16959 }
16960}
16961
16962 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016963check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016964{
16965 make_connection();
16966 if (X_DISPLAY == NULL)
16967 {
16968 EMSG(_("E240: No connection to Vim server"));
16969 return FAIL;
16970 }
16971 return OK;
16972}
16973#endif
16974
16975#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016976static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016977
16978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016979remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016980{
16981 char_u *server_name;
16982 char_u *keys;
16983 char_u *r = NULL;
16984 char_u buf[NUMBUFLEN];
16985# ifdef WIN32
16986 HWND w;
16987# else
16988 Window w;
16989# endif
16990
16991 if (check_restricted() || check_secure())
16992 return;
16993
16994# ifdef FEAT_X11
16995 if (check_connection() == FAIL)
16996 return;
16997# endif
16998
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016999 server_name = get_tv_string_chk(&argvars[0]);
17000 if (server_name == NULL)
17001 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017002 keys = get_tv_string_buf(&argvars[1], buf);
17003# ifdef WIN32
17004 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17005# else
17006 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17007 < 0)
17008# endif
17009 {
17010 if (r != NULL)
17011 EMSG(r); /* sending worked but evaluation failed */
17012 else
17013 EMSG2(_("E241: Unable to send to %s"), server_name);
17014 return;
17015 }
17016
17017 rettv->vval.v_string = r;
17018
17019 if (argvars[2].v_type != VAR_UNKNOWN)
17020 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017021 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017022 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017023 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017024
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017025 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017026 v.di_tv.v_type = VAR_STRING;
17027 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017028 idvar = get_tv_string_chk(&argvars[2]);
17029 if (idvar != NULL)
17030 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017031 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017032 }
17033}
17034#endif
17035
17036/*
17037 * "remote_expr()" function
17038 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017040f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017041{
17042 rettv->v_type = VAR_STRING;
17043 rettv->vval.v_string = NULL;
17044#ifdef FEAT_CLIENTSERVER
17045 remote_common(argvars, rettv, TRUE);
17046#endif
17047}
17048
17049/*
17050 * "remote_foreground()" function
17051 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017053f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017054{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017055#ifdef FEAT_CLIENTSERVER
17056# ifdef WIN32
17057 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017058 {
17059 char_u *server_name = get_tv_string_chk(&argvars[0]);
17060
17061 if (server_name != NULL)
17062 serverForeground(server_name);
17063 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017064# else
17065 /* Send a foreground() expression to the server. */
17066 argvars[1].v_type = VAR_STRING;
17067 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17068 argvars[2].v_type = VAR_UNKNOWN;
17069 remote_common(argvars, rettv, TRUE);
17070 vim_free(argvars[1].vval.v_string);
17071# endif
17072#endif
17073}
17074
Bram Moolenaar0d660222005-01-07 21:51:51 +000017075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017076f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017077{
17078#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017079 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017080 char_u *s = NULL;
17081# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017082 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017083# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017084 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017085
17086 if (check_restricted() || check_secure())
17087 {
17088 rettv->vval.v_number = -1;
17089 return;
17090 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017091 serverid = get_tv_string_chk(&argvars[0]);
17092 if (serverid == NULL)
17093 {
17094 rettv->vval.v_number = -1;
17095 return; /* type error; errmsg already given */
17096 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017097# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017098 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017099 if (n == 0)
17100 rettv->vval.v_number = -1;
17101 else
17102 {
17103 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17104 rettv->vval.v_number = (s != NULL);
17105 }
17106# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017107 if (check_connection() == FAIL)
17108 return;
17109
17110 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017111 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017112# endif
17113
17114 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17115 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017116 char_u *retvar;
17117
Bram Moolenaar33570922005-01-25 22:26:29 +000017118 v.di_tv.v_type = VAR_STRING;
17119 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017120 retvar = get_tv_string_chk(&argvars[1]);
17121 if (retvar != NULL)
17122 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017123 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017124 }
17125#else
17126 rettv->vval.v_number = -1;
17127#endif
17128}
17129
Bram Moolenaar0d660222005-01-07 21:51:51 +000017130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017131f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017132{
17133 char_u *r = NULL;
17134
17135#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017136 char_u *serverid = get_tv_string_chk(&argvars[0]);
17137
17138 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017139 {
17140# ifdef WIN32
17141 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017142 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017143
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017144 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017145 if (n != 0)
17146 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17147 if (r == NULL)
17148# else
17149 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017150 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017151# endif
17152 EMSG(_("E277: Unable to read a server reply"));
17153 }
17154#endif
17155 rettv->v_type = VAR_STRING;
17156 rettv->vval.v_string = r;
17157}
17158
17159/*
17160 * "remote_send()" function
17161 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017163f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017164{
17165 rettv->v_type = VAR_STRING;
17166 rettv->vval.v_string = NULL;
17167#ifdef FEAT_CLIENTSERVER
17168 remote_common(argvars, rettv, FALSE);
17169#endif
17170}
17171
17172/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017173 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017174 */
17175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017176f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017177{
Bram Moolenaar33570922005-01-25 22:26:29 +000017178 list_T *l;
17179 listitem_T *item, *item2;
17180 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017181 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017182 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017183 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017184 dict_T *d;
17185 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017186 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017187
Bram Moolenaar8c711452005-01-14 21:53:12 +000017188 if (argvars[0].v_type == VAR_DICT)
17189 {
17190 if (argvars[2].v_type != VAR_UNKNOWN)
17191 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017192 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017193 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017194 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017195 key = get_tv_string_chk(&argvars[1]);
17196 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017197 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017198 di = dict_find(d, key, -1);
17199 if (di == NULL)
17200 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017201 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17202 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017203 {
17204 *rettv = di->di_tv;
17205 init_tv(&di->di_tv);
17206 dictitem_remove(d, di);
17207 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017208 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017209 }
17210 }
17211 else if (argvars[0].v_type != VAR_LIST)
17212 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017213 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017214 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017215 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017216 int error = FALSE;
17217
17218 idx = get_tv_number_chk(&argvars[1], &error);
17219 if (error)
17220 ; /* type error: do nothing, errmsg already given */
17221 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017222 EMSGN(_(e_listidx), idx);
17223 else
17224 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017225 if (argvars[2].v_type == VAR_UNKNOWN)
17226 {
17227 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017228 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017229 *rettv = item->li_tv;
17230 vim_free(item);
17231 }
17232 else
17233 {
17234 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017235 end = get_tv_number_chk(&argvars[2], &error);
17236 if (error)
17237 ; /* type error: do nothing */
17238 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017239 EMSGN(_(e_listidx), end);
17240 else
17241 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017242 int cnt = 0;
17243
17244 for (li = item; li != NULL; li = li->li_next)
17245 {
17246 ++cnt;
17247 if (li == item2)
17248 break;
17249 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017250 if (li == NULL) /* didn't find "item2" after "item" */
17251 EMSG(_(e_invrange));
17252 else
17253 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017254 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017255 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017256 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017257 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017258 l->lv_first = item;
17259 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017260 item->li_prev = NULL;
17261 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017262 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017263 }
17264 }
17265 }
17266 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017267 }
17268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269}
17270
17271/*
17272 * "rename({from}, {to})" function
17273 */
17274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017275f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276{
17277 char_u buf[NUMBUFLEN];
17278
17279 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017280 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017281 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017282 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17283 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017284}
17285
17286/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017287 * "repeat()" function
17288 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017289 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017290f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017291{
17292 char_u *p;
17293 int n;
17294 int slen;
17295 int len;
17296 char_u *r;
17297 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017298
17299 n = get_tv_number(&argvars[1]);
17300 if (argvars[0].v_type == VAR_LIST)
17301 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017302 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017303 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017304 if (list_extend(rettv->vval.v_list,
17305 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017306 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017307 }
17308 else
17309 {
17310 p = get_tv_string(&argvars[0]);
17311 rettv->v_type = VAR_STRING;
17312 rettv->vval.v_string = NULL;
17313
17314 slen = (int)STRLEN(p);
17315 len = slen * n;
17316 if (len <= 0)
17317 return;
17318
17319 r = alloc(len + 1);
17320 if (r != NULL)
17321 {
17322 for (i = 0; i < n; i++)
17323 mch_memmove(r + i * slen, p, (size_t)slen);
17324 r[len] = NUL;
17325 }
17326
17327 rettv->vval.v_string = r;
17328 }
17329}
17330
17331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332 * "resolve()" function
17333 */
17334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017335f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017336{
17337 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017338#ifdef HAVE_READLINK
17339 char_u *buf = NULL;
17340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017341
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017342 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017343#ifdef FEAT_SHORTCUT
17344 {
17345 char_u *v = NULL;
17346
17347 v = mch_resolve_shortcut(p);
17348 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017349 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017351 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352 }
17353#else
17354# ifdef HAVE_READLINK
17355 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356 char_u *cpy;
17357 int len;
17358 char_u *remain = NULL;
17359 char_u *q;
17360 int is_relative_to_current = FALSE;
17361 int has_trailing_pathsep = FALSE;
17362 int limit = 100;
17363
17364 p = vim_strsave(p);
17365
17366 if (p[0] == '.' && (vim_ispathsep(p[1])
17367 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17368 is_relative_to_current = TRUE;
17369
17370 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017371 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017372 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017373 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017374 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376
17377 q = getnextcomp(p);
17378 if (*q != NUL)
17379 {
17380 /* Separate the first path component in "p", and keep the
17381 * remainder (beginning with the path separator). */
17382 remain = vim_strsave(q - 1);
17383 q[-1] = NUL;
17384 }
17385
Bram Moolenaard9462e32011-04-11 21:35:11 +020017386 buf = alloc(MAXPATHL + 1);
17387 if (buf == NULL)
17388 goto fail;
17389
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390 for (;;)
17391 {
17392 for (;;)
17393 {
17394 len = readlink((char *)p, (char *)buf, MAXPATHL);
17395 if (len <= 0)
17396 break;
17397 buf[len] = NUL;
17398
17399 if (limit-- == 0)
17400 {
17401 vim_free(p);
17402 vim_free(remain);
17403 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017404 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405 goto fail;
17406 }
17407
17408 /* Ensure that the result will have a trailing path separator
17409 * if the argument has one. */
17410 if (remain == NULL && has_trailing_pathsep)
17411 add_pathsep(buf);
17412
17413 /* Separate the first path component in the link value and
17414 * concatenate the remainders. */
17415 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17416 if (*q != NUL)
17417 {
17418 if (remain == NULL)
17419 remain = vim_strsave(q - 1);
17420 else
17421 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017422 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423 if (cpy != NULL)
17424 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017425 vim_free(remain);
17426 remain = cpy;
17427 }
17428 }
17429 q[-1] = NUL;
17430 }
17431
17432 q = gettail(p);
17433 if (q > p && *q == NUL)
17434 {
17435 /* Ignore trailing path separator. */
17436 q[-1] = NUL;
17437 q = gettail(p);
17438 }
17439 if (q > p && !mch_isFullName(buf))
17440 {
17441 /* symlink is relative to directory of argument */
17442 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17443 if (cpy != NULL)
17444 {
17445 STRCPY(cpy, p);
17446 STRCPY(gettail(cpy), buf);
17447 vim_free(p);
17448 p = cpy;
17449 }
17450 }
17451 else
17452 {
17453 vim_free(p);
17454 p = vim_strsave(buf);
17455 }
17456 }
17457
17458 if (remain == NULL)
17459 break;
17460
17461 /* Append the first path component of "remain" to "p". */
17462 q = getnextcomp(remain + 1);
17463 len = q - remain - (*q != NUL);
17464 cpy = vim_strnsave(p, STRLEN(p) + len);
17465 if (cpy != NULL)
17466 {
17467 STRNCAT(cpy, remain, len);
17468 vim_free(p);
17469 p = cpy;
17470 }
17471 /* Shorten "remain". */
17472 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017473 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474 else
17475 {
17476 vim_free(remain);
17477 remain = NULL;
17478 }
17479 }
17480
17481 /* If the result is a relative path name, make it explicitly relative to
17482 * the current directory if and only if the argument had this form. */
17483 if (!vim_ispathsep(*p))
17484 {
17485 if (is_relative_to_current
17486 && *p != NUL
17487 && !(p[0] == '.'
17488 && (p[1] == NUL
17489 || vim_ispathsep(p[1])
17490 || (p[1] == '.'
17491 && (p[2] == NUL
17492 || vim_ispathsep(p[2]))))))
17493 {
17494 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017495 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017496 if (cpy != NULL)
17497 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498 vim_free(p);
17499 p = cpy;
17500 }
17501 }
17502 else if (!is_relative_to_current)
17503 {
17504 /* Strip leading "./". */
17505 q = p;
17506 while (q[0] == '.' && vim_ispathsep(q[1]))
17507 q += 2;
17508 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017509 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017510 }
17511 }
17512
17513 /* Ensure that the result will have no trailing path separator
17514 * if the argument had none. But keep "/" or "//". */
17515 if (!has_trailing_pathsep)
17516 {
17517 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017518 if (after_pathsep(p, q))
17519 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520 }
17521
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017522 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523 }
17524# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017525 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526# endif
17527#endif
17528
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017529 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530
17531#ifdef HAVE_READLINK
17532fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017533 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017534#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017535 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017536}
17537
17538/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017539 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017540 */
17541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017542f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017543{
Bram Moolenaar33570922005-01-25 22:26:29 +000017544 list_T *l;
17545 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017546
Bram Moolenaar0d660222005-01-07 21:51:51 +000017547 if (argvars[0].v_type != VAR_LIST)
17548 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017549 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017550 && !tv_check_lock(l->lv_lock,
17551 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017552 {
17553 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017554 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017555 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017556 while (li != NULL)
17557 {
17558 ni = li->li_prev;
17559 list_append(l, li);
17560 li = ni;
17561 }
17562 rettv->vval.v_list = l;
17563 rettv->v_type = VAR_LIST;
17564 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017565 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017567}
17568
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017569#define SP_NOMOVE 0x01 /* don't move cursor */
17570#define SP_REPEAT 0x02 /* repeat to find outer pair */
17571#define SP_RETCOUNT 0x04 /* return matchcount */
17572#define SP_SETPCMARK 0x08 /* set previous context mark */
17573#define SP_START 0x10 /* accept match at start position */
17574#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17575#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017576#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017577
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017578static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017579
17580/*
17581 * Get flags for a search function.
17582 * Possibly sets "p_ws".
17583 * Returns BACKWARD, FORWARD or zero (for an error).
17584 */
17585 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017586get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017587{
17588 int dir = FORWARD;
17589 char_u *flags;
17590 char_u nbuf[NUMBUFLEN];
17591 int mask;
17592
17593 if (varp->v_type != VAR_UNKNOWN)
17594 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017595 flags = get_tv_string_buf_chk(varp, nbuf);
17596 if (flags == NULL)
17597 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017598 while (*flags != NUL)
17599 {
17600 switch (*flags)
17601 {
17602 case 'b': dir = BACKWARD; break;
17603 case 'w': p_ws = TRUE; break;
17604 case 'W': p_ws = FALSE; break;
17605 default: mask = 0;
17606 if (flagsp != NULL)
17607 switch (*flags)
17608 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017609 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017610 case 'e': mask = SP_END; break;
17611 case 'm': mask = SP_RETCOUNT; break;
17612 case 'n': mask = SP_NOMOVE; break;
17613 case 'p': mask = SP_SUBPAT; break;
17614 case 'r': mask = SP_REPEAT; break;
17615 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017616 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017617 }
17618 if (mask == 0)
17619 {
17620 EMSG2(_(e_invarg2), flags);
17621 dir = 0;
17622 }
17623 else
17624 *flagsp |= mask;
17625 }
17626 if (dir == 0)
17627 break;
17628 ++flags;
17629 }
17630 }
17631 return dir;
17632}
17633
Bram Moolenaar071d4272004-06-13 20:20:40 +000017634/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017635 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017636 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017637 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017638search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017639{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017640 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017641 char_u *pat;
17642 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017643 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017644 int save_p_ws = p_ws;
17645 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017646 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017647 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017648 proftime_T tm;
17649#ifdef FEAT_RELTIME
17650 long time_limit = 0;
17651#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017652 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017653 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017655 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017656 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017657 if (dir == 0)
17658 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017659 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017660 if (flags & SP_START)
17661 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017662 if (flags & SP_END)
17663 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017664 if (flags & SP_COLUMN)
17665 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017666
Bram Moolenaar76929292008-01-06 19:07:36 +000017667 /* Optional arguments: line number to stop searching and timeout. */
17668 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017669 {
17670 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17671 if (lnum_stop < 0)
17672 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017673#ifdef FEAT_RELTIME
17674 if (argvars[3].v_type != VAR_UNKNOWN)
17675 {
17676 time_limit = get_tv_number_chk(&argvars[3], NULL);
17677 if (time_limit < 0)
17678 goto theend;
17679 }
17680#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017681 }
17682
Bram Moolenaar76929292008-01-06 19:07:36 +000017683#ifdef FEAT_RELTIME
17684 /* Set the time limit, if there is one. */
17685 profile_setlimit(time_limit, &tm);
17686#endif
17687
Bram Moolenaar231334e2005-07-25 20:46:57 +000017688 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017689 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017690 * Check to make sure only those flags are set.
17691 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17692 * flags cannot be set. Check for that condition also.
17693 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017694 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017695 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017696 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017697 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017698 goto theend;
17699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017701 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017702 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017703 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017704 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017706 if (flags & SP_SUBPAT)
17707 retval = subpatnum;
17708 else
17709 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017710 if (flags & SP_SETPCMARK)
17711 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017712 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017713 if (match_pos != NULL)
17714 {
17715 /* Store the match cursor position */
17716 match_pos->lnum = pos.lnum;
17717 match_pos->col = pos.col + 1;
17718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719 /* "/$" will put the cursor after the end of the line, may need to
17720 * correct that here */
17721 check_cursor();
17722 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017723
17724 /* If 'n' flag is used: restore cursor position. */
17725 if (flags & SP_NOMOVE)
17726 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017727 else
17728 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017729theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017730 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017731
17732 return retval;
17733}
17734
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017735#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017736
17737/*
17738 * round() is not in C90, use ceil() or floor() instead.
17739 */
17740 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017741vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017742{
17743 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17744}
17745
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017746/*
17747 * "round({float})" function
17748 */
17749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017750f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017751{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017752 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017753
17754 rettv->v_type = VAR_FLOAT;
17755 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017756 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017757 else
17758 rettv->vval.v_float = 0.0;
17759}
17760#endif
17761
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017762/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017763 * "screenattr()" function
17764 */
17765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017766f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017767{
17768 int row;
17769 int col;
17770 int c;
17771
17772 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17773 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17774 if (row < 0 || row >= screen_Rows
17775 || col < 0 || col >= screen_Columns)
17776 c = -1;
17777 else
17778 c = ScreenAttrs[LineOffset[row] + col];
17779 rettv->vval.v_number = c;
17780}
17781
17782/*
17783 * "screenchar()" function
17784 */
17785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017786f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017787{
17788 int row;
17789 int col;
17790 int off;
17791 int c;
17792
17793 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17794 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17795 if (row < 0 || row >= screen_Rows
17796 || col < 0 || col >= screen_Columns)
17797 c = -1;
17798 else
17799 {
17800 off = LineOffset[row] + col;
17801#ifdef FEAT_MBYTE
17802 if (enc_utf8 && ScreenLinesUC[off] != 0)
17803 c = ScreenLinesUC[off];
17804 else
17805#endif
17806 c = ScreenLines[off];
17807 }
17808 rettv->vval.v_number = c;
17809}
17810
17811/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017812 * "screencol()" function
17813 *
17814 * First column is 1 to be consistent with virtcol().
17815 */
17816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017817f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017818{
17819 rettv->vval.v_number = screen_screencol() + 1;
17820}
17821
17822/*
17823 * "screenrow()" function
17824 */
17825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017826f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017827{
17828 rettv->vval.v_number = screen_screenrow() + 1;
17829}
17830
17831/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017832 * "search()" function
17833 */
17834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017835f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017836{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017837 int flags = 0;
17838
17839 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017840}
17841
Bram Moolenaar071d4272004-06-13 20:20:40 +000017842/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017843 * "searchdecl()" function
17844 */
17845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017846f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017847{
17848 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017849 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017850 int error = FALSE;
17851 char_u *name;
17852
17853 rettv->vval.v_number = 1; /* default: FAIL */
17854
17855 name = get_tv_string_chk(&argvars[0]);
17856 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017857 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017858 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017859 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17860 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17861 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017862 if (!error && name != NULL)
17863 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017864 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017865}
17866
17867/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017868 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017869 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017870 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017871searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872{
17873 char_u *spat, *mpat, *epat;
17874 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017876 int dir;
17877 int flags = 0;
17878 char_u nbuf1[NUMBUFLEN];
17879 char_u nbuf2[NUMBUFLEN];
17880 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017881 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017882 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017883 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017884
Bram Moolenaar071d4272004-06-13 20:20:40 +000017885 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017886 spat = get_tv_string_chk(&argvars[0]);
17887 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17888 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17889 if (spat == NULL || mpat == NULL || epat == NULL)
17890 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017891
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892 /* Handle the optional fourth argument: flags */
17893 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017894 if (dir == 0)
17895 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017896
17897 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017898 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17899 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017900 if ((flags & (SP_END | SP_SUBPAT)) != 0
17901 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017902 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017903 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017904 goto theend;
17905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017906
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017907 /* Using 'r' implies 'W', otherwise it doesn't work. */
17908 if (flags & SP_REPEAT)
17909 p_ws = FALSE;
17910
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017911 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017912 if (argvars[3].v_type == VAR_UNKNOWN
17913 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017914 skip = (char_u *)"";
17915 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017916 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017917 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017918 if (argvars[5].v_type != VAR_UNKNOWN)
17919 {
17920 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17921 if (lnum_stop < 0)
17922 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017923#ifdef FEAT_RELTIME
17924 if (argvars[6].v_type != VAR_UNKNOWN)
17925 {
17926 time_limit = get_tv_number_chk(&argvars[6], NULL);
17927 if (time_limit < 0)
17928 goto theend;
17929 }
17930#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017931 }
17932 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017933 if (skip == NULL)
17934 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017935
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017936 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017937 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017938
17939theend:
17940 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017941
17942 return retval;
17943}
17944
17945/*
17946 * "searchpair()" function
17947 */
17948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017949f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017950{
17951 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17952}
17953
17954/*
17955 * "searchpairpos()" function
17956 */
17957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017958f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017959{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017960 pos_T match_pos;
17961 int lnum = 0;
17962 int col = 0;
17963
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017964 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017965 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017966
17967 if (searchpair_cmn(argvars, &match_pos) > 0)
17968 {
17969 lnum = match_pos.lnum;
17970 col = match_pos.col;
17971 }
17972
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017973 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17974 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017975}
17976
17977/*
17978 * Search for a start/middle/end thing.
17979 * Used by searchpair(), see its documentation for the details.
17980 * Returns 0 or -1 for no match,
17981 */
17982 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017983do_searchpair(
17984 char_u *spat, /* start pattern */
17985 char_u *mpat, /* middle pattern */
17986 char_u *epat, /* end pattern */
17987 int dir, /* BACKWARD or FORWARD */
17988 char_u *skip, /* skip expression */
17989 int flags, /* SP_SETPCMARK and other SP_ values */
17990 pos_T *match_pos,
17991 linenr_T lnum_stop, /* stop at this line if not zero */
17992 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017993{
17994 char_u *save_cpo;
17995 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17996 long retval = 0;
17997 pos_T pos;
17998 pos_T firstpos;
17999 pos_T foundpos;
18000 pos_T save_cursor;
18001 pos_T save_pos;
18002 int n;
18003 int r;
18004 int nest = 1;
18005 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018006 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018007 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018008
18009 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18010 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018011 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018012
Bram Moolenaar76929292008-01-06 19:07:36 +000018013#ifdef FEAT_RELTIME
18014 /* Set the time limit, if there is one. */
18015 profile_setlimit(time_limit, &tm);
18016#endif
18017
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018018 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18019 * start/middle/end (pat3, for the top pair). */
18020 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18021 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18022 if (pat2 == NULL || pat3 == NULL)
18023 goto theend;
18024 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18025 if (*mpat == NUL)
18026 STRCPY(pat3, pat2);
18027 else
18028 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18029 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018030 if (flags & SP_START)
18031 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018032
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033 save_cursor = curwin->w_cursor;
18034 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018035 clearpos(&firstpos);
18036 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018037 pat = pat3;
18038 for (;;)
18039 {
18040 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018041 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018042 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18043 /* didn't find it or found the first match again: FAIL */
18044 break;
18045
18046 if (firstpos.lnum == 0)
18047 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018048 if (equalpos(pos, foundpos))
18049 {
18050 /* Found the same position again. Can happen with a pattern that
18051 * has "\zs" at the end and searching backwards. Advance one
18052 * character and try again. */
18053 if (dir == BACKWARD)
18054 decl(&pos);
18055 else
18056 incl(&pos);
18057 }
18058 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018059
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018060 /* clear the start flag to avoid getting stuck here */
18061 options &= ~SEARCH_START;
18062
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063 /* If the skip pattern matches, ignore this match. */
18064 if (*skip != NUL)
18065 {
18066 save_pos = curwin->w_cursor;
18067 curwin->w_cursor = pos;
18068 r = eval_to_bool(skip, &err, NULL, FALSE);
18069 curwin->w_cursor = save_pos;
18070 if (err)
18071 {
18072 /* Evaluating {skip} caused an error, break here. */
18073 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018074 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075 break;
18076 }
18077 if (r)
18078 continue;
18079 }
18080
18081 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18082 {
18083 /* Found end when searching backwards or start when searching
18084 * forward: nested pair. */
18085 ++nest;
18086 pat = pat2; /* nested, don't search for middle */
18087 }
18088 else
18089 {
18090 /* Found end when searching forward or start when searching
18091 * backward: end of (nested) pair; or found middle in outer pair. */
18092 if (--nest == 1)
18093 pat = pat3; /* outer level, search for middle */
18094 }
18095
18096 if (nest == 0)
18097 {
18098 /* Found the match: return matchcount or line number. */
18099 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018100 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018102 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018103 if (flags & SP_SETPCMARK)
18104 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018105 curwin->w_cursor = pos;
18106 if (!(flags & SP_REPEAT))
18107 break;
18108 nest = 1; /* search for next unmatched */
18109 }
18110 }
18111
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018112 if (match_pos != NULL)
18113 {
18114 /* Store the match cursor position */
18115 match_pos->lnum = curwin->w_cursor.lnum;
18116 match_pos->col = curwin->w_cursor.col + 1;
18117 }
18118
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018120 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018121 curwin->w_cursor = save_cursor;
18122
18123theend:
18124 vim_free(pat2);
18125 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018126 if (p_cpo == empty_option)
18127 p_cpo = save_cpo;
18128 else
18129 /* Darn, evaluating the {skip} expression changed the value. */
18130 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018131
18132 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018133}
18134
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018135/*
18136 * "searchpos()" function
18137 */
18138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018139f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018140{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018141 pos_T match_pos;
18142 int lnum = 0;
18143 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018144 int n;
18145 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018146
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018147 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018148 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018149
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018150 n = search_cmn(argvars, &match_pos, &flags);
18151 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018152 {
18153 lnum = match_pos.lnum;
18154 col = match_pos.col;
18155 }
18156
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018157 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18158 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018159 if (flags & SP_SUBPAT)
18160 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018161}
18162
Bram Moolenaar0d660222005-01-07 21:51:51 +000018163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018164f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018166#ifdef FEAT_CLIENTSERVER
18167 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018168 char_u *server = get_tv_string_chk(&argvars[0]);
18169 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170
Bram Moolenaar0d660222005-01-07 21:51:51 +000018171 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018172 if (server == NULL || reply == NULL)
18173 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018174 if (check_restricted() || check_secure())
18175 return;
18176# ifdef FEAT_X11
18177 if (check_connection() == FAIL)
18178 return;
18179# endif
18180
18181 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018182 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018183 EMSG(_("E258: Unable to send to client"));
18184 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018185 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018186 rettv->vval.v_number = 0;
18187#else
18188 rettv->vval.v_number = -1;
18189#endif
18190}
18191
Bram Moolenaar0d660222005-01-07 21:51:51 +000018192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018193f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018194{
18195 char_u *r = NULL;
18196
18197#ifdef FEAT_CLIENTSERVER
18198# ifdef WIN32
18199 r = serverGetVimNames();
18200# else
18201 make_connection();
18202 if (X_DISPLAY != NULL)
18203 r = serverGetVimNames(X_DISPLAY);
18204# endif
18205#endif
18206 rettv->v_type = VAR_STRING;
18207 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018208}
18209
18210/*
18211 * "setbufvar()" function
18212 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018214f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018215{
18216 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018218 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018219 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018220 char_u nbuf[NUMBUFLEN];
18221
18222 if (check_restricted() || check_secure())
18223 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018224 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18225 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018226 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018227 varp = &argvars[2];
18228
18229 if (buf != NULL && varname != NULL && varp != NULL)
18230 {
18231 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233
18234 if (*varname == '&')
18235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018236 long numval;
18237 char_u *strval;
18238 int error = FALSE;
18239
Bram Moolenaar071d4272004-06-13 20:20:40 +000018240 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018241 numval = get_tv_number_chk(varp, &error);
18242 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018243 if (!error && strval != NULL)
18244 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018245 }
18246 else
18247 {
18248 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18249 if (bufvarname != NULL)
18250 {
18251 STRCPY(bufvarname, "b:");
18252 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018253 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018254 vim_free(bufvarname);
18255 }
18256 }
18257
18258 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018259 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261}
18262
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018264f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018265{
18266 dict_T *d;
18267 dictitem_T *di;
18268 char_u *csearch;
18269
18270 if (argvars[0].v_type != VAR_DICT)
18271 {
18272 EMSG(_(e_dictreq));
18273 return;
18274 }
18275
18276 if ((d = argvars[0].vval.v_dict) != NULL)
18277 {
18278 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18279 if (csearch != NULL)
18280 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018281#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018282 if (enc_utf8)
18283 {
18284 int pcc[MAX_MCO];
18285 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018286
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018287 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18288 }
18289 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018290#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018291 set_last_csearch(PTR2CHAR(csearch),
18292 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018293 }
18294
18295 di = dict_find(d, (char_u *)"forward", -1);
18296 if (di != NULL)
18297 set_csearch_direction(get_tv_number(&di->di_tv)
18298 ? FORWARD : BACKWARD);
18299
18300 di = dict_find(d, (char_u *)"until", -1);
18301 if (di != NULL)
18302 set_csearch_until(!!get_tv_number(&di->di_tv));
18303 }
18304}
18305
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306/*
18307 * "setcmdpos()" function
18308 */
18309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018310f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018312 int pos = (int)get_tv_number(&argvars[0]) - 1;
18313
18314 if (pos >= 0)
18315 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316}
18317
18318/*
18319 * "setline()" function
18320 */
18321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018322f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323{
18324 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018325 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018326 list_T *l = NULL;
18327 listitem_T *li = NULL;
18328 long added = 0;
18329 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018330
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018331 lnum = get_tv_lnum(&argvars[0]);
18332 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018333 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018334 l = argvars[1].vval.v_list;
18335 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018336 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018337 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018338 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018339
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018340 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018341 for (;;)
18342 {
18343 if (l != NULL)
18344 {
18345 /* list argument, get next string */
18346 if (li == NULL)
18347 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018348 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018349 li = li->li_next;
18350 }
18351
18352 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018353 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018354 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018355
18356 /* When coming here from Insert mode, sync undo, so that this can be
18357 * undone separately from what was previously inserted. */
18358 if (u_sync_once == 2)
18359 {
18360 u_sync_once = 1; /* notify that u_sync() was called */
18361 u_sync(TRUE);
18362 }
18363
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018364 if (lnum <= curbuf->b_ml.ml_line_count)
18365 {
18366 /* existing line, replace it */
18367 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18368 {
18369 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018370 if (lnum == curwin->w_cursor.lnum)
18371 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018372 rettv->vval.v_number = 0; /* OK */
18373 }
18374 }
18375 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18376 {
18377 /* lnum is one past the last line, append the line */
18378 ++added;
18379 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18380 rettv->vval.v_number = 0; /* OK */
18381 }
18382
18383 if (l == NULL) /* only one string argument */
18384 break;
18385 ++lnum;
18386 }
18387
18388 if (added > 0)
18389 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018390}
18391
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018392static 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 +000018393
Bram Moolenaar071d4272004-06-13 20:20:40 +000018394/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018395 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018396 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018398set_qf_ll_list(
18399 win_T *wp UNUSED,
18400 typval_T *list_arg UNUSED,
18401 typval_T *action_arg UNUSED,
18402 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018403{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018404#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018405 char_u *act;
18406 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018407#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018408
Bram Moolenaar2641f772005-03-25 21:58:17 +000018409 rettv->vval.v_number = -1;
18410
18411#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018412 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018413 EMSG(_(e_listreq));
18414 else
18415 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018416 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018417
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018418 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018419 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018420 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018421 if (act == NULL)
18422 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018423 if (*act == 'a' || *act == 'r')
18424 action = *act;
18425 }
18426
Bram Moolenaar81484f42012-12-05 15:16:47 +010018427 if (l != NULL && set_errorlist(wp, l, action,
18428 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018429 rettv->vval.v_number = 0;
18430 }
18431#endif
18432}
18433
18434/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018435 * "setloclist()" function
18436 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018438f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018439{
18440 win_T *win;
18441
18442 rettv->vval.v_number = -1;
18443
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018444 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018445 if (win != NULL)
18446 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18447}
18448
18449/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018450 * "setmatches()" function
18451 */
18452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018453f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018454{
18455#ifdef FEAT_SEARCH_EXTRA
18456 list_T *l;
18457 listitem_T *li;
18458 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018459 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018460
18461 rettv->vval.v_number = -1;
18462 if (argvars[0].v_type != VAR_LIST)
18463 {
18464 EMSG(_(e_listreq));
18465 return;
18466 }
18467 if ((l = argvars[0].vval.v_list) != NULL)
18468 {
18469
18470 /* To some extent make sure that we are dealing with a list from
18471 * "getmatches()". */
18472 li = l->lv_first;
18473 while (li != NULL)
18474 {
18475 if (li->li_tv.v_type != VAR_DICT
18476 || (d = li->li_tv.vval.v_dict) == NULL)
18477 {
18478 EMSG(_(e_invarg));
18479 return;
18480 }
18481 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018482 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18483 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018484 && dict_find(d, (char_u *)"priority", -1) != NULL
18485 && dict_find(d, (char_u *)"id", -1) != NULL))
18486 {
18487 EMSG(_(e_invarg));
18488 return;
18489 }
18490 li = li->li_next;
18491 }
18492
18493 clear_matches(curwin);
18494 li = l->lv_first;
18495 while (li != NULL)
18496 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018497 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018498 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018499 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018500 char_u *group;
18501 int priority;
18502 int id;
18503 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018504
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018505 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018506 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18507 {
18508 if (s == NULL)
18509 {
18510 s = list_alloc();
18511 if (s == NULL)
18512 return;
18513 }
18514
18515 /* match from matchaddpos() */
18516 for (i = 1; i < 9; i++)
18517 {
18518 sprintf((char *)buf, (char *)"pos%d", i);
18519 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18520 {
18521 if (di->di_tv.v_type != VAR_LIST)
18522 return;
18523
18524 list_append_tv(s, &di->di_tv);
18525 s->lv_refcount++;
18526 }
18527 else
18528 break;
18529 }
18530 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018531
18532 group = get_dict_string(d, (char_u *)"group", FALSE);
18533 priority = (int)get_dict_number(d, (char_u *)"priority");
18534 id = (int)get_dict_number(d, (char_u *)"id");
18535 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18536 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18537 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018538 if (i == 0)
18539 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018540 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018541 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018542 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018543 }
18544 else
18545 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018546 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018547 list_unref(s);
18548 s = NULL;
18549 }
18550
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018551 li = li->li_next;
18552 }
18553 rettv->vval.v_number = 0;
18554 }
18555#endif
18556}
18557
18558/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018559 * "setpos()" function
18560 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018562f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018563{
18564 pos_T pos;
18565 int fnum;
18566 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018567 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018568
Bram Moolenaar08250432008-02-13 11:42:46 +000018569 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018570 name = get_tv_string_chk(argvars);
18571 if (name != NULL)
18572 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018573 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018574 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018575 if (--pos.col < 0)
18576 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018577 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018578 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018579 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018580 if (fnum == curbuf->b_fnum)
18581 {
18582 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018583 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018584 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018585 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018586 curwin->w_set_curswant = FALSE;
18587 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018588 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018589 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018590 }
18591 else
18592 EMSG(_(e_invarg));
18593 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018594 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18595 {
18596 /* set mark */
18597 if (setmark_pos(name[1], &pos, fnum) == OK)
18598 rettv->vval.v_number = 0;
18599 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018600 else
18601 EMSG(_(e_invarg));
18602 }
18603 }
18604}
18605
18606/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018607 * "setqflist()" function
18608 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018610f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018611{
18612 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18613}
18614
18615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616 * "setreg()" function
18617 */
18618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018619f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620{
18621 int regname;
18622 char_u *strregname;
18623 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018624 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625 int append;
18626 char_u yank_type;
18627 long block_len;
18628
18629 block_len = -1;
18630 yank_type = MAUTO;
18631 append = FALSE;
18632
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018633 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018634 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018636 if (strregname == NULL)
18637 return; /* type error; errmsg already given */
18638 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018639 if (regname == 0 || regname == '@')
18640 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018642 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018644 stropt = get_tv_string_chk(&argvars[2]);
18645 if (stropt == NULL)
18646 return; /* type error */
18647 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018648 switch (*stropt)
18649 {
18650 case 'a': case 'A': /* append */
18651 append = TRUE;
18652 break;
18653 case 'v': case 'c': /* character-wise selection */
18654 yank_type = MCHAR;
18655 break;
18656 case 'V': case 'l': /* line-wise selection */
18657 yank_type = MLINE;
18658 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659 case 'b': case Ctrl_V: /* block-wise selection */
18660 yank_type = MBLOCK;
18661 if (VIM_ISDIGIT(stropt[1]))
18662 {
18663 ++stropt;
18664 block_len = getdigits(&stropt) - 1;
18665 --stropt;
18666 }
18667 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668 }
18669 }
18670
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018671 if (argvars[1].v_type == VAR_LIST)
18672 {
18673 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018674 char_u **allocval;
18675 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018676 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018677 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018678 int len = argvars[1].vval.v_list->lv_len;
18679 listitem_T *li;
18680
Bram Moolenaar7d647822014-04-05 21:28:56 +020018681 /* First half: use for pointers to result lines; second half: use for
18682 * pointers to allocated copies. */
18683 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018684 if (lstval == NULL)
18685 return;
18686 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018687 allocval = lstval + len + 2;
18688 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018689
18690 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18691 li = li->li_next)
18692 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018693 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018694 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018695 goto free_lstval;
18696 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018697 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018698 /* Need to make a copy, next get_tv_string_buf_chk() will
18699 * overwrite the string. */
18700 strval = vim_strsave(buf);
18701 if (strval == NULL)
18702 goto free_lstval;
18703 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018704 }
18705 *curval++ = strval;
18706 }
18707 *curval++ = NULL;
18708
18709 write_reg_contents_lst(regname, lstval, -1,
18710 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018711free_lstval:
18712 while (curallocval > allocval)
18713 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018714 vim_free(lstval);
18715 }
18716 else
18717 {
18718 strval = get_tv_string_chk(&argvars[1]);
18719 if (strval == NULL)
18720 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018721 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018722 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018723 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018724 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725}
18726
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018727/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018728 * "settabvar()" function
18729 */
18730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018731f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018732{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018733#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018734 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018735 tabpage_T *tp;
18736#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018737 char_u *varname, *tabvarname;
18738 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018739
18740 rettv->vval.v_number = 0;
18741
18742 if (check_restricted() || check_secure())
18743 return;
18744
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018745#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018746 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018747#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018748 varname = get_tv_string_chk(&argvars[1]);
18749 varp = &argvars[2];
18750
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018751 if (varname != NULL && varp != NULL
18752#ifdef FEAT_WINDOWS
18753 && tp != NULL
18754#endif
18755 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018756 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018757#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018758 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018759 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018760#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018761
18762 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18763 if (tabvarname != NULL)
18764 {
18765 STRCPY(tabvarname, "t:");
18766 STRCPY(tabvarname + 2, varname);
18767 set_var(tabvarname, varp, TRUE);
18768 vim_free(tabvarname);
18769 }
18770
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018771#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018772 /* Restore current tabpage */
18773 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018774 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018775#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018776 }
18777}
18778
18779/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018780 * "settabwinvar()" function
18781 */
18782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018783f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018784{
18785 setwinvar(argvars, rettv, 1);
18786}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787
18788/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018789 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018792f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018794 setwinvar(argvars, rettv, 0);
18795}
18796
18797/*
18798 * "setwinvar()" and "settabwinvar()" functions
18799 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018800
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018802setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018803{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018804 win_T *win;
18805#ifdef FEAT_WINDOWS
18806 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018807 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018808 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809#endif
18810 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018811 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018813 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814
18815 if (check_restricted() || check_secure())
18816 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018817
18818#ifdef FEAT_WINDOWS
18819 if (off == 1)
18820 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18821 else
18822 tp = curtab;
18823#endif
18824 win = find_win_by_nr(&argvars[off], tp);
18825 varname = get_tv_string_chk(&argvars[off + 1]);
18826 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018827
18828 if (win != NULL && varname != NULL && varp != NULL)
18829 {
18830#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018831 need_switch_win = !(tp == curtab && win == curwin);
18832 if (!need_switch_win
18833 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018835 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018836 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018838 long numval;
18839 char_u *strval;
18840 int error = FALSE;
18841
18842 ++varname;
18843 numval = get_tv_number_chk(varp, &error);
18844 strval = get_tv_string_buf_chk(varp, nbuf);
18845 if (!error && strval != NULL)
18846 set_option_value(varname, numval, strval, OPT_LOCAL);
18847 }
18848 else
18849 {
18850 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18851 if (winvarname != NULL)
18852 {
18853 STRCPY(winvarname, "w:");
18854 STRCPY(winvarname + 2, varname);
18855 set_var(winvarname, varp, TRUE);
18856 vim_free(winvarname);
18857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858 }
18859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018860#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018861 if (need_switch_win)
18862 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018863#endif
18864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865}
18866
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018867#ifdef FEAT_CRYPT
18868/*
18869 * "sha256({string})" function
18870 */
18871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018872f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018873{
18874 char_u *p;
18875
18876 p = get_tv_string(&argvars[0]);
18877 rettv->vval.v_string = vim_strsave(
18878 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18879 rettv->v_type = VAR_STRING;
18880}
18881#endif /* FEAT_CRYPT */
18882
Bram Moolenaar071d4272004-06-13 20:20:40 +000018883/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018884 * "shellescape({string})" function
18885 */
18886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018887f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018888{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018889 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018890 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018891 rettv->v_type = VAR_STRING;
18892}
18893
18894/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018895 * shiftwidth() function
18896 */
18897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018898f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018899{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018900 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018901}
18902
18903/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018904 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905 */
18906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018907f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018909 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018910
Bram Moolenaar0d660222005-01-07 21:51:51 +000018911 p = get_tv_string(&argvars[0]);
18912 rettv->vval.v_string = vim_strsave(p);
18913 simplify_filename(rettv->vval.v_string); /* simplify in place */
18914 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915}
18916
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018917#ifdef FEAT_FLOAT
18918/*
18919 * "sin()" function
18920 */
18921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018922f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018923{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018924 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018925
18926 rettv->v_type = VAR_FLOAT;
18927 if (get_float_arg(argvars, &f) == OK)
18928 rettv->vval.v_float = sin(f);
18929 else
18930 rettv->vval.v_float = 0.0;
18931}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018932
18933/*
18934 * "sinh()" function
18935 */
18936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018937f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018938{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018939 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018940
18941 rettv->v_type = VAR_FLOAT;
18942 if (get_float_arg(argvars, &f) == OK)
18943 rettv->vval.v_float = sinh(f);
18944 else
18945 rettv->vval.v_float = 0.0;
18946}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018947#endif
18948
Bram Moolenaar0d660222005-01-07 21:51:51 +000018949static int
18950#ifdef __BORLANDC__
18951 _RTLENTRYF
18952#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018953 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018954static int
18955#ifdef __BORLANDC__
18956 _RTLENTRYF
18957#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018958 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018959
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018960/* struct used in the array that's given to qsort() */
18961typedef struct
18962{
18963 listitem_T *item;
18964 int idx;
18965} sortItem_T;
18966
Bram Moolenaar0b962472016-02-22 22:51:33 +010018967/* struct storing information about current sort */
18968typedef struct
18969{
18970 int item_compare_ic;
18971 int item_compare_numeric;
18972 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018973#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018974 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018975#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018976 char_u *item_compare_func;
18977 dict_T *item_compare_selfdict;
18978 int item_compare_func_err;
18979 int item_compare_keep_zero;
18980} sortinfo_T;
18981static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018982static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018983#define ITEM_COMPARE_FAIL 999
18984
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018986 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018988 static int
18989#ifdef __BORLANDC__
18990_RTLENTRYF
18991#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018992item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018994 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018995 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018996 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018997 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018998 int res;
18999 char_u numbuf1[NUMBUFLEN];
19000 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019002 si1 = (sortItem_T *)s1;
19003 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019004 tv1 = &si1->item->li_tv;
19005 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019006
Bram Moolenaar0b962472016-02-22 22:51:33 +010019007 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019008 {
19009 long v1 = get_tv_number(tv1);
19010 long v2 = get_tv_number(tv2);
19011
19012 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19013 }
19014
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019015#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019016 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019017 {
19018 float_T v1 = get_tv_float(tv1);
19019 float_T v2 = get_tv_float(tv2);
19020
19021 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19022 }
19023#endif
19024
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019025 /* tv2string() puts quotes around a string and allocates memory. Don't do
19026 * that for string variables. Use a single quote when comparing with a
19027 * non-string to do what the docs promise. */
19028 if (tv1->v_type == VAR_STRING)
19029 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019030 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019031 p1 = (char_u *)"'";
19032 else
19033 p1 = tv1->vval.v_string;
19034 }
19035 else
19036 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19037 if (tv2->v_type == VAR_STRING)
19038 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019039 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019040 p2 = (char_u *)"'";
19041 else
19042 p2 = tv2->vval.v_string;
19043 }
19044 else
19045 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019046 if (p1 == NULL)
19047 p1 = (char_u *)"";
19048 if (p2 == NULL)
19049 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019050 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019051 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019052 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019053 res = STRICMP(p1, p2);
19054 else
19055 res = STRCMP(p1, p2);
19056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019058 {
19059 double n1, n2;
19060 n1 = strtod((char *)p1, (char **)&p1);
19061 n2 = strtod((char *)p2, (char **)&p2);
19062 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19063 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019064
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019065 /* When the result would be zero, compare the item indexes. Makes the
19066 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019067 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019068 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019069
Bram Moolenaar0d660222005-01-07 21:51:51 +000019070 vim_free(tofree1);
19071 vim_free(tofree2);
19072 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073}
19074
19075 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019076#ifdef __BORLANDC__
19077_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019078#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019079item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019080{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019081 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019082 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019083 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019084 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019085 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019086
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019087 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019088 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019089 return 0;
19090
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019091 si1 = (sortItem_T *)s1;
19092 si2 = (sortItem_T *)s2;
19093
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019094 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019095 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019096 copy_tv(&si1->item->li_tv, &argv[0]);
19097 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019098
19099 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019100 res = call_func(sortinfo->item_compare_func,
Bram Moolenaar4e221c92016-02-23 13:20:22 +010019101 (int)STRLEN(sortinfo->item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019102 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar0b962472016-02-22 22:51:33 +010019103 sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019104 clear_tv(&argv[0]);
19105 clear_tv(&argv[1]);
19106
19107 if (res == FAIL)
19108 res = ITEM_COMPARE_FAIL;
19109 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019110 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19111 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019112 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019113 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019114
19115 /* When the result would be zero, compare the pointers themselves. Makes
19116 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019117 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019118 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019119
Bram Moolenaar0d660222005-01-07 21:51:51 +000019120 return res;
19121}
19122
19123/*
19124 * "sort({list})" function
19125 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019127do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128{
Bram Moolenaar33570922005-01-25 22:26:29 +000019129 list_T *l;
19130 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019131 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019132 sortinfo_T *old_sortinfo;
19133 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019134 long len;
19135 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019136
Bram Moolenaar0b962472016-02-22 22:51:33 +010019137 /* Pointer to current info struct used in compare function. Save and
19138 * restore the current one for nested calls. */
19139 old_sortinfo = sortinfo;
19140 sortinfo = &info;
19141
Bram Moolenaar0d660222005-01-07 21:51:51 +000019142 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019143 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019144 else
19145 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019146 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019147 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019148 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19149 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019150 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019151 rettv->vval.v_list = l;
19152 rettv->v_type = VAR_LIST;
19153 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019154
Bram Moolenaar0d660222005-01-07 21:51:51 +000019155 len = list_len(l);
19156 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019157 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019158
Bram Moolenaar0b962472016-02-22 22:51:33 +010019159 info.item_compare_ic = FALSE;
19160 info.item_compare_numeric = FALSE;
19161 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019162#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019163 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019164#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019165 info.item_compare_func = NULL;
19166 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019167 if (argvars[1].v_type != VAR_UNKNOWN)
19168 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019169 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019170 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019171 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019172 else
19173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019174 int error = FALSE;
19175
19176 i = get_tv_number_chk(&argvars[1], &error);
19177 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019178 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019179 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019180 info.item_compare_ic = TRUE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019181 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019182 info.item_compare_func = get_tv_string(&argvars[1]);
19183 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019184 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019185 if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019186 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019187 info.item_compare_func = NULL;
19188 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019189 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019190 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019191 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019192 info.item_compare_func = NULL;
19193 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019194 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019195#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019196 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019197 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019198 info.item_compare_func = NULL;
19199 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019200 }
19201#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019202 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019203 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019204 info.item_compare_func = NULL;
19205 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019206 }
19207 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019208 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019209
19210 if (argvars[2].v_type != VAR_UNKNOWN)
19211 {
19212 /* optional third argument: {dict} */
19213 if (argvars[2].v_type != VAR_DICT)
19214 {
19215 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019216 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019217 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019218 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019219 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019221
Bram Moolenaar0d660222005-01-07 21:51:51 +000019222 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019223 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019224 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019225 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226
Bram Moolenaar327aa022014-03-25 18:24:23 +010019227 i = 0;
19228 if (sort)
19229 {
19230 /* sort(): ptrs will be the list to sort */
19231 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019232 {
19233 ptrs[i].item = li;
19234 ptrs[i].idx = i;
19235 ++i;
19236 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019237
Bram Moolenaar0b962472016-02-22 22:51:33 +010019238 info.item_compare_func_err = FALSE;
19239 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019240 /* test the compare function */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019241 if (info.item_compare_func != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019242 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019243 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019244 EMSG(_("E702: Sort compare function failed"));
19245 else
19246 {
19247 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019248 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019249 info.item_compare_func == NULL
19250 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019251
Bram Moolenaar0b962472016-02-22 22:51:33 +010019252 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019253 {
19254 /* Clear the List and append the items in sorted order. */
19255 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19256 l->lv_len = 0;
19257 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019258 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019259 }
19260 }
19261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019262 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019263 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019264 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019265
19266 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019267 info.item_compare_func_err = FALSE;
19268 info.item_compare_keep_zero = TRUE;
19269 item_compare_func_ptr = info.item_compare_func
Bram Moolenaar327aa022014-03-25 18:24:23 +010019270 ? item_compare2 : item_compare;
19271
19272 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19273 li = li->li_next)
19274 {
19275 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19276 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019277 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019278 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019279 {
19280 EMSG(_("E882: Uniq compare function failed"));
19281 break;
19282 }
19283 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019284
Bram Moolenaar0b962472016-02-22 22:51:33 +010019285 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019286 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019287 while (--i >= 0)
19288 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019289 li = ptrs[i].item->li_next;
19290 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019291 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019292 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019293 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019294 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019295 list_fix_watch(l, li);
19296 listitem_free(li);
19297 l->lv_len--;
19298 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019299 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019300 }
19301
19302 vim_free(ptrs);
19303 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019304theend:
19305 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019306}
19307
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019308/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019309 * "sort({list})" function
19310 */
19311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019312f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019313{
19314 do_sort_uniq(argvars, rettv, TRUE);
19315}
19316
19317/*
19318 * "uniq({list})" function
19319 */
19320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019321f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019322{
19323 do_sort_uniq(argvars, rettv, FALSE);
19324}
19325
19326/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019327 * "soundfold({word})" function
19328 */
19329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019330f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019331{
19332 char_u *s;
19333
19334 rettv->v_type = VAR_STRING;
19335 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019336#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019337 rettv->vval.v_string = eval_soundfold(s);
19338#else
19339 rettv->vval.v_string = vim_strsave(s);
19340#endif
19341}
19342
19343/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019344 * "spellbadword()" function
19345 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019347f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019348{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019349 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019350 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019351 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019352
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019353 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019354 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019355
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019356#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019357 if (argvars[0].v_type == VAR_UNKNOWN)
19358 {
19359 /* Find the start and length of the badly spelled word. */
19360 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19361 if (len != 0)
19362 word = ml_get_cursor();
19363 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019364 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019365 {
19366 char_u *str = get_tv_string_chk(&argvars[0]);
19367 int capcol = -1;
19368
19369 if (str != NULL)
19370 {
19371 /* Check the argument for spelling. */
19372 while (*str != NUL)
19373 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019374 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019375 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019376 {
19377 word = str;
19378 break;
19379 }
19380 str += len;
19381 }
19382 }
19383 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019384#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019385
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019386 list_append_string(rettv->vval.v_list, word, len);
19387 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019388 attr == HLF_SPB ? "bad" :
19389 attr == HLF_SPR ? "rare" :
19390 attr == HLF_SPL ? "local" :
19391 attr == HLF_SPC ? "caps" :
19392 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019393}
19394
19395/*
19396 * "spellsuggest()" function
19397 */
19398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019399f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019400{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019401#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019402 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019403 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019404 int maxcount;
19405 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019406 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019407 listitem_T *li;
19408 int need_capital = FALSE;
19409#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019410
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019411 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019412 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019413
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019414#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019415 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019416 {
19417 str = get_tv_string(&argvars[0]);
19418 if (argvars[1].v_type != VAR_UNKNOWN)
19419 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019420 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019421 if (maxcount <= 0)
19422 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019423 if (argvars[2].v_type != VAR_UNKNOWN)
19424 {
19425 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19426 if (typeerr)
19427 return;
19428 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019429 }
19430 else
19431 maxcount = 25;
19432
Bram Moolenaar4770d092006-01-12 23:22:24 +000019433 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019434
19435 for (i = 0; i < ga.ga_len; ++i)
19436 {
19437 str = ((char_u **)ga.ga_data)[i];
19438
19439 li = listitem_alloc();
19440 if (li == NULL)
19441 vim_free(str);
19442 else
19443 {
19444 li->li_tv.v_type = VAR_STRING;
19445 li->li_tv.v_lock = 0;
19446 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019447 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019448 }
19449 }
19450 ga_clear(&ga);
19451 }
19452#endif
19453}
19454
Bram Moolenaar0d660222005-01-07 21:51:51 +000019455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019456f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019457{
19458 char_u *str;
19459 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019460 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019461 regmatch_T regmatch;
19462 char_u patbuf[NUMBUFLEN];
19463 char_u *save_cpo;
19464 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019465 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019466 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019467 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019468
19469 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19470 save_cpo = p_cpo;
19471 p_cpo = (char_u *)"";
19472
19473 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019474 if (argvars[1].v_type != VAR_UNKNOWN)
19475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019476 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19477 if (pat == NULL)
19478 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019479 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019480 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019481 }
19482 if (pat == NULL || *pat == NUL)
19483 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019484
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019485 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019486 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019487 if (typeerr)
19488 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489
Bram Moolenaar0d660222005-01-07 21:51:51 +000019490 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19491 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019492 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019493 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019494 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019495 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019496 if (*str == NUL)
19497 match = FALSE; /* empty item at the end */
19498 else
19499 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019500 if (match)
19501 end = regmatch.startp[0];
19502 else
19503 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019504 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19505 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019506 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019507 if (list_append_string(rettv->vval.v_list, str,
19508 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019509 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019510 }
19511 if (!match)
19512 break;
19513 /* Advance to just after the match. */
19514 if (regmatch.endp[0] > str)
19515 col = 0;
19516 else
19517 {
19518 /* Don't get stuck at the same match. */
19519#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019520 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019521#else
19522 col = 1;
19523#endif
19524 }
19525 str = regmatch.endp[0];
19526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527
Bram Moolenaar473de612013-06-08 18:19:48 +020019528 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019530
Bram Moolenaar0d660222005-01-07 21:51:51 +000019531 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532}
19533
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019534#ifdef FEAT_FLOAT
19535/*
19536 * "sqrt()" function
19537 */
19538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019539f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019540{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019541 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019542
19543 rettv->v_type = VAR_FLOAT;
19544 if (get_float_arg(argvars, &f) == OK)
19545 rettv->vval.v_float = sqrt(f);
19546 else
19547 rettv->vval.v_float = 0.0;
19548}
19549
19550/*
19551 * "str2float()" function
19552 */
19553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019554f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019555{
19556 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19557
19558 if (*p == '+')
19559 p = skipwhite(p + 1);
19560 (void)string2float(p, &rettv->vval.v_float);
19561 rettv->v_type = VAR_FLOAT;
19562}
19563#endif
19564
Bram Moolenaar2c932302006-03-18 21:42:09 +000019565/*
19566 * "str2nr()" function
19567 */
19568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019569f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019570{
19571 int base = 10;
19572 char_u *p;
19573 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019574 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019575
19576 if (argvars[1].v_type != VAR_UNKNOWN)
19577 {
19578 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019579 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019580 {
19581 EMSG(_(e_invarg));
19582 return;
19583 }
19584 }
19585
19586 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019587 if (*p == '+')
19588 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019589 switch (base)
19590 {
19591 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19592 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19593 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19594 default: what = 0;
19595 }
19596 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019597 rettv->vval.v_number = n;
19598}
19599
Bram Moolenaar071d4272004-06-13 20:20:40 +000019600#ifdef HAVE_STRFTIME
19601/*
19602 * "strftime({format}[, {time}])" function
19603 */
19604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019605f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606{
19607 char_u result_buf[256];
19608 struct tm *curtime;
19609 time_t seconds;
19610 char_u *p;
19611
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019612 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019614 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019615 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616 seconds = time(NULL);
19617 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019618 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619 curtime = localtime(&seconds);
19620 /* MSVC returns NULL for an invalid value of seconds. */
19621 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019622 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019623 else
19624 {
19625# ifdef FEAT_MBYTE
19626 vimconv_T conv;
19627 char_u *enc;
19628
19629 conv.vc_type = CONV_NONE;
19630 enc = enc_locale();
19631 convert_setup(&conv, p_enc, enc);
19632 if (conv.vc_type != CONV_NONE)
19633 p = string_convert(&conv, p, NULL);
19634# endif
19635 if (p != NULL)
19636 (void)strftime((char *)result_buf, sizeof(result_buf),
19637 (char *)p, curtime);
19638 else
19639 result_buf[0] = NUL;
19640
19641# ifdef FEAT_MBYTE
19642 if (conv.vc_type != CONV_NONE)
19643 vim_free(p);
19644 convert_setup(&conv, enc, p_enc);
19645 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019646 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647 else
19648# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019649 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019650
19651# ifdef FEAT_MBYTE
19652 /* Release conversion descriptors */
19653 convert_setup(&conv, NULL, NULL);
19654 vim_free(enc);
19655# endif
19656 }
19657}
19658#endif
19659
19660/*
19661 * "stridx()" function
19662 */
19663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019664f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665{
19666 char_u buf[NUMBUFLEN];
19667 char_u *needle;
19668 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019669 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019670 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019671 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019672
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019673 needle = get_tv_string_chk(&argvars[1]);
19674 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019675 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019676 if (needle == NULL || haystack == NULL)
19677 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019678
Bram Moolenaar33570922005-01-25 22:26:29 +000019679 if (argvars[2].v_type != VAR_UNKNOWN)
19680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019681 int error = FALSE;
19682
19683 start_idx = get_tv_number_chk(&argvars[2], &error);
19684 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019685 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019686 if (start_idx >= 0)
19687 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019688 }
19689
19690 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19691 if (pos != NULL)
19692 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019693}
19694
19695/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019696 * "string()" function
19697 */
19698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019699f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019700{
19701 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019702 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019703
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019704 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019705 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019706 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019707 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019708 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019709}
19710
19711/*
19712 * "strlen()" function
19713 */
19714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019715f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019717 rettv->vval.v_number = (varnumber_T)(STRLEN(
19718 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019719}
19720
19721/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019722 * "strchars()" function
19723 */
19724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019725f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019726{
19727 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019728 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019729#ifdef FEAT_MBYTE
19730 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019731 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019732#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019733
19734 if (argvars[1].v_type != VAR_UNKNOWN)
19735 skipcc = get_tv_number_chk(&argvars[1], NULL);
19736 if (skipcc < 0 || skipcc > 1)
19737 EMSG(_(e_invarg));
19738 else
19739 {
19740#ifdef FEAT_MBYTE
19741 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19742 while (*s != NUL)
19743 {
19744 func_mb_ptr2char_adv(&s);
19745 ++len;
19746 }
19747 rettv->vval.v_number = len;
19748#else
19749 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19750#endif
19751 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019752}
19753
19754/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019755 * "strdisplaywidth()" function
19756 */
19757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019758f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019759{
19760 char_u *s = get_tv_string(&argvars[0]);
19761 int col = 0;
19762
19763 if (argvars[1].v_type != VAR_UNKNOWN)
19764 col = get_tv_number(&argvars[1]);
19765
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019766 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019767}
19768
19769/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019770 * "strwidth()" function
19771 */
19772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019773f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019774{
19775 char_u *s = get_tv_string(&argvars[0]);
19776
19777 rettv->vval.v_number = (varnumber_T)(
19778#ifdef FEAT_MBYTE
19779 mb_string2cells(s, -1)
19780#else
19781 STRLEN(s)
19782#endif
19783 );
19784}
19785
19786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019787 * "strpart()" function
19788 */
19789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019790f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791{
19792 char_u *p;
19793 int n;
19794 int len;
19795 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019796 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019798 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019799 slen = (int)STRLEN(p);
19800
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019801 n = get_tv_number_chk(&argvars[1], &error);
19802 if (error)
19803 len = 0;
19804 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019805 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019806 else
19807 len = slen - n; /* default len: all bytes that are available. */
19808
19809 /*
19810 * Only return the overlap between the specified part and the actual
19811 * string.
19812 */
19813 if (n < 0)
19814 {
19815 len += n;
19816 n = 0;
19817 }
19818 else if (n > slen)
19819 n = slen;
19820 if (len < 0)
19821 len = 0;
19822 else if (n + len > slen)
19823 len = slen - n;
19824
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019825 rettv->v_type = VAR_STRING;
19826 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019827}
19828
19829/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019830 * "strridx()" function
19831 */
19832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019833f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019834{
19835 char_u buf[NUMBUFLEN];
19836 char_u *needle;
19837 char_u *haystack;
19838 char_u *rest;
19839 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019840 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019841
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019842 needle = get_tv_string_chk(&argvars[1]);
19843 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019844
19845 rettv->vval.v_number = -1;
19846 if (needle == NULL || haystack == NULL)
19847 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019848
19849 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019850 if (argvars[2].v_type != VAR_UNKNOWN)
19851 {
19852 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019853 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019854 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019855 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019856 }
19857 else
19858 end_idx = haystack_len;
19859
Bram Moolenaar0d660222005-01-07 21:51:51 +000019860 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019861 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019862 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019863 lastmatch = haystack + end_idx;
19864 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019865 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019866 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019867 for (rest = haystack; *rest != '\0'; ++rest)
19868 {
19869 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019870 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019871 break;
19872 lastmatch = rest;
19873 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019874 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019875
19876 if (lastmatch == NULL)
19877 rettv->vval.v_number = -1;
19878 else
19879 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19880}
19881
19882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019883 * "strtrans()" function
19884 */
19885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019886f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019887{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019888 rettv->v_type = VAR_STRING;
19889 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019890}
19891
19892/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019893 * "submatch()" function
19894 */
19895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019896f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019897{
Bram Moolenaar41571762014-04-02 19:00:58 +020019898 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019899 int no;
19900 int retList = 0;
19901
19902 no = (int)get_tv_number_chk(&argvars[0], &error);
19903 if (error)
19904 return;
19905 error = FALSE;
19906 if (argvars[1].v_type != VAR_UNKNOWN)
19907 retList = get_tv_number_chk(&argvars[1], &error);
19908 if (error)
19909 return;
19910
19911 if (retList == 0)
19912 {
19913 rettv->v_type = VAR_STRING;
19914 rettv->vval.v_string = reg_submatch(no);
19915 }
19916 else
19917 {
19918 rettv->v_type = VAR_LIST;
19919 rettv->vval.v_list = reg_submatch_list(no);
19920 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019921}
19922
19923/*
19924 * "substitute()" function
19925 */
19926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019927f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019928{
19929 char_u patbuf[NUMBUFLEN];
19930 char_u subbuf[NUMBUFLEN];
19931 char_u flagsbuf[NUMBUFLEN];
19932
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019933 char_u *str = get_tv_string_chk(&argvars[0]);
19934 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19935 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19936 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19937
Bram Moolenaar0d660222005-01-07 21:51:51 +000019938 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019939 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19940 rettv->vval.v_string = NULL;
19941 else
19942 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019943}
19944
19945/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019946 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019949f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019950{
19951 int id = 0;
19952#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019953 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019954 long col;
19955 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019956 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019957
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019958 lnum = get_tv_lnum(argvars); /* -1 on type error */
19959 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19960 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019961
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019962 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019963 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019964 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019965#endif
19966
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019967 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019968}
19969
19970/*
19971 * "synIDattr(id, what [, mode])" function
19972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019974f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975{
19976 char_u *p = NULL;
19977#ifdef FEAT_SYN_HL
19978 int id;
19979 char_u *what;
19980 char_u *mode;
19981 char_u modebuf[NUMBUFLEN];
19982 int modec;
19983
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019984 id = get_tv_number(&argvars[0]);
19985 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019986 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019987 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019988 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019989 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019990 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019991 modec = 0; /* replace invalid with current */
19992 }
19993 else
19994 {
19995#ifdef FEAT_GUI
19996 if (gui.in_use)
19997 modec = 'g';
19998 else
19999#endif
20000 if (t_colors > 1)
20001 modec = 'c';
20002 else
20003 modec = 't';
20004 }
20005
20006
20007 switch (TOLOWER_ASC(what[0]))
20008 {
20009 case 'b':
20010 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20011 p = highlight_color(id, what, modec);
20012 else /* bold */
20013 p = highlight_has_attr(id, HL_BOLD, modec);
20014 break;
20015
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020016 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020017 p = highlight_color(id, what, modec);
20018 break;
20019
20020 case 'i':
20021 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20022 p = highlight_has_attr(id, HL_INVERSE, modec);
20023 else /* italic */
20024 p = highlight_has_attr(id, HL_ITALIC, modec);
20025 break;
20026
20027 case 'n': /* name */
20028 p = get_highlight_name(NULL, id - 1);
20029 break;
20030
20031 case 'r': /* reverse */
20032 p = highlight_has_attr(id, HL_INVERSE, modec);
20033 break;
20034
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020035 case 's':
20036 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20037 p = highlight_color(id, what, modec);
20038 else /* standout */
20039 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020040 break;
20041
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020042 case 'u':
20043 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20044 /* underline */
20045 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20046 else
20047 /* undercurl */
20048 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049 break;
20050 }
20051
20052 if (p != NULL)
20053 p = vim_strsave(p);
20054#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020055 rettv->v_type = VAR_STRING;
20056 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057}
20058
20059/*
20060 * "synIDtrans(id)" function
20061 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020063f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064{
20065 int id;
20066
20067#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020068 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069
20070 if (id > 0)
20071 id = syn_get_final_id(id);
20072 else
20073#endif
20074 id = 0;
20075
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020076 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020077}
20078
20079/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020080 * "synconcealed(lnum, col)" function
20081 */
20082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020083f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020084{
20085#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20086 long lnum;
20087 long col;
20088 int syntax_flags = 0;
20089 int cchar;
20090 int matchid = 0;
20091 char_u str[NUMBUFLEN];
20092#endif
20093
20094 rettv->v_type = VAR_LIST;
20095 rettv->vval.v_list = NULL;
20096
20097#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20098 lnum = get_tv_lnum(argvars); /* -1 on type error */
20099 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20100
20101 vim_memset(str, NUL, sizeof(str));
20102
20103 if (rettv_list_alloc(rettv) != FAIL)
20104 {
20105 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20106 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20107 && curwin->w_p_cole > 0)
20108 {
20109 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20110 syntax_flags = get_syntax_info(&matchid);
20111
20112 /* get the conceal character */
20113 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20114 {
20115 cchar = syn_get_sub_char();
20116 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20117 cchar = lcs_conceal;
20118 if (cchar != NUL)
20119 {
20120# ifdef FEAT_MBYTE
20121 if (has_mbyte)
20122 (*mb_char2bytes)(cchar, str);
20123 else
20124# endif
20125 str[0] = cchar;
20126 }
20127 }
20128 }
20129
20130 list_append_number(rettv->vval.v_list,
20131 (syntax_flags & HL_CONCEAL) != 0);
20132 /* -1 to auto-determine strlen */
20133 list_append_string(rettv->vval.v_list, str, -1);
20134 list_append_number(rettv->vval.v_list, matchid);
20135 }
20136#endif
20137}
20138
20139/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020140 * "synstack(lnum, col)" function
20141 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020143f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020144{
20145#ifdef FEAT_SYN_HL
20146 long lnum;
20147 long col;
20148 int i;
20149 int id;
20150#endif
20151
20152 rettv->v_type = VAR_LIST;
20153 rettv->vval.v_list = NULL;
20154
20155#ifdef FEAT_SYN_HL
20156 lnum = get_tv_lnum(argvars); /* -1 on type error */
20157 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20158
20159 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020160 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020161 && rettv_list_alloc(rettv) != FAIL)
20162 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020163 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020164 for (i = 0; ; ++i)
20165 {
20166 id = syn_get_stack_item(i);
20167 if (id < 0)
20168 break;
20169 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20170 break;
20171 }
20172 }
20173#endif
20174}
20175
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020177get_cmd_output_as_rettv(
20178 typval_T *argvars,
20179 typval_T *rettv,
20180 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020181{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020182 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020183 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020184 char_u *infile = NULL;
20185 char_u buf[NUMBUFLEN];
20186 int err = FALSE;
20187 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020188 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020189 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020190
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020191 rettv->v_type = VAR_STRING;
20192 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020193 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020194 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020195
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020196 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020197 {
20198 /*
20199 * Write the string to a temp file, to be used for input of the shell
20200 * command.
20201 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020202 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020203 {
20204 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020205 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020206 }
20207
20208 fd = mch_fopen((char *)infile, WRITEBIN);
20209 if (fd == NULL)
20210 {
20211 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020212 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020213 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020214 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020215 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020216 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20217 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020218 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020219 else
20220 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020221 size_t len;
20222
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020223 p = get_tv_string_buf_chk(&argvars[1], buf);
20224 if (p == NULL)
20225 {
20226 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020227 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020228 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020229 len = STRLEN(p);
20230 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020231 err = TRUE;
20232 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020233 if (fclose(fd) != 0)
20234 err = TRUE;
20235 if (err)
20236 {
20237 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020238 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020239 }
20240 }
20241
Bram Moolenaar52a72462014-08-29 15:53:52 +020020242 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20243 * echoes typeahead, that messes up the display. */
20244 if (!msg_silent)
20245 flags += SHELL_COOKED;
20246
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020247 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020249 int len;
20250 listitem_T *li;
20251 char_u *s = NULL;
20252 char_u *start;
20253 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020254 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020255
Bram Moolenaar52a72462014-08-29 15:53:52 +020020256 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020257 if (res == NULL)
20258 goto errret;
20259
20260 list = list_alloc();
20261 if (list == NULL)
20262 goto errret;
20263
20264 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020265 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020266 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020267 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020268 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020269 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020270
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020271 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020272 if (s == NULL)
20273 goto errret;
20274
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020275 for (p = s; start < end; ++p, ++start)
20276 *p = *start == NUL ? NL : *start;
20277 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020278
20279 li = listitem_alloc();
20280 if (li == NULL)
20281 {
20282 vim_free(s);
20283 goto errret;
20284 }
20285 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020286 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020287 li->li_tv.vval.v_string = s;
20288 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020289 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020290
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020291 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020292 rettv->v_type = VAR_LIST;
20293 rettv->vval.v_list = list;
20294 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020296 else
20297 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020298 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020299#ifdef USE_CR
20300 /* translate <CR> into <NL> */
20301 if (res != NULL)
20302 {
20303 char_u *s;
20304
20305 for (s = res; *s; ++s)
20306 {
20307 if (*s == CAR)
20308 *s = NL;
20309 }
20310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020311#else
20312# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020313 /* translate <CR><NL> into <NL> */
20314 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020315 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020316 char_u *s, *d;
20317
20318 d = res;
20319 for (s = res; *s; ++s)
20320 {
20321 if (s[0] == CAR && s[1] == NL)
20322 ++s;
20323 *d++ = *s;
20324 }
20325 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327# endif
20328#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020329 rettv->vval.v_string = res;
20330 res = NULL;
20331 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020332
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020333errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020334 if (infile != NULL)
20335 {
20336 mch_remove(infile);
20337 vim_free(infile);
20338 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020339 if (res != NULL)
20340 vim_free(res);
20341 if (list != NULL)
20342 list_free(list, TRUE);
20343}
20344
20345/*
20346 * "system()" function
20347 */
20348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020349f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020350{
20351 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20352}
20353
20354/*
20355 * "systemlist()" function
20356 */
20357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020358f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020359{
20360 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020361}
20362
20363/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020364 * "tabpagebuflist()" function
20365 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020367f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020368{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020369#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020370 tabpage_T *tp;
20371 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020372
20373 if (argvars[0].v_type == VAR_UNKNOWN)
20374 wp = firstwin;
20375 else
20376 {
20377 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20378 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020379 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020380 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020381 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020382 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020383 for (; wp != NULL; wp = wp->w_next)
20384 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020385 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020386 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020387 }
20388#endif
20389}
20390
20391
20392/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020393 * "tabpagenr()" function
20394 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020396f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020397{
20398 int nr = 1;
20399#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020400 char_u *arg;
20401
20402 if (argvars[0].v_type != VAR_UNKNOWN)
20403 {
20404 arg = get_tv_string_chk(&argvars[0]);
20405 nr = 0;
20406 if (arg != NULL)
20407 {
20408 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020409 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020410 else
20411 EMSG2(_(e_invexpr2), arg);
20412 }
20413 }
20414 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020415 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020416#endif
20417 rettv->vval.v_number = nr;
20418}
20419
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020420
20421#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020422static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020423
20424/*
20425 * Common code for tabpagewinnr() and winnr().
20426 */
20427 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020428get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020429{
20430 win_T *twin;
20431 int nr = 1;
20432 win_T *wp;
20433 char_u *arg;
20434
20435 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20436 if (argvar->v_type != VAR_UNKNOWN)
20437 {
20438 arg = get_tv_string_chk(argvar);
20439 if (arg == NULL)
20440 nr = 0; /* type error; errmsg already given */
20441 else if (STRCMP(arg, "$") == 0)
20442 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20443 else if (STRCMP(arg, "#") == 0)
20444 {
20445 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20446 if (twin == NULL)
20447 nr = 0;
20448 }
20449 else
20450 {
20451 EMSG2(_(e_invexpr2), arg);
20452 nr = 0;
20453 }
20454 }
20455
20456 if (nr > 0)
20457 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20458 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020459 {
20460 if (wp == NULL)
20461 {
20462 /* didn't find it in this tabpage */
20463 nr = 0;
20464 break;
20465 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020466 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020467 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020468 return nr;
20469}
20470#endif
20471
20472/*
20473 * "tabpagewinnr()" function
20474 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020476f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020477{
20478 int nr = 1;
20479#ifdef FEAT_WINDOWS
20480 tabpage_T *tp;
20481
20482 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20483 if (tp == NULL)
20484 nr = 0;
20485 else
20486 nr = get_winnr(tp, &argvars[1]);
20487#endif
20488 rettv->vval.v_number = nr;
20489}
20490
20491
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020492/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020493 * "tagfiles()" function
20494 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020496f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020497{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020498 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020499 tagname_T tn;
20500 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020501
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020502 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020503 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020504 fname = alloc(MAXPATHL);
20505 if (fname == NULL)
20506 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020507
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020508 for (first = TRUE; ; first = FALSE)
20509 if (get_tagfname(&tn, first, fname) == FAIL
20510 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020511 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020512 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020513 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020514}
20515
20516/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020517 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020518 */
20519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020520f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020521{
20522 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020523
20524 tag_pattern = get_tv_string(&argvars[0]);
20525
20526 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020527 if (*tag_pattern == NUL)
20528 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020529
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020530 if (rettv_list_alloc(rettv) == OK)
20531 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020532}
20533
20534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020535 * "tempname()" function
20536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020538f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539{
20540 static int x = 'A';
20541
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020542 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020543 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020544
20545 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20546 * names. Skip 'I' and 'O', they are used for shell redirection. */
20547 do
20548 {
20549 if (x == 'Z')
20550 x = '0';
20551 else if (x == '9')
20552 x = 'A';
20553 else
20554 {
20555#ifdef EBCDIC
20556 if (x == 'I')
20557 x = 'J';
20558 else if (x == 'R')
20559 x = 'S';
20560 else
20561#endif
20562 ++x;
20563 }
20564 } while (x == 'I' || x == 'O');
20565}
20566
20567/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020568 * "test(list)" function: Just checking the walls...
20569 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020570 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020571f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020572{
20573 /* Used for unit testing. Change the code below to your liking. */
20574#if 0
20575 listitem_T *li;
20576 list_T *l;
20577 char_u *bad, *good;
20578
20579 if (argvars[0].v_type != VAR_LIST)
20580 return;
20581 l = argvars[0].vval.v_list;
20582 if (l == NULL)
20583 return;
20584 li = l->lv_first;
20585 if (li == NULL)
20586 return;
20587 bad = get_tv_string(&li->li_tv);
20588 li = li->li_next;
20589 if (li == NULL)
20590 return;
20591 good = get_tv_string(&li->li_tv);
20592 rettv->vval.v_number = test_edit_score(bad, good);
20593#endif
20594}
20595
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020596#ifdef FEAT_FLOAT
20597/*
20598 * "tan()" function
20599 */
20600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020601f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020602{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020603 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020604
20605 rettv->v_type = VAR_FLOAT;
20606 if (get_float_arg(argvars, &f) == OK)
20607 rettv->vval.v_float = tan(f);
20608 else
20609 rettv->vval.v_float = 0.0;
20610}
20611
20612/*
20613 * "tanh()" function
20614 */
20615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020616f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020617{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020618 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020619
20620 rettv->v_type = VAR_FLOAT;
20621 if (get_float_arg(argvars, &f) == OK)
20622 rettv->vval.v_float = tanh(f);
20623 else
20624 rettv->vval.v_float = 0.0;
20625}
20626#endif
20627
Bram Moolenaard52d9742005-08-21 22:20:28 +000020628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020629 * "tolower(string)" function
20630 */
20631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020632f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633{
20634 char_u *p;
20635
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020636 p = vim_strsave(get_tv_string(&argvars[0]));
20637 rettv->v_type = VAR_STRING;
20638 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020639
20640 if (p != NULL)
20641 while (*p != NUL)
20642 {
20643#ifdef FEAT_MBYTE
20644 int l;
20645
20646 if (enc_utf8)
20647 {
20648 int c, lc;
20649
20650 c = utf_ptr2char(p);
20651 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020652 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020653 /* TODO: reallocate string when byte count changes. */
20654 if (utf_char2len(lc) == l)
20655 utf_char2bytes(lc, p);
20656 p += l;
20657 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020658 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020659 p += l; /* skip multi-byte character */
20660 else
20661#endif
20662 {
20663 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20664 ++p;
20665 }
20666 }
20667}
20668
20669/*
20670 * "toupper(string)" function
20671 */
20672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020673f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020674{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020675 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020676 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020677}
20678
20679/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020680 * "tr(string, fromstr, tostr)" function
20681 */
20682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020683f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020684{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020685 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020686 char_u *fromstr;
20687 char_u *tostr;
20688 char_u *p;
20689#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020690 int inlen;
20691 int fromlen;
20692 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020693 int idx;
20694 char_u *cpstr;
20695 int cplen;
20696 int first = TRUE;
20697#endif
20698 char_u buf[NUMBUFLEN];
20699 char_u buf2[NUMBUFLEN];
20700 garray_T ga;
20701
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020702 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020703 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20704 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020705
20706 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020707 rettv->v_type = VAR_STRING;
20708 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020709 if (fromstr == NULL || tostr == NULL)
20710 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020711 ga_init2(&ga, (int)sizeof(char), 80);
20712
20713#ifdef FEAT_MBYTE
20714 if (!has_mbyte)
20715#endif
20716 /* not multi-byte: fromstr and tostr must be the same length */
20717 if (STRLEN(fromstr) != STRLEN(tostr))
20718 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020719#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020720error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020721#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020722 EMSG2(_(e_invarg2), fromstr);
20723 ga_clear(&ga);
20724 return;
20725 }
20726
20727 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020728 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020729 {
20730#ifdef FEAT_MBYTE
20731 if (has_mbyte)
20732 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020733 inlen = (*mb_ptr2len)(in_str);
20734 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020735 cplen = inlen;
20736 idx = 0;
20737 for (p = fromstr; *p != NUL; p += fromlen)
20738 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020739 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020740 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020741 {
20742 for (p = tostr; *p != NUL; p += tolen)
20743 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020744 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020745 if (idx-- == 0)
20746 {
20747 cplen = tolen;
20748 cpstr = p;
20749 break;
20750 }
20751 }
20752 if (*p == NUL) /* tostr is shorter than fromstr */
20753 goto error;
20754 break;
20755 }
20756 ++idx;
20757 }
20758
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020759 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020760 {
20761 /* Check that fromstr and tostr have the same number of
20762 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020763 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020764 first = FALSE;
20765 for (p = tostr; *p != NUL; p += tolen)
20766 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020767 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020768 --idx;
20769 }
20770 if (idx != 0)
20771 goto error;
20772 }
20773
Bram Moolenaarcde88542015-08-11 19:14:00 +020020774 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020775 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020776 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020777
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020778 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020779 }
20780 else
20781#endif
20782 {
20783 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020784 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020785 if (p != NULL)
20786 ga_append(&ga, tostr[p - fromstr]);
20787 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020788 ga_append(&ga, *in_str);
20789 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020790 }
20791 }
20792
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020793 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020794 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020795 ga_append(&ga, NUL);
20796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020797 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020798}
20799
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020800#ifdef FEAT_FLOAT
20801/*
20802 * "trunc({float})" function
20803 */
20804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020805f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020806{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020807 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020808
20809 rettv->v_type = VAR_FLOAT;
20810 if (get_float_arg(argvars, &f) == OK)
20811 /* trunc() is not in C90, use floor() or ceil() instead. */
20812 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20813 else
20814 rettv->vval.v_float = 0.0;
20815}
20816#endif
20817
Bram Moolenaar8299df92004-07-10 09:47:34 +000020818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020819 * "type(expr)" function
20820 */
20821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020822f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020824 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020825
20826 switch (argvars[0].v_type)
20827 {
20828 case VAR_NUMBER: n = 0; break;
20829 case VAR_STRING: n = 1; break;
20830 case VAR_FUNC: n = 2; break;
20831 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020832 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020833 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020834 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020835 if (argvars[0].vval.v_number == VVAL_FALSE
20836 || argvars[0].vval.v_number == VVAL_TRUE)
20837 n = 6;
20838 else
20839 n = 7;
20840 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020841 case VAR_JOB: n = 8; break;
20842 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020843 case VAR_UNKNOWN:
20844 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20845 n = -1;
20846 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020847 }
20848 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020849}
20850
20851/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020852 * "undofile(name)" function
20853 */
20854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020855f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020856{
20857 rettv->v_type = VAR_STRING;
20858#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020859 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020860 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020861
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020862 if (*fname == NUL)
20863 {
20864 /* If there is no file name there will be no undo file. */
20865 rettv->vval.v_string = NULL;
20866 }
20867 else
20868 {
20869 char_u *ffname = FullName_save(fname, FALSE);
20870
20871 if (ffname != NULL)
20872 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20873 vim_free(ffname);
20874 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020875 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020876#else
20877 rettv->vval.v_string = NULL;
20878#endif
20879}
20880
20881/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020882 * "undotree()" function
20883 */
20884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020885f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020886{
20887 if (rettv_dict_alloc(rettv) == OK)
20888 {
20889 dict_T *dict = rettv->vval.v_dict;
20890 list_T *list;
20891
Bram Moolenaar730cde92010-06-27 05:18:54 +020020892 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020893 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020894 dict_add_nr_str(dict, "save_last",
20895 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020896 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20897 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020898 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020899
20900 list = list_alloc();
20901 if (list != NULL)
20902 {
20903 u_eval_tree(curbuf->b_u_oldhead, list);
20904 dict_add_list(dict, "entries", list);
20905 }
20906 }
20907}
20908
20909/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020910 * "values(dict)" function
20911 */
20912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020913f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020914{
20915 dict_list(argvars, rettv, 1);
20916}
20917
20918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020919 * "virtcol(string)" function
20920 */
20921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020922f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020923{
20924 colnr_T vcol = 0;
20925 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020926 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020927
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020928 fp = var2fpos(&argvars[0], FALSE, &fnum);
20929 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20930 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020931 {
20932 getvvcol(curwin, fp, NULL, NULL, &vcol);
20933 ++vcol;
20934 }
20935
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020936 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020937}
20938
20939/*
20940 * "visualmode()" function
20941 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020943f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020944{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020945 char_u str[2];
20946
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020947 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020948 str[0] = curbuf->b_visual_mode_eval;
20949 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020950 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020951
20952 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020953 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020955}
20956
20957/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020958 * "wildmenumode()" function
20959 */
20960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020961f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020962{
20963#ifdef FEAT_WILDMENU
20964 if (wild_menu_showing)
20965 rettv->vval.v_number = 1;
20966#endif
20967}
20968
20969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020970 * "winbufnr(nr)" function
20971 */
20972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020973f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020974{
20975 win_T *wp;
20976
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020977 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020978 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020979 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020980 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020981 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020982}
20983
20984/*
20985 * "wincol()" function
20986 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020988f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989{
20990 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020991 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020992}
20993
20994/*
20995 * "winheight(nr)" function
20996 */
20997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020998f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020999{
21000 win_T *wp;
21001
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021002 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021003 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021004 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021006 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007}
21008
21009/*
21010 * "winline()" function
21011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021013f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021014{
21015 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021016 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017}
21018
21019/*
21020 * "winnr()" function
21021 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021023f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021024{
21025 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021026
Bram Moolenaar071d4272004-06-13 20:20:40 +000021027#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021028 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021029#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021030 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021031}
21032
21033/*
21034 * "winrestcmd()" function
21035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021037f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021038{
21039#ifdef FEAT_WINDOWS
21040 win_T *wp;
21041 int winnr = 1;
21042 garray_T ga;
21043 char_u buf[50];
21044
21045 ga_init2(&ga, (int)sizeof(char), 70);
21046 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21047 {
21048 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21049 ga_concat(&ga, buf);
21050# ifdef FEAT_VERTSPLIT
21051 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21052 ga_concat(&ga, buf);
21053# endif
21054 ++winnr;
21055 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021056 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021058 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021059#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021060 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021061#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021062 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063}
21064
21065/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021066 * "winrestview()" function
21067 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021069f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021070{
21071 dict_T *dict;
21072
21073 if (argvars[0].v_type != VAR_DICT
21074 || (dict = argvars[0].vval.v_dict) == NULL)
21075 EMSG(_(e_invarg));
21076 else
21077 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021078 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21079 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21080 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21081 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021082#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021083 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21084 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021085#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021086 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21087 {
21088 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21089 curwin->w_set_curswant = FALSE;
21090 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021091
Bram Moolenaar82c25852014-05-28 16:47:16 +020021092 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21093 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021094#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021095 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21096 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021097#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021098 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21099 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21100 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21101 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021102
21103 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021104 win_new_height(curwin, curwin->w_height);
21105# ifdef FEAT_VERTSPLIT
21106 win_new_width(curwin, W_WIDTH(curwin));
21107# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021108 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021109
Bram Moolenaarb851a962014-10-31 15:45:52 +010021110 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021111 curwin->w_topline = 1;
21112 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21113 curwin->w_topline = curbuf->b_ml.ml_line_count;
21114#ifdef FEAT_DIFF
21115 check_topfill(curwin, TRUE);
21116#endif
21117 }
21118}
21119
21120/*
21121 * "winsaveview()" function
21122 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021124f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021125{
21126 dict_T *dict;
21127
Bram Moolenaara800b422010-06-27 01:15:55 +020021128 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021129 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021130 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021131
21132 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21133 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21134#ifdef FEAT_VIRTUALEDIT
21135 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21136#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021137 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021138 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21139
21140 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21141#ifdef FEAT_DIFF
21142 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21143#endif
21144 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21145 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21146}
21147
21148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149 * "winwidth(nr)" function
21150 */
21151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021152f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021153{
21154 win_T *wp;
21155
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021156 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021157 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021158 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159 else
21160#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021161 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021163 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021164#endif
21165}
21166
Bram Moolenaar071d4272004-06-13 20:20:40 +000021167/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021168 * "wordcount()" function
21169 */
21170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021171f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021172{
21173 if (rettv_dict_alloc(rettv) == FAIL)
21174 return;
21175 cursor_pos_info(rettv->vval.v_dict);
21176}
21177
21178/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021179 * Write list of strings to file
21180 */
21181 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021182write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021183{
21184 listitem_T *li;
21185 int c;
21186 int ret = OK;
21187 char_u *s;
21188
21189 for (li = list->lv_first; li != NULL; li = li->li_next)
21190 {
21191 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21192 {
21193 if (*s == '\n')
21194 c = putc(NUL, fd);
21195 else
21196 c = putc(*s, fd);
21197 if (c == EOF)
21198 {
21199 ret = FAIL;
21200 break;
21201 }
21202 }
21203 if (!binary || li->li_next != NULL)
21204 if (putc('\n', fd) == EOF)
21205 {
21206 ret = FAIL;
21207 break;
21208 }
21209 if (ret == FAIL)
21210 {
21211 EMSG(_(e_write));
21212 break;
21213 }
21214 }
21215 return ret;
21216}
21217
21218/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021219 * "writefile()" function
21220 */
21221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021222f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021223{
21224 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021225 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021226 char_u *fname;
21227 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021228 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021229
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021230 if (check_restricted() || check_secure())
21231 return;
21232
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021233 if (argvars[0].v_type != VAR_LIST)
21234 {
21235 EMSG2(_(e_listarg), "writefile()");
21236 return;
21237 }
21238 if (argvars[0].vval.v_list == NULL)
21239 return;
21240
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021241 if (argvars[2].v_type != VAR_UNKNOWN)
21242 {
21243 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21244 binary = TRUE;
21245 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21246 append = TRUE;
21247 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021248
21249 /* Always open the file in binary mode, library functions have a mind of
21250 * their own about CR-LF conversion. */
21251 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021252 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21253 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021254 {
21255 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21256 ret = -1;
21257 }
21258 else
21259 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021260 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21261 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021262 fclose(fd);
21263 }
21264
21265 rettv->vval.v_number = ret;
21266}
21267
21268/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021269 * "xor(expr, expr)" function
21270 */
21271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021272f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021273{
21274 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21275 ^ get_tv_number_chk(&argvars[1], NULL);
21276}
21277
21278
21279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021280 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021281 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282 */
21283 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021284var2fpos(
21285 typval_T *varp,
21286 int dollar_lnum, /* TRUE when $ is last line */
21287 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021288{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021289 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021290 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021291 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021292
Bram Moolenaara5525202006-03-02 22:52:09 +000021293 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021294 if (varp->v_type == VAR_LIST)
21295 {
21296 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021297 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021298 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021299 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021300
21301 l = varp->vval.v_list;
21302 if (l == NULL)
21303 return NULL;
21304
21305 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021306 pos.lnum = list_find_nr(l, 0L, &error);
21307 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021308 return NULL; /* invalid line number */
21309
21310 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021311 pos.col = list_find_nr(l, 1L, &error);
21312 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021313 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021314 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021315
21316 /* We accept "$" for the column number: last column. */
21317 li = list_find(l, 1L);
21318 if (li != NULL && li->li_tv.v_type == VAR_STRING
21319 && li->li_tv.vval.v_string != NULL
21320 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21321 pos.col = len + 1;
21322
Bram Moolenaara5525202006-03-02 22:52:09 +000021323 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021324 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021325 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021326 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021327
Bram Moolenaara5525202006-03-02 22:52:09 +000021328#ifdef FEAT_VIRTUALEDIT
21329 /* Get the virtual offset. Defaults to zero. */
21330 pos.coladd = list_find_nr(l, 2L, &error);
21331 if (error)
21332 pos.coladd = 0;
21333#endif
21334
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021335 return &pos;
21336 }
21337
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021338 name = get_tv_string_chk(varp);
21339 if (name == NULL)
21340 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021341 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021342 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021343 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21344 {
21345 if (VIsual_active)
21346 return &VIsual;
21347 return &curwin->w_cursor;
21348 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021349 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021351 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021352 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21353 return NULL;
21354 return pp;
21355 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021356
21357#ifdef FEAT_VIRTUALEDIT
21358 pos.coladd = 0;
21359#endif
21360
Bram Moolenaar477933c2007-07-17 14:32:23 +000021361 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021362 {
21363 pos.col = 0;
21364 if (name[1] == '0') /* "w0": first visible line */
21365 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021366 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021367 pos.lnum = curwin->w_topline;
21368 return &pos;
21369 }
21370 else if (name[1] == '$') /* "w$": last visible line */
21371 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021372 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021373 pos.lnum = curwin->w_botline - 1;
21374 return &pos;
21375 }
21376 }
21377 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021378 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021379 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021380 {
21381 pos.lnum = curbuf->b_ml.ml_line_count;
21382 pos.col = 0;
21383 }
21384 else
21385 {
21386 pos.lnum = curwin->w_cursor.lnum;
21387 pos.col = (colnr_T)STRLEN(ml_get_curline());
21388 }
21389 return &pos;
21390 }
21391 return NULL;
21392}
21393
21394/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021395 * Convert list in "arg" into a position and optional file number.
21396 * When "fnump" is NULL there is no file number, only 3 items.
21397 * Note that the column is passed on as-is, the caller may want to decrement
21398 * it to use 1 for the first column.
21399 * Return FAIL when conversion is not possible, doesn't check the position for
21400 * validity.
21401 */
21402 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021403list2fpos(
21404 typval_T *arg,
21405 pos_T *posp,
21406 int *fnump,
21407 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021408{
21409 list_T *l = arg->vval.v_list;
21410 long i = 0;
21411 long n;
21412
Bram Moolenaar493c1782014-05-28 14:34:46 +020021413 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21414 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021415 if (arg->v_type != VAR_LIST
21416 || l == NULL
21417 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021418 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021419 return FAIL;
21420
21421 if (fnump != NULL)
21422 {
21423 n = list_find_nr(l, i++, NULL); /* fnum */
21424 if (n < 0)
21425 return FAIL;
21426 if (n == 0)
21427 n = curbuf->b_fnum; /* current buffer */
21428 *fnump = n;
21429 }
21430
21431 n = list_find_nr(l, i++, NULL); /* lnum */
21432 if (n < 0)
21433 return FAIL;
21434 posp->lnum = n;
21435
21436 n = list_find_nr(l, i++, NULL); /* col */
21437 if (n < 0)
21438 return FAIL;
21439 posp->col = n;
21440
21441#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021442 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021443 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021444 posp->coladd = 0;
21445 else
21446 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021447#endif
21448
Bram Moolenaar493c1782014-05-28 14:34:46 +020021449 if (curswantp != NULL)
21450 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21451
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021452 return OK;
21453}
21454
21455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021456 * Get the length of an environment variable name.
21457 * Advance "arg" to the first character after the name.
21458 * Return 0 for error.
21459 */
21460 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021461get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021462{
21463 char_u *p;
21464 int len;
21465
21466 for (p = *arg; vim_isIDc(*p); ++p)
21467 ;
21468 if (p == *arg) /* no name found */
21469 return 0;
21470
21471 len = (int)(p - *arg);
21472 *arg = p;
21473 return len;
21474}
21475
21476/*
21477 * Get the length of the name of a function or internal variable.
21478 * "arg" is advanced to the first non-white character after the name.
21479 * Return 0 if something is wrong.
21480 */
21481 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021482get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021483{
21484 char_u *p;
21485 int len;
21486
21487 /* Find the end of the name. */
21488 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021489 {
21490 if (*p == ':')
21491 {
21492 /* "s:" is start of "s:var", but "n:" is not and can be used in
21493 * slice "[n:]". Also "xx:" is not a namespace. */
21494 len = (int)(p - *arg);
21495 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21496 || len > 1)
21497 break;
21498 }
21499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021500 if (p == *arg) /* no name found */
21501 return 0;
21502
21503 len = (int)(p - *arg);
21504 *arg = skipwhite(p);
21505
21506 return len;
21507}
21508
21509/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021510 * Get the length of the name of a variable or function.
21511 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021512 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021513 * Return -1 if curly braces expansion failed.
21514 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021515 * If the name contains 'magic' {}'s, expand them and return the
21516 * expanded name in an allocated string via 'alias' - caller must free.
21517 */
21518 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021519get_name_len(
21520 char_u **arg,
21521 char_u **alias,
21522 int evaluate,
21523 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021524{
21525 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021526 char_u *p;
21527 char_u *expr_start;
21528 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529
21530 *alias = NULL; /* default to no alias */
21531
21532 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21533 && (*arg)[2] == (int)KE_SNR)
21534 {
21535 /* hard coded <SNR>, already translated */
21536 *arg += 3;
21537 return get_id_len(arg) + 3;
21538 }
21539 len = eval_fname_script(*arg);
21540 if (len > 0)
21541 {
21542 /* literal "<SID>", "s:" or "<SNR>" */
21543 *arg += len;
21544 }
21545
Bram Moolenaar071d4272004-06-13 20:20:40 +000021546 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021547 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021548 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021549 p = find_name_end(*arg, &expr_start, &expr_end,
21550 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021551 if (expr_start != NULL)
21552 {
21553 char_u *temp_string;
21554
21555 if (!evaluate)
21556 {
21557 len += (int)(p - *arg);
21558 *arg = skipwhite(p);
21559 return len;
21560 }
21561
21562 /*
21563 * Include any <SID> etc in the expanded string:
21564 * Thus the -len here.
21565 */
21566 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21567 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021568 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021569 *alias = temp_string;
21570 *arg = skipwhite(p);
21571 return (int)STRLEN(temp_string);
21572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021573
21574 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021575 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021576 EMSG2(_(e_invexpr2), *arg);
21577
21578 return len;
21579}
21580
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021581/*
21582 * Find the end of a variable or function name, taking care of magic braces.
21583 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21584 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021585 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021586 * Return a pointer to just after the name. Equal to "arg" if there is no
21587 * valid name.
21588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021589 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021590find_name_end(
21591 char_u *arg,
21592 char_u **expr_start,
21593 char_u **expr_end,
21594 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021595{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021596 int mb_nest = 0;
21597 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021598 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021599 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021600
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021601 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021603 *expr_start = NULL;
21604 *expr_end = NULL;
21605 }
21606
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021607 /* Quick check for valid starting character. */
21608 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21609 return arg;
21610
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021611 for (p = arg; *p != NUL
21612 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021613 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021614 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021615 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021616 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021617 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021618 if (*p == '\'')
21619 {
21620 /* skip over 'string' to avoid counting [ and ] inside it. */
21621 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21622 ;
21623 if (*p == NUL)
21624 break;
21625 }
21626 else if (*p == '"')
21627 {
21628 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21629 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21630 if (*p == '\\' && p[1] != NUL)
21631 ++p;
21632 if (*p == NUL)
21633 break;
21634 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021635 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21636 {
21637 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021638 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021639 len = (int)(p - arg);
21640 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021641 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021642 break;
21643 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021644
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021645 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021646 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021647 if (*p == '[')
21648 ++br_nest;
21649 else if (*p == ']')
21650 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021652
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021653 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021655 if (*p == '{')
21656 {
21657 mb_nest++;
21658 if (expr_start != NULL && *expr_start == NULL)
21659 *expr_start = p;
21660 }
21661 else if (*p == '}')
21662 {
21663 mb_nest--;
21664 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21665 *expr_end = p;
21666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021668 }
21669
21670 return p;
21671}
21672
21673/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021674 * Expands out the 'magic' {}'s in a variable/function name.
21675 * Note that this can call itself recursively, to deal with
21676 * constructs like foo{bar}{baz}{bam}
21677 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21678 * "in_start" ^
21679 * "expr_start" ^
21680 * "expr_end" ^
21681 * "in_end" ^
21682 *
21683 * Returns a new allocated string, which the caller must free.
21684 * Returns NULL for failure.
21685 */
21686 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021687make_expanded_name(
21688 char_u *in_start,
21689 char_u *expr_start,
21690 char_u *expr_end,
21691 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021692{
21693 char_u c1;
21694 char_u *retval = NULL;
21695 char_u *temp_result;
21696 char_u *nextcmd = NULL;
21697
21698 if (expr_end == NULL || in_end == NULL)
21699 return NULL;
21700 *expr_start = NUL;
21701 *expr_end = NUL;
21702 c1 = *in_end;
21703 *in_end = NUL;
21704
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021705 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021706 if (temp_result != NULL && nextcmd == NULL)
21707 {
21708 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21709 + (in_end - expr_end) + 1));
21710 if (retval != NULL)
21711 {
21712 STRCPY(retval, in_start);
21713 STRCAT(retval, temp_result);
21714 STRCAT(retval, expr_end + 1);
21715 }
21716 }
21717 vim_free(temp_result);
21718
21719 *in_end = c1; /* put char back for error messages */
21720 *expr_start = '{';
21721 *expr_end = '}';
21722
21723 if (retval != NULL)
21724 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021725 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021726 if (expr_start != NULL)
21727 {
21728 /* Further expansion! */
21729 temp_result = make_expanded_name(retval, expr_start,
21730 expr_end, temp_result);
21731 vim_free(retval);
21732 retval = temp_result;
21733 }
21734 }
21735
21736 return retval;
21737}
21738
21739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021741 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021742 */
21743 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021744eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021745{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021746 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21747}
21748
21749/*
21750 * Return TRUE if character "c" can be used as the first character in a
21751 * variable or function name (excluding '{' and '}').
21752 */
21753 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021754eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021755{
21756 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757}
21758
21759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021760 * Set number v: variable to "val".
21761 */
21762 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021763set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021764{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021765 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021766}
21767
21768/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021769 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021770 */
21771 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021772get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021773{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021774 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021775}
21776
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021777/*
21778 * Get string v: variable value. Uses a static buffer, can only be used once.
21779 */
21780 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021781get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021782{
21783 return get_tv_string(&vimvars[idx].vv_tv);
21784}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021785
Bram Moolenaar071d4272004-06-13 20:20:40 +000021786/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021787 * Get List v: variable value. Caller must take care of reference count when
21788 * needed.
21789 */
21790 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021791get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021792{
21793 return vimvars[idx].vv_list;
21794}
21795
21796/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021797 * Set v:char to character "c".
21798 */
21799 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021800set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021801{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021802 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021803
21804#ifdef FEAT_MBYTE
21805 if (has_mbyte)
21806 buf[(*mb_char2bytes)(c, buf)] = NUL;
21807 else
21808#endif
21809 {
21810 buf[0] = c;
21811 buf[1] = NUL;
21812 }
21813 set_vim_var_string(VV_CHAR, buf, -1);
21814}
21815
21816/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021817 * Set v:count to "count" and v:count1 to "count1".
21818 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021819 */
21820 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021821set_vcount(
21822 long count,
21823 long count1,
21824 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021825{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021826 if (set_prevcount)
21827 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021828 vimvars[VV_COUNT].vv_nr = count;
21829 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021830}
21831
21832/*
21833 * Set string v: variable to a copy of "val".
21834 */
21835 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021836set_vim_var_string(
21837 int idx,
21838 char_u *val,
21839 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021840{
Bram Moolenaara542c682016-01-31 16:28:04 +010021841 clear_tv(&vimvars[idx].vv_di.di_tv);
21842 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021843 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021844 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021845 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021846 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021848 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021849}
21850
21851/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021852 * Set List v: variable to "val".
21853 */
21854 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021855set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021856{
Bram Moolenaara542c682016-01-31 16:28:04 +010021857 clear_tv(&vimvars[idx].vv_di.di_tv);
21858 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021859 vimvars[idx].vv_list = val;
21860 if (val != NULL)
21861 ++val->lv_refcount;
21862}
21863
21864/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021865 * Set Dictionary v: variable to "val".
21866 */
21867 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021868set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021869{
21870 int todo;
21871 hashitem_T *hi;
21872
Bram Moolenaara542c682016-01-31 16:28:04 +010021873 clear_tv(&vimvars[idx].vv_di.di_tv);
21874 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021875 vimvars[idx].vv_dict = val;
21876 if (val != NULL)
21877 {
21878 ++val->dv_refcount;
21879
21880 /* Set readonly */
21881 todo = (int)val->dv_hashtab.ht_used;
21882 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21883 {
21884 if (HASHITEM_EMPTY(hi))
21885 continue;
21886 --todo;
21887 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21888 }
21889 }
21890}
21891
21892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893 * Set v:register if needed.
21894 */
21895 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021896set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021897{
21898 char_u regname;
21899
21900 if (c == 0 || c == ' ')
21901 regname = '"';
21902 else
21903 regname = c;
21904 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021905 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906 set_vim_var_string(VV_REG, &regname, 1);
21907}
21908
21909/*
21910 * Get or set v:exception. If "oldval" == NULL, return the current value.
21911 * Otherwise, restore the value to "oldval" and return NULL.
21912 * Must always be called in pairs to save and restore v:exception! Does not
21913 * take care of memory allocations.
21914 */
21915 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021916v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021917{
21918 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021919 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920
Bram Moolenaare9a41262005-01-15 22:18:47 +000021921 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021922 return NULL;
21923}
21924
21925/*
21926 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21927 * Otherwise, restore the value to "oldval" and return NULL.
21928 * Must always be called in pairs to save and restore v:throwpoint! Does not
21929 * take care of memory allocations.
21930 */
21931 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021932v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933{
21934 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021935 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021936
Bram Moolenaare9a41262005-01-15 22:18:47 +000021937 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938 return NULL;
21939}
21940
21941#if defined(FEAT_AUTOCMD) || defined(PROTO)
21942/*
21943 * Set v:cmdarg.
21944 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21945 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21946 * Must always be called in pairs!
21947 */
21948 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021949set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021950{
21951 char_u *oldval;
21952 char_u *newval;
21953 unsigned len;
21954
Bram Moolenaare9a41262005-01-15 22:18:47 +000021955 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021956 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021957 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021958 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021959 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021960 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021961 }
21962
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021963 if (eap->force_bin == FORCE_BIN)
21964 len = 6;
21965 else if (eap->force_bin == FORCE_NOBIN)
21966 len = 8;
21967 else
21968 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021969
21970 if (eap->read_edit)
21971 len += 7;
21972
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021973 if (eap->force_ff != 0)
21974 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21975# ifdef FEAT_MBYTE
21976 if (eap->force_enc != 0)
21977 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021978 if (eap->bad_char != 0)
21979 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021980# endif
21981
21982 newval = alloc(len + 1);
21983 if (newval == NULL)
21984 return NULL;
21985
21986 if (eap->force_bin == FORCE_BIN)
21987 sprintf((char *)newval, " ++bin");
21988 else if (eap->force_bin == FORCE_NOBIN)
21989 sprintf((char *)newval, " ++nobin");
21990 else
21991 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021992
21993 if (eap->read_edit)
21994 STRCAT(newval, " ++edit");
21995
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021996 if (eap->force_ff != 0)
21997 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21998 eap->cmd + eap->force_ff);
21999# ifdef FEAT_MBYTE
22000 if (eap->force_enc != 0)
22001 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22002 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022003 if (eap->bad_char == BAD_KEEP)
22004 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22005 else if (eap->bad_char == BAD_DROP)
22006 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22007 else if (eap->bad_char != 0)
22008 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022009# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022010 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022011 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022012}
22013#endif
22014
22015/*
22016 * Get the value of internal variable "name".
22017 * Return OK or FAIL.
22018 */
22019 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022020get_var_tv(
22021 char_u *name,
22022 int len, /* length of "name" */
22023 typval_T *rettv, /* NULL when only checking existence */
22024 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22025 int verbose, /* may give error message */
22026 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022027{
22028 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022029 typval_T *tv = NULL;
22030 typval_T atv;
22031 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022032 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022033
22034 /* truncate the name, so that we can use strcmp() */
22035 cc = name[len];
22036 name[len] = NUL;
22037
22038 /*
22039 * Check for "b:changedtick".
22040 */
22041 if (STRCMP(name, "b:changedtick") == 0)
22042 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022043 atv.v_type = VAR_NUMBER;
22044 atv.vval.v_number = curbuf->b_changedtick;
22045 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046 }
22047
22048 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022049 * Check for user-defined variables.
22050 */
22051 else
22052 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022053 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022055 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022056 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022057 if (dip != NULL)
22058 *dip = v;
22059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022060 }
22061
Bram Moolenaare9a41262005-01-15 22:18:47 +000022062 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022064 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022065 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022066 ret = FAIL;
22067 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022068 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022069 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022070
22071 name[len] = cc;
22072
22073 return ret;
22074}
22075
22076/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022077 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22078 * Also handle function call with Funcref variable: func(expr)
22079 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22080 */
22081 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022082handle_subscript(
22083 char_u **arg,
22084 typval_T *rettv,
22085 int evaluate, /* do more than finding the end */
22086 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022087{
22088 int ret = OK;
22089 dict_T *selfdict = NULL;
22090 char_u *s;
22091 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022092 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022093
22094 while (ret == OK
22095 && (**arg == '['
22096 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022097 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022098 && !vim_iswhite(*(*arg - 1)))
22099 {
22100 if (**arg == '(')
22101 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000022102 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022103 if (evaluate)
22104 {
22105 functv = *rettv;
22106 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022107
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022108 /* Invoke the function. Recursive! */
22109 s = functv.vval.v_string;
22110 }
22111 else
22112 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022113 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022114 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
22115 &len, evaluate, selfdict);
22116
22117 /* Clear the funcref afterwards, so that deleting it while
22118 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022119 if (evaluate)
22120 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022121
22122 /* Stop the expression evaluation when immediately aborting on
22123 * error, or when an interrupt occurred or an exception was thrown
22124 * but not caught. */
22125 if (aborting())
22126 {
22127 if (ret == OK)
22128 clear_tv(rettv);
22129 ret = FAIL;
22130 }
22131 dict_unref(selfdict);
22132 selfdict = NULL;
22133 }
22134 else /* **arg == '[' || **arg == '.' */
22135 {
22136 dict_unref(selfdict);
22137 if (rettv->v_type == VAR_DICT)
22138 {
22139 selfdict = rettv->vval.v_dict;
22140 if (selfdict != NULL)
22141 ++selfdict->dv_refcount;
22142 }
22143 else
22144 selfdict = NULL;
22145 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22146 {
22147 clear_tv(rettv);
22148 ret = FAIL;
22149 }
22150 }
22151 }
22152 dict_unref(selfdict);
22153 return ret;
22154}
22155
22156/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022157 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022158 * value).
22159 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022160 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022161alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022162{
Bram Moolenaar33570922005-01-25 22:26:29 +000022163 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022164}
22165
22166/*
22167 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168 * The string "s" must have been allocated, it is consumed.
22169 * Return NULL for out of memory, the variable otherwise.
22170 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022171 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022172alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022173{
Bram Moolenaar33570922005-01-25 22:26:29 +000022174 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022176 rettv = alloc_tv();
22177 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022178 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022179 rettv->v_type = VAR_STRING;
22180 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022181 }
22182 else
22183 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022184 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022185}
22186
22187/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022188 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022189 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022190 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022191free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022192{
22193 if (varp != NULL)
22194 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022195 switch (varp->v_type)
22196 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022197 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022198 func_unref(varp->vval.v_string);
22199 /*FALLTHROUGH*/
22200 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022201 vim_free(varp->vval.v_string);
22202 break;
22203 case VAR_LIST:
22204 list_unref(varp->vval.v_list);
22205 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022206 case VAR_DICT:
22207 dict_unref(varp->vval.v_dict);
22208 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022209 case VAR_JOB:
22210#ifdef FEAT_JOB
22211 job_unref(varp->vval.v_job);
22212 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022213#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022214 case VAR_CHANNEL:
22215#ifdef FEAT_CHANNEL
22216 channel_unref(varp->vval.v_channel);
22217 break;
22218#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022219 case VAR_NUMBER:
22220 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022221 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022222 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022223 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022225 vim_free(varp);
22226 }
22227}
22228
22229/*
22230 * Free the memory for a variable value and set the value to NULL or 0.
22231 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022232 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022233clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022234{
22235 if (varp != NULL)
22236 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022237 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022238 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022239 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022240 func_unref(varp->vval.v_string);
22241 /*FALLTHROUGH*/
22242 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022243 vim_free(varp->vval.v_string);
22244 varp->vval.v_string = NULL;
22245 break;
22246 case VAR_LIST:
22247 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022248 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022249 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022250 case VAR_DICT:
22251 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022252 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022253 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022254 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022255 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022256 varp->vval.v_number = 0;
22257 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022258 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022259#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022260 varp->vval.v_float = 0.0;
22261 break;
22262#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022263 case VAR_JOB:
22264#ifdef FEAT_JOB
22265 job_unref(varp->vval.v_job);
22266 varp->vval.v_job = NULL;
22267#endif
22268 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022269 case VAR_CHANNEL:
22270#ifdef FEAT_CHANNEL
22271 channel_unref(varp->vval.v_channel);
22272 varp->vval.v_channel = NULL;
22273#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022274 case VAR_UNKNOWN:
22275 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022277 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278 }
22279}
22280
22281/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022282 * Set the value of a variable to NULL without freeing items.
22283 */
22284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022285init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022286{
22287 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022288 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022289}
22290
22291/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022292 * Get the number value of a variable.
22293 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022294 * For incompatible types, return 0.
22295 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22296 * caller of incompatible types: it sets *denote to TRUE if "denote"
22297 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022298 */
22299 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022300get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022301{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022302 int error = FALSE;
22303
22304 return get_tv_number_chk(varp, &error); /* return 0L on error */
22305}
22306
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022307 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022308get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022309{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022310 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022311
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022312 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022313 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022314 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022315 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022316 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022317#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022318 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022319 break;
22320#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022321 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022322 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022323 break;
22324 case VAR_STRING:
22325 if (varp->vval.v_string != NULL)
22326 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022327 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022328 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022329 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022330 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022331 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022332 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022333 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022334 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022335 case VAR_SPECIAL:
22336 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22337 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022338 case VAR_JOB:
22339#ifdef FEAT_JOB
22340 EMSG(_("E910: Using a Job as a Number"));
22341 break;
22342#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022343 case VAR_CHANNEL:
22344#ifdef FEAT_CHANNEL
22345 EMSG(_("E913: Using a Channel as a Number"));
22346 break;
22347#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022348 case VAR_UNKNOWN:
22349 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022350 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022351 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022352 if (denote == NULL) /* useful for values that must be unsigned */
22353 n = -1;
22354 else
22355 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022356 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022357}
22358
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022359#ifdef FEAT_FLOAT
22360 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022361get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022362{
22363 switch (varp->v_type)
22364 {
22365 case VAR_NUMBER:
22366 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022367 case VAR_FLOAT:
22368 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022369 case VAR_FUNC:
22370 EMSG(_("E891: Using a Funcref as a Float"));
22371 break;
22372 case VAR_STRING:
22373 EMSG(_("E892: Using a String as a Float"));
22374 break;
22375 case VAR_LIST:
22376 EMSG(_("E893: Using a List as a Float"));
22377 break;
22378 case VAR_DICT:
22379 EMSG(_("E894: Using a Dictionary as a Float"));
22380 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022381 case VAR_SPECIAL:
22382 EMSG(_("E907: Using a special value as a Float"));
22383 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022384 case VAR_JOB:
22385# ifdef FEAT_JOB
22386 EMSG(_("E911: Using a Job as a Float"));
22387 break;
22388# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022389 case VAR_CHANNEL:
22390# ifdef FEAT_CHANNEL
22391 EMSG(_("E914: Using a Channel as a Float"));
22392 break;
22393# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022394 case VAR_UNKNOWN:
22395 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022396 break;
22397 }
22398 return 0;
22399}
22400#endif
22401
Bram Moolenaar071d4272004-06-13 20:20:40 +000022402/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022403 * Get the lnum from the first argument.
22404 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022405 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406 */
22407 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022408get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022409{
Bram Moolenaar33570922005-01-25 22:26:29 +000022410 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022411 linenr_T lnum;
22412
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022413 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414 if (lnum == 0) /* no valid number, try using line() */
22415 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022416 rettv.v_type = VAR_NUMBER;
22417 f_line(argvars, &rettv);
22418 lnum = rettv.vval.v_number;
22419 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022420 }
22421 return lnum;
22422}
22423
22424/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022425 * Get the lnum from the first argument.
22426 * Also accepts "$", then "buf" is used.
22427 * Returns 0 on error.
22428 */
22429 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022430get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022431{
22432 if (argvars[0].v_type == VAR_STRING
22433 && argvars[0].vval.v_string != NULL
22434 && argvars[0].vval.v_string[0] == '$'
22435 && buf != NULL)
22436 return buf->b_ml.ml_line_count;
22437 return get_tv_number_chk(&argvars[0], NULL);
22438}
22439
22440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022441 * Get the string value of a variable.
22442 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022443 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22444 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022445 * If the String variable has never been set, return an empty string.
22446 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022447 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22448 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022449 */
22450 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022451get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022452{
22453 static char_u mybuf[NUMBUFLEN];
22454
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022455 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022456}
22457
22458 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022459get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022460{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022461 char_u *res = get_tv_string_buf_chk(varp, buf);
22462
22463 return res != NULL ? res : (char_u *)"";
22464}
22465
Bram Moolenaar7d647822014-04-05 21:28:56 +020022466/*
22467 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22468 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022469 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022470get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022471{
22472 static char_u mybuf[NUMBUFLEN];
22473
22474 return get_tv_string_buf_chk(varp, mybuf);
22475}
22476
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022477 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022478get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022479{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022480 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022481 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022482 case VAR_NUMBER:
22483 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22484 return buf;
22485 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022486 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022487 break;
22488 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022489 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022490 break;
22491 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022492 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022493 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022494 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022495#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022496 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022497 break;
22498#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022499 case VAR_STRING:
22500 if (varp->vval.v_string != NULL)
22501 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022502 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022503 case VAR_SPECIAL:
22504 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22505 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022506 case VAR_JOB:
22507#ifdef FEAT_JOB
22508 {
22509 job_T *job = varp->vval.v_job;
22510 char *status = job->jv_status == JOB_FAILED ? "fail"
22511 : job->jv_status == JOB_ENDED ? "dead"
22512 : "run";
22513# ifdef UNIX
22514 vim_snprintf((char *)buf, NUMBUFLEN,
22515 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022516# elif defined(WIN32)
22517 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022518 "process %ld %s",
22519 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022520 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022521# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022522 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022523 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22524# endif
22525 return buf;
22526 }
22527#endif
22528 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022529 case VAR_CHANNEL:
22530#ifdef FEAT_CHANNEL
22531 {
22532 channel_T *channel = varp->vval.v_channel;
22533 char *status = channel_status(channel);
22534
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022535 if (channel == NULL)
22536 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22537 else
22538 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022539 "channel %d %s", channel->ch_id, status);
22540 return buf;
22541 }
22542#endif
22543 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022544 case VAR_UNKNOWN:
22545 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022546 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022548 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022549}
22550
22551/*
22552 * Find variable "name" in the list of variables.
22553 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022554 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022555 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022556 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022557 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022558 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022559find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022560{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022561 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022562 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022563
Bram Moolenaara7043832005-01-21 11:56:39 +000022564 ht = find_var_ht(name, &varname);
22565 if (htp != NULL)
22566 *htp = ht;
22567 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022568 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022569 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022570}
22571
22572/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022573 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022574 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022575 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022576 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022577find_var_in_ht(
22578 hashtab_T *ht,
22579 int htname,
22580 char_u *varname,
22581 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022582{
Bram Moolenaar33570922005-01-25 22:26:29 +000022583 hashitem_T *hi;
22584
22585 if (*varname == NUL)
22586 {
22587 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022588 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022589 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022590 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022591 case 'g': return &globvars_var;
22592 case 'v': return &vimvars_var;
22593 case 'b': return &curbuf->b_bufvar;
22594 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022595#ifdef FEAT_WINDOWS
22596 case 't': return &curtab->tp_winvar;
22597#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022598 case 'l': return current_funccal == NULL
22599 ? NULL : &current_funccal->l_vars_var;
22600 case 'a': return current_funccal == NULL
22601 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022602 }
22603 return NULL;
22604 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022605
22606 hi = hash_find(ht, varname);
22607 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022608 {
22609 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022610 * worked find the variable again. Don't auto-load a script if it was
22611 * loaded already, otherwise it would be loaded every time when
22612 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022613 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022614 {
22615 /* Note: script_autoload() may make "hi" invalid. It must either
22616 * be obtained again or not used. */
22617 if (!script_autoload(varname, FALSE) || aborting())
22618 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022619 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022620 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022621 if (HASHITEM_EMPTY(hi))
22622 return NULL;
22623 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022624 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022625}
22626
22627/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022628 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022629 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022630 * Set "varname" to the start of name without ':'.
22631 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022632 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022633find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022635 hashitem_T *hi;
22636
Bram Moolenaar73627d02015-08-11 15:46:09 +020022637 if (name[0] == NUL)
22638 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022639 if (name[1] != ':')
22640 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022641 /* The name must not start with a colon or #. */
22642 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022643 return NULL;
22644 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022645
22646 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022647 hi = hash_find(&compat_hashtab, name);
22648 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022649 return &compat_hashtab;
22650
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022652 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022653 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022654 }
22655 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022656 if (*name == 'g') /* global variable */
22657 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022658 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22659 */
22660 if (vim_strchr(name + 2, ':') != NULL
22661 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022662 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022663 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022664 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022666 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022667#ifdef FEAT_WINDOWS
22668 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022669 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022670#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022671 if (*name == 'v') /* v: variable */
22672 return &vimvarht;
22673 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022674 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022675 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022676 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022677 if (*name == 's' /* script variable */
22678 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22679 return &SCRIPT_VARS(current_SID);
22680 return NULL;
22681}
22682
22683/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022684 * Get function call environment based on bactrace debug level
22685 */
22686 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022687get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022688{
22689 int i;
22690 funccall_T *funccal;
22691 funccall_T *temp_funccal;
22692
22693 funccal = current_funccal;
22694 if (debug_backtrace_level > 0)
22695 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022696 for (i = 0; i < debug_backtrace_level; i++)
22697 {
22698 temp_funccal = funccal->caller;
22699 if (temp_funccal)
22700 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022701 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022702 /* backtrace level overflow. reset to max */
22703 debug_backtrace_level = i;
22704 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022705 }
22706 return funccal;
22707}
22708
22709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022710 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022711 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712 * Returns NULL when it doesn't exist.
22713 */
22714 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022715get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022716{
Bram Moolenaar33570922005-01-25 22:26:29 +000022717 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022718
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022719 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022720 if (v == NULL)
22721 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022722 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022723}
22724
22725/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022726 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022727 * sourcing this script and when executing functions defined in the script.
22728 */
22729 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022730new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022731{
Bram Moolenaara7043832005-01-21 11:56:39 +000022732 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022733 hashtab_T *ht;
22734 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022735
Bram Moolenaar071d4272004-06-13 20:20:40 +000022736 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22737 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022738 /* Re-allocating ga_data means that an ht_array pointing to
22739 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022740 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022741 for (i = 1; i <= ga_scripts.ga_len; ++i)
22742 {
22743 ht = &SCRIPT_VARS(i);
22744 if (ht->ht_mask == HT_INIT_SIZE - 1)
22745 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022746 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022747 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022748 }
22749
Bram Moolenaar071d4272004-06-13 20:20:40 +000022750 while (ga_scripts.ga_len < id)
22751 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022752 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022753 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022754 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022756 }
22757 }
22758}
22759
22760/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022761 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22762 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022763 */
22764 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022765init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022766{
Bram Moolenaar33570922005-01-25 22:26:29 +000022767 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022768 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022769 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022770 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022771 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022772 dict_var->di_tv.vval.v_dict = dict;
22773 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022774 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022775 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22776 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022777}
22778
22779/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022780 * Unreference a dictionary initialized by init_var_dict().
22781 */
22782 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022783unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022784{
22785 /* Now the dict needs to be freed if no one else is using it, go back to
22786 * normal reference counting. */
22787 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22788 dict_unref(dict);
22789}
22790
22791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022792 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022793 * Frees all allocated variables and the value they contain.
22794 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022795 */
22796 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022797vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022798{
22799 vars_clear_ext(ht, TRUE);
22800}
22801
22802/*
22803 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22804 */
22805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022806vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022807{
Bram Moolenaara7043832005-01-21 11:56:39 +000022808 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022809 hashitem_T *hi;
22810 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022811
Bram Moolenaar33570922005-01-25 22:26:29 +000022812 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022813 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022814 for (hi = ht->ht_array; todo > 0; ++hi)
22815 {
22816 if (!HASHITEM_EMPTY(hi))
22817 {
22818 --todo;
22819
Bram Moolenaar33570922005-01-25 22:26:29 +000022820 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022821 * ht_array might change then. hash_clear() takes care of it
22822 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022823 v = HI2DI(hi);
22824 if (free_val)
22825 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022826 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022827 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022828 }
22829 }
22830 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022831 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022832}
22833
Bram Moolenaara7043832005-01-21 11:56:39 +000022834/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022835 * Delete a variable from hashtab "ht" at item "hi".
22836 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022837 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022839delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022840{
Bram Moolenaar33570922005-01-25 22:26:29 +000022841 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022842
22843 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022844 clear_tv(&di->di_tv);
22845 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022846}
22847
22848/*
22849 * List the value of one internal variable.
22850 */
22851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022852list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022853{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022854 char_u *tofree;
22855 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022856 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022857
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022858 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022859 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022860 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022861 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022862}
22863
Bram Moolenaar071d4272004-06-13 20:20:40 +000022864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022865list_one_var_a(
22866 char_u *prefix,
22867 char_u *name,
22868 int type,
22869 char_u *string,
22870 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022871{
Bram Moolenaar31859182007-08-14 20:41:13 +000022872 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22873 msg_start();
22874 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875 if (name != NULL) /* "a:" vars don't have a name stored */
22876 msg_puts(name);
22877 msg_putchar(' ');
22878 msg_advance(22);
22879 if (type == VAR_NUMBER)
22880 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022881 else if (type == VAR_FUNC)
22882 msg_putchar('*');
22883 else if (type == VAR_LIST)
22884 {
22885 msg_putchar('[');
22886 if (*string == '[')
22887 ++string;
22888 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022889 else if (type == VAR_DICT)
22890 {
22891 msg_putchar('{');
22892 if (*string == '{')
22893 ++string;
22894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895 else
22896 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022897
Bram Moolenaar071d4272004-06-13 20:20:40 +000022898 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022899
22900 if (type == VAR_FUNC)
22901 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022902 if (*first)
22903 {
22904 msg_clr_eos();
22905 *first = FALSE;
22906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022907}
22908
22909/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022910 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022911 * If the variable already exists, the value is updated.
22912 * Otherwise the variable is created.
22913 */
22914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022915set_var(
22916 char_u *name,
22917 typval_T *tv,
22918 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919{
Bram Moolenaar33570922005-01-25 22:26:29 +000022920 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022922 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022923
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022924 ht = find_var_ht(name, &varname);
22925 if (ht == NULL || *varname == NUL)
22926 {
22927 EMSG2(_(e_illvar), name);
22928 return;
22929 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022930 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022931
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022932 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22933 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022934
Bram Moolenaar33570922005-01-25 22:26:29 +000022935 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022937 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022938 if (var_check_ro(v->di_flags, name, FALSE)
22939 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022940 return;
22941 if (v->di_tv.v_type != tv->v_type
22942 && !((v->di_tv.v_type == VAR_STRING
22943 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022944 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022945 || tv->v_type == VAR_NUMBER))
22946#ifdef FEAT_FLOAT
22947 && !((v->di_tv.v_type == VAR_NUMBER
22948 || v->di_tv.v_type == VAR_FLOAT)
22949 && (tv->v_type == VAR_NUMBER
22950 || tv->v_type == VAR_FLOAT))
22951#endif
22952 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022953 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022954 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022955 return;
22956 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022957
22958 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022959 * Handle setting internal v: variables separately where needed to
22960 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022961 */
22962 if (ht == &vimvarht)
22963 {
22964 if (v->di_tv.v_type == VAR_STRING)
22965 {
22966 vim_free(v->di_tv.vval.v_string);
22967 if (copy || tv->v_type != VAR_STRING)
22968 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22969 else
22970 {
22971 /* Take over the string to avoid an extra alloc/free. */
22972 v->di_tv.vval.v_string = tv->vval.v_string;
22973 tv->vval.v_string = NULL;
22974 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022975 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022976 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022977 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022978 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022979 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022980 if (STRCMP(varname, "searchforward") == 0)
22981 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022982#ifdef FEAT_SEARCH_EXTRA
22983 else if (STRCMP(varname, "hlsearch") == 0)
22984 {
22985 no_hlsearch = !v->di_tv.vval.v_number;
22986 redraw_all_later(SOME_VALID);
22987 }
22988#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022989 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022990 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022991 else if (v->di_tv.v_type != tv->v_type)
22992 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022993 }
22994
22995 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996 }
22997 else /* add a new variable */
22998 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022999 /* Can't add "v:" variable. */
23000 if (ht == &vimvarht)
23001 {
23002 EMSG2(_(e_illvar), name);
23003 return;
23004 }
23005
Bram Moolenaar92124a32005-06-17 22:03:40 +000023006 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023007 if (!valid_varname(varname))
23008 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023009
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023010 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23011 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023012 if (v == NULL)
23013 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023014 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023015 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023016 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023017 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023018 return;
23019 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023020 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023021 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023022
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023023 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023024 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023025 else
23026 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023027 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023028 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023029 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023031}
23032
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023033/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023034 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023035 * Also give an error message.
23036 */
23037 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023038var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023039{
23040 if (flags & DI_FLAGS_RO)
23041 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023042 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023043 return TRUE;
23044 }
23045 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23046 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023047 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023048 return TRUE;
23049 }
23050 return FALSE;
23051}
23052
23053/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023054 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23055 * Also give an error message.
23056 */
23057 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023058var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023059{
23060 if (flags & DI_FLAGS_FIX)
23061 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023062 EMSG2(_("E795: Cannot delete variable %s"),
23063 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023064 return TRUE;
23065 }
23066 return FALSE;
23067}
23068
23069/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023070 * Check if a funcref is assigned to a valid variable name.
23071 * Return TRUE and give an error if not.
23072 */
23073 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023074var_check_func_name(
23075 char_u *name, /* points to start of variable name */
23076 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023077{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023078 /* Allow for w: b: s: and t:. */
23079 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023080 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23081 ? name[2] : name[0]))
23082 {
23083 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23084 name);
23085 return TRUE;
23086 }
23087 /* Don't allow hiding a function. When "v" is not NULL we might be
23088 * assigning another function to the same var, the type is checked
23089 * below. */
23090 if (new_var && function_exists(name))
23091 {
23092 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23093 name);
23094 return TRUE;
23095 }
23096 return FALSE;
23097}
23098
23099/*
23100 * Check if a variable name is valid.
23101 * Return FALSE and give an error if not.
23102 */
23103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023104valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023105{
23106 char_u *p;
23107
23108 for (p = varname; *p != NUL; ++p)
23109 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23110 && *p != AUTOLOAD_CHAR)
23111 {
23112 EMSG2(_(e_illvar), varname);
23113 return FALSE;
23114 }
23115 return TRUE;
23116}
23117
23118/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023119 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023120 * Also give an error message, using "name" or _("name") when use_gettext is
23121 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023122 */
23123 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023124tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023125{
23126 if (lock & VAR_LOCKED)
23127 {
23128 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023129 name == NULL ? (char_u *)_("Unknown")
23130 : use_gettext ? (char_u *)_(name)
23131 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023132 return TRUE;
23133 }
23134 if (lock & VAR_FIXED)
23135 {
23136 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023137 name == NULL ? (char_u *)_("Unknown")
23138 : use_gettext ? (char_u *)_(name)
23139 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023140 return TRUE;
23141 }
23142 return FALSE;
23143}
23144
23145/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023146 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023147 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023148 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023149 * It is OK for "from" and "to" to point to the same item. This is used to
23150 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023151 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023152 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023153copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023154{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023155 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023156 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023157 switch (from->v_type)
23158 {
23159 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023160 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023161 to->vval.v_number = from->vval.v_number;
23162 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023163 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023164#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023165 to->vval.v_float = from->vval.v_float;
23166 break;
23167#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023168 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010023169#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010023170 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023171 if (to->vval.v_job != NULL)
23172 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023173 break;
23174#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023175 case VAR_CHANNEL:
23176#ifdef FEAT_CHANNEL
23177 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023178 if (to->vval.v_channel != NULL)
23179 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023180 break;
23181#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023182 case VAR_STRING:
23183 case VAR_FUNC:
23184 if (from->vval.v_string == NULL)
23185 to->vval.v_string = NULL;
23186 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023187 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023188 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023189 if (from->v_type == VAR_FUNC)
23190 func_ref(to->vval.v_string);
23191 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023192 break;
23193 case VAR_LIST:
23194 if (from->vval.v_list == NULL)
23195 to->vval.v_list = NULL;
23196 else
23197 {
23198 to->vval.v_list = from->vval.v_list;
23199 ++to->vval.v_list->lv_refcount;
23200 }
23201 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023202 case VAR_DICT:
23203 if (from->vval.v_dict == NULL)
23204 to->vval.v_dict = NULL;
23205 else
23206 {
23207 to->vval.v_dict = from->vval.v_dict;
23208 ++to->vval.v_dict->dv_refcount;
23209 }
23210 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023211 case VAR_UNKNOWN:
23212 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023213 break;
23214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023215}
23216
23217/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023218 * Make a copy of an item.
23219 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023220 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23221 * reference to an already copied list/dict can be used.
23222 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023223 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023224 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023225item_copy(
23226 typval_T *from,
23227 typval_T *to,
23228 int deep,
23229 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023230{
23231 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023232 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023233
Bram Moolenaar33570922005-01-25 22:26:29 +000023234 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023235 {
23236 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023237 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023238 }
23239 ++recurse;
23240
23241 switch (from->v_type)
23242 {
23243 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023244 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023245 case VAR_STRING:
23246 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010023247 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023248 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023249 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023250 copy_tv(from, to);
23251 break;
23252 case VAR_LIST:
23253 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023254 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023255 if (from->vval.v_list == NULL)
23256 to->vval.v_list = NULL;
23257 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23258 {
23259 /* use the copy made earlier */
23260 to->vval.v_list = from->vval.v_list->lv_copylist;
23261 ++to->vval.v_list->lv_refcount;
23262 }
23263 else
23264 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23265 if (to->vval.v_list == NULL)
23266 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023267 break;
23268 case VAR_DICT:
23269 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023270 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023271 if (from->vval.v_dict == NULL)
23272 to->vval.v_dict = NULL;
23273 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23274 {
23275 /* use the copy made earlier */
23276 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23277 ++to->vval.v_dict->dv_refcount;
23278 }
23279 else
23280 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23281 if (to->vval.v_dict == NULL)
23282 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023283 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023284 case VAR_UNKNOWN:
23285 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023286 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023287 }
23288 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023289 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023290}
23291
23292/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023293 * ":echo expr1 ..." print each argument separated with a space, add a
23294 * newline at the end.
23295 * ":echon expr1 ..." print each argument plain.
23296 */
23297 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023298ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023299{
23300 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023301 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023302 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023303 char_u *p;
23304 int needclr = TRUE;
23305 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023306 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023307
23308 if (eap->skip)
23309 ++emsg_skip;
23310 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23311 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023312 /* If eval1() causes an error message the text from the command may
23313 * still need to be cleared. E.g., "echo 22,44". */
23314 need_clr_eos = needclr;
23315
Bram Moolenaar071d4272004-06-13 20:20:40 +000023316 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023317 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023318 {
23319 /*
23320 * Report the invalid expression unless the expression evaluation
23321 * has been cancelled due to an aborting error, an interrupt, or an
23322 * exception.
23323 */
23324 if (!aborting())
23325 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023326 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023327 break;
23328 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023329 need_clr_eos = FALSE;
23330
Bram Moolenaar071d4272004-06-13 20:20:40 +000023331 if (!eap->skip)
23332 {
23333 if (atstart)
23334 {
23335 atstart = FALSE;
23336 /* Call msg_start() after eval1(), evaluating the expression
23337 * may cause a message to appear. */
23338 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023339 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023340 /* Mark the saved text as finishing the line, so that what
23341 * follows is displayed on a new line when scrolling back
23342 * at the more prompt. */
23343 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023344 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023346 }
23347 else if (eap->cmdidx == CMD_echo)
23348 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023349 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023350 if (p != NULL)
23351 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023352 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023353 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023354 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023355 if (*p != TAB && needclr)
23356 {
23357 /* remove any text still there from the command */
23358 msg_clr_eos();
23359 needclr = FALSE;
23360 }
23361 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023362 }
23363 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023364 {
23365#ifdef FEAT_MBYTE
23366 if (has_mbyte)
23367 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023368 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023369
23370 (void)msg_outtrans_len_attr(p, i, echo_attr);
23371 p += i - 1;
23372 }
23373 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023374#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023375 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023377 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023378 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023379 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023380 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023381 arg = skipwhite(arg);
23382 }
23383 eap->nextcmd = check_nextcmd(arg);
23384
23385 if (eap->skip)
23386 --emsg_skip;
23387 else
23388 {
23389 /* remove text that may still be there from the command */
23390 if (needclr)
23391 msg_clr_eos();
23392 if (eap->cmdidx == CMD_echo)
23393 msg_end();
23394 }
23395}
23396
23397/*
23398 * ":echohl {name}".
23399 */
23400 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023401ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023402{
23403 int id;
23404
23405 id = syn_name2id(eap->arg);
23406 if (id == 0)
23407 echo_attr = 0;
23408 else
23409 echo_attr = syn_id2attr(id);
23410}
23411
23412/*
23413 * ":execute expr1 ..." execute the result of an expression.
23414 * ":echomsg expr1 ..." Print a message
23415 * ":echoerr expr1 ..." Print an error
23416 * Each gets spaces around each argument and a newline at the end for
23417 * echo commands
23418 */
23419 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023420ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023421{
23422 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023423 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023424 int ret = OK;
23425 char_u *p;
23426 garray_T ga;
23427 int len;
23428 int save_did_emsg;
23429
23430 ga_init2(&ga, 1, 80);
23431
23432 if (eap->skip)
23433 ++emsg_skip;
23434 while (*arg != NUL && *arg != '|' && *arg != '\n')
23435 {
23436 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023437 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023438 {
23439 /*
23440 * Report the invalid expression unless the expression evaluation
23441 * has been cancelled due to an aborting error, an interrupt, or an
23442 * exception.
23443 */
23444 if (!aborting())
23445 EMSG2(_(e_invexpr2), p);
23446 ret = FAIL;
23447 break;
23448 }
23449
23450 if (!eap->skip)
23451 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023452 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023453 len = (int)STRLEN(p);
23454 if (ga_grow(&ga, len + 2) == FAIL)
23455 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023456 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023457 ret = FAIL;
23458 break;
23459 }
23460 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023461 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023462 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023463 ga.ga_len += len;
23464 }
23465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023466 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023467 arg = skipwhite(arg);
23468 }
23469
23470 if (ret != FAIL && ga.ga_data != NULL)
23471 {
23472 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023473 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023474 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023475 out_flush();
23476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023477 else if (eap->cmdidx == CMD_echoerr)
23478 {
23479 /* We don't want to abort following commands, restore did_emsg. */
23480 save_did_emsg = did_emsg;
23481 EMSG((char_u *)ga.ga_data);
23482 if (!force_abort)
23483 did_emsg = save_did_emsg;
23484 }
23485 else if (eap->cmdidx == CMD_execute)
23486 do_cmdline((char_u *)ga.ga_data,
23487 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23488 }
23489
23490 ga_clear(&ga);
23491
23492 if (eap->skip)
23493 --emsg_skip;
23494
23495 eap->nextcmd = check_nextcmd(arg);
23496}
23497
23498/*
23499 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23500 * "arg" points to the "&" or '+' when called, to "option" when returning.
23501 * Returns NULL when no option name found. Otherwise pointer to the char
23502 * after the option name.
23503 */
23504 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023505find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023506{
23507 char_u *p = *arg;
23508
23509 ++p;
23510 if (*p == 'g' && p[1] == ':')
23511 {
23512 *opt_flags = OPT_GLOBAL;
23513 p += 2;
23514 }
23515 else if (*p == 'l' && p[1] == ':')
23516 {
23517 *opt_flags = OPT_LOCAL;
23518 p += 2;
23519 }
23520 else
23521 *opt_flags = 0;
23522
23523 if (!ASCII_ISALPHA(*p))
23524 return NULL;
23525 *arg = p;
23526
23527 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23528 p += 4; /* termcap option */
23529 else
23530 while (ASCII_ISALPHA(*p))
23531 ++p;
23532 return p;
23533}
23534
23535/*
23536 * ":function"
23537 */
23538 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023539ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023540{
23541 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023542 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023543 int j;
23544 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023545 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023546 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023547 char_u *name = NULL;
23548 char_u *p;
23549 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023550 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023551 garray_T newargs;
23552 garray_T newlines;
23553 int varargs = FALSE;
23554 int mustend = FALSE;
23555 int flags = 0;
23556 ufunc_T *fp;
23557 int indent;
23558 int nesting;
23559 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023560 dictitem_T *v;
23561 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023562 static int func_nr = 0; /* number for nameless function */
23563 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023564 hashtab_T *ht;
23565 int todo;
23566 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023567 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023568
23569 /*
23570 * ":function" without argument: list functions.
23571 */
23572 if (ends_excmd(*eap->arg))
23573 {
23574 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023575 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023576 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023577 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023578 {
23579 if (!HASHITEM_EMPTY(hi))
23580 {
23581 --todo;
23582 fp = HI2UF(hi);
23583 if (!isdigit(*fp->uf_name))
23584 list_func_head(fp, FALSE);
23585 }
23586 }
23587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023588 eap->nextcmd = check_nextcmd(eap->arg);
23589 return;
23590 }
23591
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023592 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023593 * ":function /pat": list functions matching pattern.
23594 */
23595 if (*eap->arg == '/')
23596 {
23597 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23598 if (!eap->skip)
23599 {
23600 regmatch_T regmatch;
23601
23602 c = *p;
23603 *p = NUL;
23604 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23605 *p = c;
23606 if (regmatch.regprog != NULL)
23607 {
23608 regmatch.rm_ic = p_ic;
23609
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023610 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023611 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23612 {
23613 if (!HASHITEM_EMPTY(hi))
23614 {
23615 --todo;
23616 fp = HI2UF(hi);
23617 if (!isdigit(*fp->uf_name)
23618 && vim_regexec(&regmatch, fp->uf_name, 0))
23619 list_func_head(fp, FALSE);
23620 }
23621 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023622 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023623 }
23624 }
23625 if (*p == '/')
23626 ++p;
23627 eap->nextcmd = check_nextcmd(p);
23628 return;
23629 }
23630
23631 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023632 * Get the function name. There are these situations:
23633 * func normal function name
23634 * "name" == func, "fudi.fd_dict" == NULL
23635 * dict.func new dictionary entry
23636 * "name" == NULL, "fudi.fd_dict" set,
23637 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23638 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023639 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023640 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23641 * dict.func existing dict entry that's not a Funcref
23642 * "name" == NULL, "fudi.fd_dict" set,
23643 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023644 * s:func script-local function name
23645 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023647 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023648 name = trans_function_name(&p, eap->skip, 0, &fudi);
23649 paren = (vim_strchr(p, '(') != NULL);
23650 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023651 {
23652 /*
23653 * Return on an invalid expression in braces, unless the expression
23654 * evaluation has been cancelled due to an aborting error, an
23655 * interrupt, or an exception.
23656 */
23657 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023658 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023659 if (!eap->skip && fudi.fd_newkey != NULL)
23660 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023661 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023662 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023664 else
23665 eap->skip = TRUE;
23666 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023667
Bram Moolenaar071d4272004-06-13 20:20:40 +000023668 /* An error in a function call during evaluation of an expression in magic
23669 * braces should not cause the function not to be defined. */
23670 saved_did_emsg = did_emsg;
23671 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023672
23673 /*
23674 * ":function func" with only function name: list function.
23675 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023676 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023677 {
23678 if (!ends_excmd(*skipwhite(p)))
23679 {
23680 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023681 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023682 }
23683 eap->nextcmd = check_nextcmd(p);
23684 if (eap->nextcmd != NULL)
23685 *p = NUL;
23686 if (!eap->skip && !got_int)
23687 {
23688 fp = find_func(name);
23689 if (fp != NULL)
23690 {
23691 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023692 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023693 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023694 if (FUNCLINE(fp, j) == NULL)
23695 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023696 msg_putchar('\n');
23697 msg_outnum((long)(j + 1));
23698 if (j < 9)
23699 msg_putchar(' ');
23700 if (j < 99)
23701 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023702 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023703 out_flush(); /* show a line at a time */
23704 ui_breakcheck();
23705 }
23706 if (!got_int)
23707 {
23708 msg_putchar('\n');
23709 msg_puts((char_u *)" endfunction");
23710 }
23711 }
23712 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023713 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023714 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023715 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023716 }
23717
23718 /*
23719 * ":function name(arg1, arg2)" Define function.
23720 */
23721 p = skipwhite(p);
23722 if (*p != '(')
23723 {
23724 if (!eap->skip)
23725 {
23726 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023727 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023728 }
23729 /* attempt to continue by skipping some text */
23730 if (vim_strchr(p, '(') != NULL)
23731 p = vim_strchr(p, '(');
23732 }
23733 p = skipwhite(p + 1);
23734
23735 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23736 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23737
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023738 if (!eap->skip)
23739 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023740 /* Check the name of the function. Unless it's a dictionary function
23741 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023742 if (name != NULL)
23743 arg = name;
23744 else
23745 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023746 if (arg != NULL && (fudi.fd_di == NULL
23747 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023748 {
23749 if (*arg == K_SPECIAL)
23750 j = 3;
23751 else
23752 j = 0;
23753 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23754 : eval_isnamec(arg[j])))
23755 ++j;
23756 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023757 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023758 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023759 /* Disallow using the g: dict. */
23760 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23761 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023762 }
23763
Bram Moolenaar071d4272004-06-13 20:20:40 +000023764 /*
23765 * Isolate the arguments: "arg1, arg2, ...)"
23766 */
23767 while (*p != ')')
23768 {
23769 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23770 {
23771 varargs = TRUE;
23772 p += 3;
23773 mustend = TRUE;
23774 }
23775 else
23776 {
23777 arg = p;
23778 while (ASCII_ISALNUM(*p) || *p == '_')
23779 ++p;
23780 if (arg == p || isdigit(*arg)
23781 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23782 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23783 {
23784 if (!eap->skip)
23785 EMSG2(_("E125: Illegal argument: %s"), arg);
23786 break;
23787 }
23788 if (ga_grow(&newargs, 1) == FAIL)
23789 goto erret;
23790 c = *p;
23791 *p = NUL;
23792 arg = vim_strsave(arg);
23793 if (arg == NULL)
23794 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023795
23796 /* Check for duplicate argument name. */
23797 for (i = 0; i < newargs.ga_len; ++i)
23798 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23799 {
23800 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023801 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023802 goto erret;
23803 }
23804
Bram Moolenaar071d4272004-06-13 20:20:40 +000023805 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23806 *p = c;
23807 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023808 if (*p == ',')
23809 ++p;
23810 else
23811 mustend = TRUE;
23812 }
23813 p = skipwhite(p);
23814 if (mustend && *p != ')')
23815 {
23816 if (!eap->skip)
23817 EMSG2(_(e_invarg2), eap->arg);
23818 break;
23819 }
23820 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023821 if (*p != ')')
23822 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023823 ++p; /* skip the ')' */
23824
Bram Moolenaare9a41262005-01-15 22:18:47 +000023825 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023826 for (;;)
23827 {
23828 p = skipwhite(p);
23829 if (STRNCMP(p, "range", 5) == 0)
23830 {
23831 flags |= FC_RANGE;
23832 p += 5;
23833 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023834 else if (STRNCMP(p, "dict", 4) == 0)
23835 {
23836 flags |= FC_DICT;
23837 p += 4;
23838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023839 else if (STRNCMP(p, "abort", 5) == 0)
23840 {
23841 flags |= FC_ABORT;
23842 p += 5;
23843 }
23844 else
23845 break;
23846 }
23847
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023848 /* When there is a line break use what follows for the function body.
23849 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23850 if (*p == '\n')
23851 line_arg = p + 1;
23852 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023853 EMSG(_(e_trailing));
23854
23855 /*
23856 * Read the body of the function, until ":endfunction" is found.
23857 */
23858 if (KeyTyped)
23859 {
23860 /* Check if the function already exists, don't let the user type the
23861 * whole function before telling him it doesn't work! For a script we
23862 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023863 if (!eap->skip && !eap->forceit)
23864 {
23865 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23866 EMSG(_(e_funcdict));
23867 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023868 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023870
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023871 if (!eap->skip && did_emsg)
23872 goto erret;
23873
Bram Moolenaar071d4272004-06-13 20:20:40 +000023874 msg_putchar('\n'); /* don't overwrite the function name */
23875 cmdline_row = msg_row;
23876 }
23877
23878 indent = 2;
23879 nesting = 0;
23880 for (;;)
23881 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023882 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023883 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023884 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023885 saved_wait_return = FALSE;
23886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023887 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023888 sourcing_lnum_off = sourcing_lnum;
23889
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023890 if (line_arg != NULL)
23891 {
23892 /* Use eap->arg, split up in parts by line breaks. */
23893 theline = line_arg;
23894 p = vim_strchr(theline, '\n');
23895 if (p == NULL)
23896 line_arg += STRLEN(line_arg);
23897 else
23898 {
23899 *p = NUL;
23900 line_arg = p + 1;
23901 }
23902 }
23903 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023904 theline = getcmdline(':', 0L, indent);
23905 else
23906 theline = eap->getline(':', eap->cookie, indent);
23907 if (KeyTyped)
23908 lines_left = Rows - 1;
23909 if (theline == NULL)
23910 {
23911 EMSG(_("E126: Missing :endfunction"));
23912 goto erret;
23913 }
23914
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023915 /* Detect line continuation: sourcing_lnum increased more than one. */
23916 if (sourcing_lnum > sourcing_lnum_off + 1)
23917 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23918 else
23919 sourcing_lnum_off = 0;
23920
Bram Moolenaar071d4272004-06-13 20:20:40 +000023921 if (skip_until != NULL)
23922 {
23923 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23924 * don't check for ":endfunc". */
23925 if (STRCMP(theline, skip_until) == 0)
23926 {
23927 vim_free(skip_until);
23928 skip_until = NULL;
23929 }
23930 }
23931 else
23932 {
23933 /* skip ':' and blanks*/
23934 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23935 ;
23936
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023937 /* Check for "endfunction". */
23938 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023939 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023940 if (line_arg == NULL)
23941 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023942 break;
23943 }
23944
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023945 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023946 * at "end". */
23947 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23948 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023949 else if (STRNCMP(p, "if", 2) == 0
23950 || STRNCMP(p, "wh", 2) == 0
23951 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023952 || STRNCMP(p, "try", 3) == 0)
23953 indent += 2;
23954
23955 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023956 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023957 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023958 if (*p == '!')
23959 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023960 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023961 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23962 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023963 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023964 ++nesting;
23965 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023966 }
23967 }
23968
23969 /* Check for ":append" or ":insert". */
23970 p = skip_range(p, NULL);
23971 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23972 || (p[0] == 'i'
23973 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23974 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23975 skip_until = vim_strsave((char_u *)".");
23976
23977 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23978 arg = skipwhite(skiptowhite(p));
23979 if (arg[0] == '<' && arg[1] =='<'
23980 && ((p[0] == 'p' && p[1] == 'y'
23981 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23982 || (p[0] == 'p' && p[1] == 'e'
23983 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23984 || (p[0] == 't' && p[1] == 'c'
23985 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023986 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23987 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023988 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23989 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023990 || (p[0] == 'm' && p[1] == 'z'
23991 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023992 ))
23993 {
23994 /* ":python <<" continues until a dot, like ":append" */
23995 p = skipwhite(arg + 2);
23996 if (*p == NUL)
23997 skip_until = vim_strsave((char_u *)".");
23998 else
23999 skip_until = vim_strsave(p);
24000 }
24001 }
24002
24003 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024004 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024005 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024006 if (line_arg == NULL)
24007 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024009 }
24010
24011 /* Copy the line to newly allocated memory. get_one_sourceline()
24012 * allocates 250 bytes per line, this saves 80% on average. The cost
24013 * is an extra alloc/free. */
24014 p = vim_strsave(theline);
24015 if (p != NULL)
24016 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024017 if (line_arg == NULL)
24018 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024019 theline = p;
24020 }
24021
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024022 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24023
24024 /* Add NULL lines for continuation lines, so that the line count is
24025 * equal to the index in the growarray. */
24026 while (sourcing_lnum_off-- > 0)
24027 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024028
24029 /* Check for end of eap->arg. */
24030 if (line_arg != NULL && *line_arg == NUL)
24031 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024032 }
24033
24034 /* Don't define the function when skipping commands or when an error was
24035 * detected. */
24036 if (eap->skip || did_emsg)
24037 goto erret;
24038
24039 /*
24040 * If there are no errors, add the function
24041 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024042 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024043 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024044 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024045 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024046 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024047 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024048 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024049 goto erret;
24050 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024051
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024052 fp = find_func(name);
24053 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024054 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024055 if (!eap->forceit)
24056 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024057 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024058 goto erret;
24059 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024060 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024061 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024062 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024063 name);
24064 goto erret;
24065 }
24066 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024067 ga_clear_strings(&(fp->uf_args));
24068 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024069 vim_free(name);
24070 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024072 }
24073 else
24074 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024075 char numbuf[20];
24076
24077 fp = NULL;
24078 if (fudi.fd_newkey == NULL && !eap->forceit)
24079 {
24080 EMSG(_(e_funcdict));
24081 goto erret;
24082 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024083 if (fudi.fd_di == NULL)
24084 {
24085 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024086 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024087 goto erret;
24088 }
24089 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024090 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024091 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024092
24093 /* Give the function a sequential number. Can only be used with a
24094 * Funcref! */
24095 vim_free(name);
24096 sprintf(numbuf, "%d", ++func_nr);
24097 name = vim_strsave((char_u *)numbuf);
24098 if (name == NULL)
24099 goto erret;
24100 }
24101
24102 if (fp == NULL)
24103 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024104 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024105 {
24106 int slen, plen;
24107 char_u *scriptname;
24108
24109 /* Check that the autoload name matches the script name. */
24110 j = FAIL;
24111 if (sourcing_name != NULL)
24112 {
24113 scriptname = autoload_name(name);
24114 if (scriptname != NULL)
24115 {
24116 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024117 plen = (int)STRLEN(p);
24118 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024119 if (slen > plen && fnamecmp(p,
24120 sourcing_name + slen - plen) == 0)
24121 j = OK;
24122 vim_free(scriptname);
24123 }
24124 }
24125 if (j == FAIL)
24126 {
24127 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24128 goto erret;
24129 }
24130 }
24131
24132 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024133 if (fp == NULL)
24134 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024135
24136 if (fudi.fd_dict != NULL)
24137 {
24138 if (fudi.fd_di == NULL)
24139 {
24140 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024141 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024142 if (fudi.fd_di == NULL)
24143 {
24144 vim_free(fp);
24145 goto erret;
24146 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024147 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24148 {
24149 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024150 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024151 goto erret;
24152 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024153 }
24154 else
24155 /* overwrite existing dict entry */
24156 clear_tv(&fudi.fd_di->di_tv);
24157 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024158 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024159 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024160 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024161
24162 /* behave like "dict" was used */
24163 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024164 }
24165
Bram Moolenaar071d4272004-06-13 20:20:40 +000024166 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024167 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024168 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24169 {
24170 vim_free(fp);
24171 goto erret;
24172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024173 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024174 fp->uf_args = newargs;
24175 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024176#ifdef FEAT_PROFILE
24177 fp->uf_tml_count = NULL;
24178 fp->uf_tml_total = NULL;
24179 fp->uf_tml_self = NULL;
24180 fp->uf_profiling = FALSE;
24181 if (prof_def_func())
24182 func_do_profile(fp);
24183#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024184 fp->uf_varargs = varargs;
24185 fp->uf_flags = flags;
24186 fp->uf_calls = 0;
24187 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024188 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024189
24190erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024191 ga_clear_strings(&newargs);
24192 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024193ret_free:
24194 vim_free(skip_until);
24195 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024196 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024197 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024198 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024199}
24200
24201/*
24202 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024203 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024204 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024205 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024206 * TFN_INT: internal function name OK
24207 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024208 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024209 * Advances "pp" to just after the function name (if no error).
24210 */
24211 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024212trans_function_name(
24213 char_u **pp,
24214 int skip, /* only find the end, don't evaluate */
24215 int flags,
24216 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024217{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024218 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024219 char_u *start;
24220 char_u *end;
24221 int lead;
24222 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024223 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024224 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024225
24226 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024227 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024228 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024229
24230 /* Check for hard coded <SNR>: already translated function ID (from a user
24231 * command). */
24232 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24233 && (*pp)[2] == (int)KE_SNR)
24234 {
24235 *pp += 3;
24236 len = get_id_len(pp) + 3;
24237 return vim_strnsave(start, len);
24238 }
24239
24240 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24241 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024242 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024243 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024244 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024245
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024246 /* Note that TFN_ flags use the same values as GLV_ flags. */
24247 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024248 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024249 if (end == start)
24250 {
24251 if (!skip)
24252 EMSG(_("E129: Function name required"));
24253 goto theend;
24254 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024255 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024256 {
24257 /*
24258 * Report an invalid expression in braces, unless the expression
24259 * evaluation has been cancelled due to an aborting error, an
24260 * interrupt, or an exception.
24261 */
24262 if (!aborting())
24263 {
24264 if (end != NULL)
24265 EMSG2(_(e_invarg2), start);
24266 }
24267 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024268 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024269 goto theend;
24270 }
24271
24272 if (lv.ll_tv != NULL)
24273 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024274 if (fdp != NULL)
24275 {
24276 fdp->fd_dict = lv.ll_dict;
24277 fdp->fd_newkey = lv.ll_newkey;
24278 lv.ll_newkey = NULL;
24279 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024280 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024281 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24282 {
24283 name = vim_strsave(lv.ll_tv->vval.v_string);
24284 *pp = end;
24285 }
24286 else
24287 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024288 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24289 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024290 EMSG(_(e_funcref));
24291 else
24292 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024293 name = NULL;
24294 }
24295 goto theend;
24296 }
24297
24298 if (lv.ll_name == NULL)
24299 {
24300 /* Error found, but continue after the function name. */
24301 *pp = end;
24302 goto theend;
24303 }
24304
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024305 /* Check if the name is a Funcref. If so, use the value. */
24306 if (lv.ll_exp_name != NULL)
24307 {
24308 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024309 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024310 if (name == lv.ll_exp_name)
24311 name = NULL;
24312 }
24313 else
24314 {
24315 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024316 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024317 if (name == *pp)
24318 name = NULL;
24319 }
24320 if (name != NULL)
24321 {
24322 name = vim_strsave(name);
24323 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024324 if (STRNCMP(name, "<SNR>", 5) == 0)
24325 {
24326 /* Change "<SNR>" to the byte sequence. */
24327 name[0] = K_SPECIAL;
24328 name[1] = KS_EXTRA;
24329 name[2] = (int)KE_SNR;
24330 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24331 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024332 goto theend;
24333 }
24334
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024335 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024336 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024337 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024338 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24339 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24340 {
24341 /* When there was "s:" already or the name expanded to get a
24342 * leading "s:" then remove it. */
24343 lv.ll_name += 2;
24344 len -= 2;
24345 lead = 2;
24346 }
24347 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024348 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024349 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024350 /* skip over "s:" and "g:" */
24351 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024352 lv.ll_name += 2;
24353 len = (int)(end - lv.ll_name);
24354 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024355
24356 /*
24357 * Copy the function name to allocated memory.
24358 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24359 * Accept <SNR>123_name() outside a script.
24360 */
24361 if (skip)
24362 lead = 0; /* do nothing */
24363 else if (lead > 0)
24364 {
24365 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024366 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24367 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024368 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024369 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024370 if (current_SID <= 0)
24371 {
24372 EMSG(_(e_usingsid));
24373 goto theend;
24374 }
24375 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24376 lead += (int)STRLEN(sid_buf);
24377 }
24378 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024379 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024380 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024381 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024382 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024383 goto theend;
24384 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024385 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024386 {
24387 char_u *cp = vim_strchr(lv.ll_name, ':');
24388
24389 if (cp != NULL && cp < end)
24390 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024391 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024392 goto theend;
24393 }
24394 }
24395
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024396 name = alloc((unsigned)(len + lead + 1));
24397 if (name != NULL)
24398 {
24399 if (lead > 0)
24400 {
24401 name[0] = K_SPECIAL;
24402 name[1] = KS_EXTRA;
24403 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024404 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024405 STRCPY(name + 3, sid_buf);
24406 }
24407 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024408 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024409 }
24410 *pp = end;
24411
24412theend:
24413 clear_lval(&lv);
24414 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024415}
24416
24417/*
24418 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24419 * Return 2 if "p" starts with "s:".
24420 * Return 0 otherwise.
24421 */
24422 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024423eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024424{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024425 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24426 * the standard library function. */
24427 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24428 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024429 return 5;
24430 if (p[0] == 's' && p[1] == ':')
24431 return 2;
24432 return 0;
24433}
24434
24435/*
24436 * Return TRUE if "p" starts with "<SID>" or "s:".
24437 * Only works if eval_fname_script() returned non-zero for "p"!
24438 */
24439 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024440eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024441{
24442 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24443}
24444
24445/*
24446 * List the head of the function: "name(arg1, arg2)".
24447 */
24448 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024449list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024450{
24451 int j;
24452
24453 msg_start();
24454 if (indent)
24455 MSG_PUTS(" ");
24456 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024457 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024458 {
24459 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024460 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024461 }
24462 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024463 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024464 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024465 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024466 {
24467 if (j)
24468 MSG_PUTS(", ");
24469 msg_puts(FUNCARG(fp, j));
24470 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024471 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024472 {
24473 if (j)
24474 MSG_PUTS(", ");
24475 MSG_PUTS("...");
24476 }
24477 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024478 if (fp->uf_flags & FC_ABORT)
24479 MSG_PUTS(" abort");
24480 if (fp->uf_flags & FC_RANGE)
24481 MSG_PUTS(" range");
24482 if (fp->uf_flags & FC_DICT)
24483 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024484 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024485 if (p_verbose > 0)
24486 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024487}
24488
24489/*
24490 * Find a function by name, return pointer to it in ufuncs.
24491 * Return NULL for unknown function.
24492 */
24493 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024494find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024495{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024496 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024497
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024498 hi = hash_find(&func_hashtab, name);
24499 if (!HASHITEM_EMPTY(hi))
24500 return HI2UF(hi);
24501 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024502}
24503
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024504#if defined(EXITFREE) || defined(PROTO)
24505 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024506free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024507{
24508 hashitem_T *hi;
24509
24510 /* Need to start all over every time, because func_free() may change the
24511 * hash table. */
24512 while (func_hashtab.ht_used > 0)
24513 for (hi = func_hashtab.ht_array; ; ++hi)
24514 if (!HASHITEM_EMPTY(hi))
24515 {
24516 func_free(HI2UF(hi));
24517 break;
24518 }
24519}
24520#endif
24521
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024522 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024523translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024524{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024525 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024526 return find_internal_func(name) >= 0;
24527 return find_func(name) != NULL;
24528}
24529
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024530/*
24531 * Return TRUE if a function "name" exists.
24532 */
24533 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024534function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024535{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024536 char_u *nm = name;
24537 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024538 int n = FALSE;
24539
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024540 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
24541 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024542 nm = skipwhite(nm);
24543
24544 /* Only accept "funcname", "funcname ", "funcname (..." and
24545 * "funcname(...", not "funcname!...". */
24546 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024547 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024548 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024549 return n;
24550}
24551
Bram Moolenaara1544c02013-05-30 12:35:52 +020024552 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024553get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024554{
24555 char_u *nm = name;
24556 char_u *p;
24557
24558 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
24559
24560 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024561 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024562 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024563
Bram Moolenaara1544c02013-05-30 12:35:52 +020024564 vim_free(p);
24565 return NULL;
24566}
24567
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024568/*
24569 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024570 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24571 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024572 */
24573 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024574builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024575{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024576 char_u *p;
24577
24578 if (!ASCII_ISLOWER(name[0]))
24579 return FALSE;
24580 p = vim_strchr(name, AUTOLOAD_CHAR);
24581 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024582}
24583
Bram Moolenaar05159a02005-02-26 23:04:13 +000024584#if defined(FEAT_PROFILE) || defined(PROTO)
24585/*
24586 * Start profiling function "fp".
24587 */
24588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024589func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024590{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024591 int len = fp->uf_lines.ga_len;
24592
24593 if (len == 0)
24594 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024595 fp->uf_tm_count = 0;
24596 profile_zero(&fp->uf_tm_self);
24597 profile_zero(&fp->uf_tm_total);
24598 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024599 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024600 if (fp->uf_tml_total == NULL)
24601 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024602 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024603 if (fp->uf_tml_self == NULL)
24604 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024605 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024606 fp->uf_tml_idx = -1;
24607 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24608 || fp->uf_tml_self == NULL)
24609 return; /* out of memory */
24610
24611 fp->uf_profiling = TRUE;
24612}
24613
24614/*
24615 * Dump the profiling results for all functions in file "fd".
24616 */
24617 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024618func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024619{
24620 hashitem_T *hi;
24621 int todo;
24622 ufunc_T *fp;
24623 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024624 ufunc_T **sorttab;
24625 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024626
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024627 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024628 if (todo == 0)
24629 return; /* nothing to dump */
24630
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024631 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024632
Bram Moolenaar05159a02005-02-26 23:04:13 +000024633 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24634 {
24635 if (!HASHITEM_EMPTY(hi))
24636 {
24637 --todo;
24638 fp = HI2UF(hi);
24639 if (fp->uf_profiling)
24640 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024641 if (sorttab != NULL)
24642 sorttab[st_len++] = fp;
24643
Bram Moolenaar05159a02005-02-26 23:04:13 +000024644 if (fp->uf_name[0] == K_SPECIAL)
24645 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24646 else
24647 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24648 if (fp->uf_tm_count == 1)
24649 fprintf(fd, "Called 1 time\n");
24650 else
24651 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24652 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24653 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24654 fprintf(fd, "\n");
24655 fprintf(fd, "count total (s) self (s)\n");
24656
24657 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24658 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024659 if (FUNCLINE(fp, i) == NULL)
24660 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024661 prof_func_line(fd, fp->uf_tml_count[i],
24662 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024663 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24664 }
24665 fprintf(fd, "\n");
24666 }
24667 }
24668 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024669
24670 if (sorttab != NULL && st_len > 0)
24671 {
24672 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24673 prof_total_cmp);
24674 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24675 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24676 prof_self_cmp);
24677 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24678 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024679
24680 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024681}
Bram Moolenaar73830342005-02-28 22:48:19 +000024682
24683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024684prof_sort_list(
24685 FILE *fd,
24686 ufunc_T **sorttab,
24687 int st_len,
24688 char *title,
24689 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024690{
24691 int i;
24692 ufunc_T *fp;
24693
24694 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24695 fprintf(fd, "count total (s) self (s) function\n");
24696 for (i = 0; i < 20 && i < st_len; ++i)
24697 {
24698 fp = sorttab[i];
24699 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24700 prefer_self);
24701 if (fp->uf_name[0] == K_SPECIAL)
24702 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24703 else
24704 fprintf(fd, " %s()\n", fp->uf_name);
24705 }
24706 fprintf(fd, "\n");
24707}
24708
24709/*
24710 * Print the count and times for one function or function line.
24711 */
24712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024713prof_func_line(
24714 FILE *fd,
24715 int count,
24716 proftime_T *total,
24717 proftime_T *self,
24718 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024719{
24720 if (count > 0)
24721 {
24722 fprintf(fd, "%5d ", count);
24723 if (prefer_self && profile_equal(total, self))
24724 fprintf(fd, " ");
24725 else
24726 fprintf(fd, "%s ", profile_msg(total));
24727 if (!prefer_self && profile_equal(total, self))
24728 fprintf(fd, " ");
24729 else
24730 fprintf(fd, "%s ", profile_msg(self));
24731 }
24732 else
24733 fprintf(fd, " ");
24734}
24735
24736/*
24737 * Compare function for total time sorting.
24738 */
24739 static int
24740#ifdef __BORLANDC__
24741_RTLENTRYF
24742#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024743prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024744{
24745 ufunc_T *p1, *p2;
24746
24747 p1 = *(ufunc_T **)s1;
24748 p2 = *(ufunc_T **)s2;
24749 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24750}
24751
24752/*
24753 * Compare function for self time sorting.
24754 */
24755 static int
24756#ifdef __BORLANDC__
24757_RTLENTRYF
24758#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024759prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024760{
24761 ufunc_T *p1, *p2;
24762
24763 p1 = *(ufunc_T **)s1;
24764 p2 = *(ufunc_T **)s2;
24765 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24766}
24767
Bram Moolenaar05159a02005-02-26 23:04:13 +000024768#endif
24769
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024770/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024771 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024772 * Return TRUE if a package was loaded.
24773 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024774 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024775script_autoload(
24776 char_u *name,
24777 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024778{
24779 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024780 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024781 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024782 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024783
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024784 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024785 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024786 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024787 return FALSE;
24788
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024789 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024790
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024791 /* Find the name in the list of previously loaded package names. Skip
24792 * "autoload/", it's always the same. */
24793 for (i = 0; i < ga_loaded.ga_len; ++i)
24794 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24795 break;
24796 if (!reload && i < ga_loaded.ga_len)
24797 ret = FALSE; /* was loaded already */
24798 else
24799 {
24800 /* Remember the name if it wasn't loaded already. */
24801 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24802 {
24803 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24804 tofree = NULL;
24805 }
24806
24807 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024808 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024809 ret = TRUE;
24810 }
24811
24812 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024813 return ret;
24814}
24815
24816/*
24817 * Return the autoload script name for a function or variable name.
24818 * Returns NULL when out of memory.
24819 */
24820 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024821autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024822{
24823 char_u *p;
24824 char_u *scriptname;
24825
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024826 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024827 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24828 if (scriptname == NULL)
24829 return FALSE;
24830 STRCPY(scriptname, "autoload/");
24831 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024832 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024833 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024834 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024835 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024836 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024837}
24838
Bram Moolenaar071d4272004-06-13 20:20:40 +000024839#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24840
24841/*
24842 * Function given to ExpandGeneric() to obtain the list of user defined
24843 * function names.
24844 */
24845 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024846get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024847{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024848 static long_u done;
24849 static hashitem_T *hi;
24850 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024851
24852 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024853 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024854 done = 0;
24855 hi = func_hashtab.ht_array;
24856 }
24857 if (done < func_hashtab.ht_used)
24858 {
24859 if (done++ > 0)
24860 ++hi;
24861 while (HASHITEM_EMPTY(hi))
24862 ++hi;
24863 fp = HI2UF(hi);
24864
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024865 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024866 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024867
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024868 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24869 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024870
24871 cat_func_name(IObuff, fp);
24872 if (xp->xp_context != EXPAND_USER_FUNC)
24873 {
24874 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024875 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024876 STRCAT(IObuff, ")");
24877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024878 return IObuff;
24879 }
24880 return NULL;
24881}
24882
24883#endif /* FEAT_CMDL_COMPL */
24884
24885/*
24886 * Copy the function name of "fp" to buffer "buf".
24887 * "buf" must be able to hold the function name plus three bytes.
24888 * Takes care of script-local function names.
24889 */
24890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024891cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024892{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024893 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024894 {
24895 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024896 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024897 }
24898 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024899 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024900}
24901
24902/*
24903 * ":delfunction {name}"
24904 */
24905 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024906ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024907{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024908 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024909 char_u *p;
24910 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024911 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024912
24913 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024914 name = trans_function_name(&p, eap->skip, 0, &fudi);
24915 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024916 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024917 {
24918 if (fudi.fd_dict != NULL && !eap->skip)
24919 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024920 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024922 if (!ends_excmd(*skipwhite(p)))
24923 {
24924 vim_free(name);
24925 EMSG(_(e_trailing));
24926 return;
24927 }
24928 eap->nextcmd = check_nextcmd(p);
24929 if (eap->nextcmd != NULL)
24930 *p = NUL;
24931
24932 if (!eap->skip)
24933 fp = find_func(name);
24934 vim_free(name);
24935
24936 if (!eap->skip)
24937 {
24938 if (fp == NULL)
24939 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024940 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024941 return;
24942 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024943 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024944 {
24945 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24946 return;
24947 }
24948
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024949 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024950 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024951 /* Delete the dict item that refers to the function, it will
24952 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024953 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024954 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024955 else
24956 func_free(fp);
24957 }
24958}
24959
24960/*
24961 * Free a function and remove it from the list of functions.
24962 */
24963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024964func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024965{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024966 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024967
24968 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024969 ga_clear_strings(&(fp->uf_args));
24970 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024971#ifdef FEAT_PROFILE
24972 vim_free(fp->uf_tml_count);
24973 vim_free(fp->uf_tml_total);
24974 vim_free(fp->uf_tml_self);
24975#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024976
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024977 /* remove the function from the function hashtable */
24978 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24979 if (HASHITEM_EMPTY(hi))
24980 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024981 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024982 hash_remove(&func_hashtab, hi);
24983
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024984 vim_free(fp);
24985}
24986
24987/*
24988 * Unreference a Function: decrement the reference count and free it when it
24989 * becomes zero. Only for numbered functions.
24990 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024991 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024992func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024993{
24994 ufunc_T *fp;
24995
24996 if (name != NULL && isdigit(*name))
24997 {
24998 fp = find_func(name);
24999 if (fp == NULL)
25000 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025001 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025002 {
25003 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025004 * when "uf_calls" becomes zero. */
25005 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025006 func_free(fp);
25007 }
25008 }
25009}
25010
25011/*
25012 * Count a reference to a Function.
25013 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025014 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025015func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025016{
25017 ufunc_T *fp;
25018
25019 if (name != NULL && isdigit(*name))
25020 {
25021 fp = find_func(name);
25022 if (fp == NULL)
25023 EMSG2(_(e_intern2), "func_ref()");
25024 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025025 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025026 }
25027}
25028
25029/*
25030 * Call a user function.
25031 */
25032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025033call_user_func(
25034 ufunc_T *fp, /* pointer to function */
25035 int argcount, /* nr of args */
25036 typval_T *argvars, /* arguments */
25037 typval_T *rettv, /* return value */
25038 linenr_T firstline, /* first line of range */
25039 linenr_T lastline, /* last line of range */
25040 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025041{
Bram Moolenaar33570922005-01-25 22:26:29 +000025042 char_u *save_sourcing_name;
25043 linenr_T save_sourcing_lnum;
25044 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025045 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025046 int save_did_emsg;
25047 static int depth = 0;
25048 dictitem_T *v;
25049 int fixvar_idx = 0; /* index in fixvar[] */
25050 int i;
25051 int ai;
25052 char_u numbuf[NUMBUFLEN];
25053 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025054 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025055#ifdef FEAT_PROFILE
25056 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025057 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025059
25060 /* If depth of calling is getting too high, don't execute the function */
25061 if (depth >= p_mfd)
25062 {
25063 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025064 rettv->v_type = VAR_NUMBER;
25065 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025066 return;
25067 }
25068 ++depth;
25069
25070 line_breakcheck(); /* check for CTRL-C hit */
25071
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025072 fc = (funccall_T *)alloc(sizeof(funccall_T));
25073 fc->caller = current_funccal;
25074 current_funccal = fc;
25075 fc->func = fp;
25076 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025077 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025078 fc->linenr = 0;
25079 fc->returned = FALSE;
25080 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025081 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025082 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25083 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025084
Bram Moolenaar33570922005-01-25 22:26:29 +000025085 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025086 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025087 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25088 * each argument variable and saves a lot of time.
25089 */
25090 /*
25091 * Init l: variables.
25092 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025093 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025094 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025095 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025096 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25097 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025098 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025099 name = v->di_key;
25100 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025101 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025102 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025103 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025104 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025105 v->di_tv.vval.v_dict = selfdict;
25106 ++selfdict->dv_refcount;
25107 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025108
Bram Moolenaar33570922005-01-25 22:26:29 +000025109 /*
25110 * Init a: variables.
25111 * Set a:0 to "argcount".
25112 * Set a:000 to a list with room for the "..." arguments.
25113 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025114 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025115 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025116 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025117 /* Use "name" to avoid a warning from some compiler that checks the
25118 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025119 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025120 name = v->di_key;
25121 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025122 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025123 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025124 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025125 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025126 v->di_tv.vval.v_list = &fc->l_varlist;
25127 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25128 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25129 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025130
25131 /*
25132 * Set a:firstline to "firstline" and a:lastline to "lastline".
25133 * Set a:name to named arguments.
25134 * Set a:N to the "..." arguments.
25135 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025136 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025137 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025138 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025139 (varnumber_T)lastline);
25140 for (i = 0; i < argcount; ++i)
25141 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025142 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025143 if (ai < 0)
25144 /* named argument a:name */
25145 name = FUNCARG(fp, i);
25146 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025147 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025148 /* "..." argument a:1, a:2, etc. */
25149 sprintf((char *)numbuf, "%d", ai + 1);
25150 name = numbuf;
25151 }
25152 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25153 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025154 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025155 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25156 }
25157 else
25158 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025159 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25160 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025161 if (v == NULL)
25162 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025163 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025164 }
25165 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025166 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025167
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025168 /* Note: the values are copied directly to avoid alloc/free.
25169 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025170 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025171 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025172
25173 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25174 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025175 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25176 fc->l_listitems[ai].li_tv = argvars[i];
25177 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025178 }
25179 }
25180
Bram Moolenaar071d4272004-06-13 20:20:40 +000025181 /* Don't redraw while executing the function. */
25182 ++RedrawingDisabled;
25183 save_sourcing_name = sourcing_name;
25184 save_sourcing_lnum = sourcing_lnum;
25185 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025186 /* need space for function name + ("function " + 3) or "[number]" */
25187 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25188 + STRLEN(fp->uf_name) + 20;
25189 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025190 if (sourcing_name != NULL)
25191 {
25192 if (save_sourcing_name != NULL
25193 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025194 sprintf((char *)sourcing_name, "%s[%d]..",
25195 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025196 else
25197 STRCPY(sourcing_name, "function ");
25198 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25199
25200 if (p_verbose >= 12)
25201 {
25202 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025203 verbose_enter_scroll();
25204
Bram Moolenaar555b2802005-05-19 21:08:39 +000025205 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025206 if (p_verbose >= 14)
25207 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025208 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025209 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025210 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025211 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025212
25213 msg_puts((char_u *)"(");
25214 for (i = 0; i < argcount; ++i)
25215 {
25216 if (i > 0)
25217 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025218 if (argvars[i].v_type == VAR_NUMBER)
25219 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025220 else
25221 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025222 /* Do not want errors such as E724 here. */
25223 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025224 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025225 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025226 if (s != NULL)
25227 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025228 if (vim_strsize(s) > MSG_BUF_CLEN)
25229 {
25230 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25231 s = buf;
25232 }
25233 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025234 vim_free(tofree);
25235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025236 }
25237 }
25238 msg_puts((char_u *)")");
25239 }
25240 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025241
25242 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025243 --no_wait_return;
25244 }
25245 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025246#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025247 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025248 {
25249 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25250 func_do_profile(fp);
25251 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025252 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025253 {
25254 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025255 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025256 profile_zero(&fp->uf_tm_children);
25257 }
25258 script_prof_save(&wait_start);
25259 }
25260#endif
25261
Bram Moolenaar071d4272004-06-13 20:20:40 +000025262 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025263 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025264 save_did_emsg = did_emsg;
25265 did_emsg = FALSE;
25266
25267 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025268 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025269 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25270
25271 --RedrawingDisabled;
25272
25273 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025274 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025275 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025276 clear_tv(rettv);
25277 rettv->v_type = VAR_NUMBER;
25278 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025279 }
25280
Bram Moolenaar05159a02005-02-26 23:04:13 +000025281#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025282 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025283 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025284 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025285 profile_end(&call_start);
25286 profile_sub_wait(&wait_start, &call_start);
25287 profile_add(&fp->uf_tm_total, &call_start);
25288 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025289 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025290 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025291 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25292 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025293 }
25294 }
25295#endif
25296
Bram Moolenaar071d4272004-06-13 20:20:40 +000025297 /* when being verbose, mention the return value */
25298 if (p_verbose >= 12)
25299 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025300 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025301 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025302
Bram Moolenaar071d4272004-06-13 20:20:40 +000025303 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025304 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025305 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025306 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025307 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025308 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025309 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025310 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025311 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025312 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025313 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025314
Bram Moolenaar555b2802005-05-19 21:08:39 +000025315 /* The value may be very long. Skip the middle part, so that we
25316 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025317 * truncate it at the end. Don't want errors such as E724 here. */
25318 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025319 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025320 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025321 if (s != NULL)
25322 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025323 if (vim_strsize(s) > MSG_BUF_CLEN)
25324 {
25325 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25326 s = buf;
25327 }
25328 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025329 vim_free(tofree);
25330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025331 }
25332 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025333
25334 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025335 --no_wait_return;
25336 }
25337
25338 vim_free(sourcing_name);
25339 sourcing_name = save_sourcing_name;
25340 sourcing_lnum = save_sourcing_lnum;
25341 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025342#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025343 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025344 script_prof_restore(&wait_start);
25345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025346
25347 if (p_verbose >= 12 && sourcing_name != NULL)
25348 {
25349 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025350 verbose_enter_scroll();
25351
Bram Moolenaar555b2802005-05-19 21:08:39 +000025352 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025353 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025354
25355 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025356 --no_wait_return;
25357 }
25358
25359 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025360 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025361 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025362
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025363 /* If the a:000 list and the l: and a: dicts are not referenced we can
25364 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025365 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25366 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25367 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25368 {
25369 free_funccal(fc, FALSE);
25370 }
25371 else
25372 {
25373 hashitem_T *hi;
25374 listitem_T *li;
25375 int todo;
25376
25377 /* "fc" is still in use. This can happen when returning "a:000" or
25378 * assigning "l:" to a global variable.
25379 * Link "fc" in the list for garbage collection later. */
25380 fc->caller = previous_funccal;
25381 previous_funccal = fc;
25382
25383 /* Make a copy of the a: variables, since we didn't do that above. */
25384 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25385 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25386 {
25387 if (!HASHITEM_EMPTY(hi))
25388 {
25389 --todo;
25390 v = HI2DI(hi);
25391 copy_tv(&v->di_tv, &v->di_tv);
25392 }
25393 }
25394
25395 /* Make a copy of the a:000 items, since we didn't do that above. */
25396 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25397 copy_tv(&li->li_tv, &li->li_tv);
25398 }
25399}
25400
25401/*
25402 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025403 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025404 */
25405 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025406can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025407{
25408 return (fc->l_varlist.lv_copyID != copyID
25409 && fc->l_vars.dv_copyID != copyID
25410 && fc->l_avars.dv_copyID != copyID);
25411}
25412
25413/*
25414 * Free "fc" and what it contains.
25415 */
25416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025417free_funccal(
25418 funccall_T *fc,
25419 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025420{
25421 listitem_T *li;
25422
25423 /* The a: variables typevals may not have been allocated, only free the
25424 * allocated variables. */
25425 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25426
25427 /* free all l: variables */
25428 vars_clear(&fc->l_vars.dv_hashtab);
25429
25430 /* Free the a:000 variables if they were allocated. */
25431 if (free_val)
25432 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25433 clear_tv(&li->li_tv);
25434
25435 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025436}
25437
25438/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025439 * Add a number variable "name" to dict "dp" with value "nr".
25440 */
25441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025442add_nr_var(
25443 dict_T *dp,
25444 dictitem_T *v,
25445 char *name,
25446 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025447{
25448 STRCPY(v->di_key, name);
25449 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25450 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25451 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025452 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025453 v->di_tv.vval.v_number = nr;
25454}
25455
25456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025457 * ":return [expr]"
25458 */
25459 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025460ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025461{
25462 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025463 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025464 int returning = FALSE;
25465
25466 if (current_funccal == NULL)
25467 {
25468 EMSG(_("E133: :return not inside a function"));
25469 return;
25470 }
25471
25472 if (eap->skip)
25473 ++emsg_skip;
25474
25475 eap->nextcmd = NULL;
25476 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025477 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025478 {
25479 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025480 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025481 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025482 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025483 }
25484 /* It's safer to return also on error. */
25485 else if (!eap->skip)
25486 {
25487 /*
25488 * Return unless the expression evaluation has been cancelled due to an
25489 * aborting error, an interrupt, or an exception.
25490 */
25491 if (!aborting())
25492 returning = do_return(eap, FALSE, TRUE, NULL);
25493 }
25494
25495 /* When skipping or the return gets pending, advance to the next command
25496 * in this line (!returning). Otherwise, ignore the rest of the line.
25497 * Following lines will be ignored by get_func_line(). */
25498 if (returning)
25499 eap->nextcmd = NULL;
25500 else if (eap->nextcmd == NULL) /* no argument */
25501 eap->nextcmd = check_nextcmd(arg);
25502
25503 if (eap->skip)
25504 --emsg_skip;
25505}
25506
25507/*
25508 * Return from a function. Possibly makes the return pending. Also called
25509 * for a pending return at the ":endtry" or after returning from an extra
25510 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025511 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025512 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025513 * FALSE when the return gets pending.
25514 */
25515 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025516do_return(
25517 exarg_T *eap,
25518 int reanimate,
25519 int is_cmd,
25520 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025521{
25522 int idx;
25523 struct condstack *cstack = eap->cstack;
25524
25525 if (reanimate)
25526 /* Undo the return. */
25527 current_funccal->returned = FALSE;
25528
25529 /*
25530 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25531 * not in its finally clause (which then is to be executed next) is found.
25532 * In this case, make the ":return" pending for execution at the ":endtry".
25533 * Otherwise, return normally.
25534 */
25535 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25536 if (idx >= 0)
25537 {
25538 cstack->cs_pending[idx] = CSTP_RETURN;
25539
25540 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025541 /* A pending return again gets pending. "rettv" points to an
25542 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025543 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025544 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025545 else
25546 {
25547 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025548 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025549 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025550 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025551
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025552 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025553 {
25554 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025555 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025556 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025557 else
25558 EMSG(_(e_outofmem));
25559 }
25560 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025561 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025562
25563 if (reanimate)
25564 {
25565 /* The pending return value could be overwritten by a ":return"
25566 * without argument in a finally clause; reset the default
25567 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025568 current_funccal->rettv->v_type = VAR_NUMBER;
25569 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025570 }
25571 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025572 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025573 }
25574 else
25575 {
25576 current_funccal->returned = TRUE;
25577
25578 /* If the return is carried out now, store the return value. For
25579 * a return immediately after reanimation, the value is already
25580 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025581 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025582 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025583 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025584 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025585 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025586 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025587 }
25588 }
25589
25590 return idx < 0;
25591}
25592
25593/*
25594 * Free the variable with a pending return value.
25595 */
25596 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025597discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025598{
Bram Moolenaar33570922005-01-25 22:26:29 +000025599 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025600}
25601
25602/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025603 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025604 * is an allocated string. Used by report_pending() for verbose messages.
25605 */
25606 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025607get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025608{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025609 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025610 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025611 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025612
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025613 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025614 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025615 if (s == NULL)
25616 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025617
25618 STRCPY(IObuff, ":return ");
25619 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25620 if (STRLEN(s) + 8 >= IOSIZE)
25621 STRCPY(IObuff + IOSIZE - 4, "...");
25622 vim_free(tofree);
25623 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025624}
25625
25626/*
25627 * Get next function line.
25628 * Called by do_cmdline() to get the next line.
25629 * Returns allocated string, or NULL for end of function.
25630 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025631 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025632get_func_line(
25633 int c UNUSED,
25634 void *cookie,
25635 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025636{
Bram Moolenaar33570922005-01-25 22:26:29 +000025637 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025638 ufunc_T *fp = fcp->func;
25639 char_u *retval;
25640 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025641
25642 /* If breakpoints have been added/deleted need to check for it. */
25643 if (fcp->dbg_tick != debug_tick)
25644 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025645 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025646 sourcing_lnum);
25647 fcp->dbg_tick = debug_tick;
25648 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025649#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025650 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025651 func_line_end(cookie);
25652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025653
Bram Moolenaar05159a02005-02-26 23:04:13 +000025654 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025655 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25656 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025657 retval = NULL;
25658 else
25659 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025660 /* Skip NULL lines (continuation lines). */
25661 while (fcp->linenr < gap->ga_len
25662 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25663 ++fcp->linenr;
25664 if (fcp->linenr >= gap->ga_len)
25665 retval = NULL;
25666 else
25667 {
25668 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25669 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025670#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025671 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025672 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025673#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025675 }
25676
25677 /* Did we encounter a breakpoint? */
25678 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25679 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025680 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025681 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025682 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025683 sourcing_lnum);
25684 fcp->dbg_tick = debug_tick;
25685 }
25686
25687 return retval;
25688}
25689
Bram Moolenaar05159a02005-02-26 23:04:13 +000025690#if defined(FEAT_PROFILE) || defined(PROTO)
25691/*
25692 * Called when starting to read a function line.
25693 * "sourcing_lnum" must be correct!
25694 * When skipping lines it may not actually be executed, but we won't find out
25695 * until later and we need to store the time now.
25696 */
25697 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025698func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025699{
25700 funccall_T *fcp = (funccall_T *)cookie;
25701 ufunc_T *fp = fcp->func;
25702
25703 if (fp->uf_profiling && sourcing_lnum >= 1
25704 && sourcing_lnum <= fp->uf_lines.ga_len)
25705 {
25706 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025707 /* Skip continuation lines. */
25708 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25709 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025710 fp->uf_tml_execed = FALSE;
25711 profile_start(&fp->uf_tml_start);
25712 profile_zero(&fp->uf_tml_children);
25713 profile_get_wait(&fp->uf_tml_wait);
25714 }
25715}
25716
25717/*
25718 * Called when actually executing a function line.
25719 */
25720 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025721func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025722{
25723 funccall_T *fcp = (funccall_T *)cookie;
25724 ufunc_T *fp = fcp->func;
25725
25726 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25727 fp->uf_tml_execed = TRUE;
25728}
25729
25730/*
25731 * Called when done with a function line.
25732 */
25733 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025734func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025735{
25736 funccall_T *fcp = (funccall_T *)cookie;
25737 ufunc_T *fp = fcp->func;
25738
25739 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25740 {
25741 if (fp->uf_tml_execed)
25742 {
25743 ++fp->uf_tml_count[fp->uf_tml_idx];
25744 profile_end(&fp->uf_tml_start);
25745 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025746 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025747 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25748 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025749 }
25750 fp->uf_tml_idx = -1;
25751 }
25752}
25753#endif
25754
Bram Moolenaar071d4272004-06-13 20:20:40 +000025755/*
25756 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025757 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025758 */
25759 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025760func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025761{
Bram Moolenaar33570922005-01-25 22:26:29 +000025762 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025763
25764 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25765 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025766 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025767 || fcp->returned);
25768}
25769
25770/*
25771 * return TRUE if cookie indicates a function which "abort"s on errors.
25772 */
25773 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025774func_has_abort(
25775 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025776{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025777 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025778}
25779
25780#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25781typedef enum
25782{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025783 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25784 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25785 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025786} var_flavour_T;
25787
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025788static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025789
25790 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025791var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025792{
25793 char_u *p = varname;
25794
25795 if (ASCII_ISUPPER(*p))
25796 {
25797 while (*(++p))
25798 if (ASCII_ISLOWER(*p))
25799 return VAR_FLAVOUR_SESSION;
25800 return VAR_FLAVOUR_VIMINFO;
25801 }
25802 else
25803 return VAR_FLAVOUR_DEFAULT;
25804}
25805#endif
25806
25807#if defined(FEAT_VIMINFO) || defined(PROTO)
25808/*
25809 * Restore global vars that start with a capital from the viminfo file
25810 */
25811 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025812read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025813{
25814 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025815 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025816 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025817 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025818
25819 if (!writing && (find_viminfo_parameter('!') != NULL))
25820 {
25821 tab = vim_strchr(virp->vir_line + 1, '\t');
25822 if (tab != NULL)
25823 {
25824 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025825 switch (*tab)
25826 {
25827 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025828#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025829 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025830#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025831 case 'D': type = VAR_DICT; break;
25832 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025833 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025835
25836 tab = vim_strchr(tab, '\t');
25837 if (tab != NULL)
25838 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025839 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025840 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025841 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025842 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025843#ifdef FEAT_FLOAT
25844 else if (type == VAR_FLOAT)
25845 (void)string2float(tab + 1, &tv.vval.v_float);
25846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025847 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025848 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025849 if (type == VAR_DICT || type == VAR_LIST)
25850 {
25851 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25852
25853 if (etv == NULL)
25854 /* Failed to parse back the dict or list, use it as a
25855 * string. */
25856 tv.v_type = VAR_STRING;
25857 else
25858 {
25859 vim_free(tv.vval.v_string);
25860 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025861 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025862 }
25863 }
25864
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025865 /* when in a function use global variables */
25866 save_funccal = current_funccal;
25867 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025868 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025869 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025870
25871 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025872 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025873 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25874 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025875 }
25876 }
25877 }
25878
25879 return viminfo_readline(virp);
25880}
25881
25882/*
25883 * Write global vars that start with a capital to the viminfo file
25884 */
25885 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025886write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025887{
Bram Moolenaar33570922005-01-25 22:26:29 +000025888 hashitem_T *hi;
25889 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025890 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025891 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025892 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025893 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025894 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025895
25896 if (find_viminfo_parameter('!') == NULL)
25897 return;
25898
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025899 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025900
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025901 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025902 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025903 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025904 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025905 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025906 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025907 this_var = HI2DI(hi);
25908 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025909 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025910 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025911 {
25912 case VAR_STRING: s = "STR"; break;
25913 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025914 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025915 case VAR_DICT: s = "DIC"; break;
25916 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025917 case VAR_SPECIAL: s = "XPL"; break;
25918
25919 case VAR_UNKNOWN:
25920 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025921 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025922 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025923 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025924 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025925 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025926 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025927 if (p != NULL)
25928 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025929 vim_free(tofree);
25930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025931 }
25932 }
25933}
25934#endif
25935
25936#if defined(FEAT_SESSION) || defined(PROTO)
25937 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025938store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025939{
Bram Moolenaar33570922005-01-25 22:26:29 +000025940 hashitem_T *hi;
25941 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025942 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025943 char_u *p, *t;
25944
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025945 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025946 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025947 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025948 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025949 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025950 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025951 this_var = HI2DI(hi);
25952 if ((this_var->di_tv.v_type == VAR_NUMBER
25953 || this_var->di_tv.v_type == VAR_STRING)
25954 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025955 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025956 /* Escape special characters with a backslash. Turn a LF and
25957 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025958 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025959 (char_u *)"\\\"\n\r");
25960 if (p == NULL) /* out of memory */
25961 break;
25962 for (t = p; *t != NUL; ++t)
25963 if (*t == '\n')
25964 *t = 'n';
25965 else if (*t == '\r')
25966 *t = 'r';
25967 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025968 this_var->di_key,
25969 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25970 : ' ',
25971 p,
25972 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25973 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025974 || put_eol(fd) == FAIL)
25975 {
25976 vim_free(p);
25977 return FAIL;
25978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025979 vim_free(p);
25980 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025981#ifdef FEAT_FLOAT
25982 else if (this_var->di_tv.v_type == VAR_FLOAT
25983 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25984 {
25985 float_T f = this_var->di_tv.vval.v_float;
25986 int sign = ' ';
25987
25988 if (f < 0)
25989 {
25990 f = -f;
25991 sign = '-';
25992 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025993 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025994 this_var->di_key, sign, f) < 0)
25995 || put_eol(fd) == FAIL)
25996 return FAIL;
25997 }
25998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025999 }
26000 }
26001 return OK;
26002}
26003#endif
26004
Bram Moolenaar661b1822005-07-28 22:36:45 +000026005/*
26006 * Display script name where an item was last set.
26007 * Should only be invoked when 'verbose' is non-zero.
26008 */
26009 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026010last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026011{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026012 char_u *p;
26013
Bram Moolenaar661b1822005-07-28 22:36:45 +000026014 if (scriptID != 0)
26015 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026016 p = home_replace_save(NULL, get_scriptname(scriptID));
26017 if (p != NULL)
26018 {
26019 verbose_enter();
26020 MSG_PUTS(_("\n\tLast set from "));
26021 MSG_PUTS(p);
26022 vim_free(p);
26023 verbose_leave();
26024 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026025 }
26026}
26027
Bram Moolenaard812df62008-11-09 12:46:09 +000026028/*
26029 * List v:oldfiles in a nice way.
26030 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026031 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026032ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026033{
26034 list_T *l = vimvars[VV_OLDFILES].vv_list;
26035 listitem_T *li;
26036 int nr = 0;
26037
26038 if (l == NULL)
26039 msg((char_u *)_("No old files"));
26040 else
26041 {
26042 msg_start();
26043 msg_scroll = TRUE;
26044 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26045 {
26046 msg_outnum((long)++nr);
26047 MSG_PUTS(": ");
26048 msg_outtrans(get_tv_string(&li->li_tv));
26049 msg_putchar('\n');
26050 out_flush(); /* output one line at a time */
26051 ui_breakcheck();
26052 }
26053 /* Assume "got_int" was set to truncate the listing. */
26054 got_int = FALSE;
26055
26056#ifdef FEAT_BROWSE_CMD
26057 if (cmdmod.browse)
26058 {
26059 quit_more = FALSE;
26060 nr = prompt_for_number(FALSE);
26061 msg_starthere();
26062 if (nr > 0)
26063 {
26064 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26065 (long)nr);
26066
26067 if (p != NULL)
26068 {
26069 p = expand_env_save(p);
26070 eap->arg = p;
26071 eap->cmdidx = CMD_edit;
26072 cmdmod.browse = FALSE;
26073 do_exedit(eap, NULL);
26074 vim_free(p);
26075 }
26076 }
26077 }
26078#endif
26079 }
26080}
26081
Bram Moolenaar53744302015-07-17 17:38:22 +020026082/* reset v:option_new, v:option_old and v:option_type */
26083 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026084reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026085{
26086 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26087 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26088 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26089}
26090
26091
Bram Moolenaar071d4272004-06-13 20:20:40 +000026092#endif /* FEAT_EVAL */
26093
Bram Moolenaar071d4272004-06-13 20:20:40 +000026094
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026095#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026096
26097#ifdef WIN3264
26098/*
26099 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26100 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026101static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26102static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26103static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026104
26105/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026106 * Get the short path (8.3) for the filename in "fnamep".
26107 * Only works for a valid file name.
26108 * When the path gets longer "fnamep" is changed and the allocated buffer
26109 * is put in "bufp".
26110 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26111 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026112 */
26113 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026114get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026115{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026116 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026117 char_u *newbuf;
26118
26119 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026120 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026121 if (l > len - 1)
26122 {
26123 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026124 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026125 newbuf = vim_strnsave(*fnamep, l);
26126 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026127 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026128
26129 vim_free(*bufp);
26130 *fnamep = *bufp = newbuf;
26131
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026132 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026133 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026134 }
26135
26136 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026137 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026138}
26139
26140/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026141 * Get the short path (8.3) for the filename in "fname". The converted
26142 * path is returned in "bufp".
26143 *
26144 * Some of the directories specified in "fname" may not exist. This function
26145 * will shorten the existing directories at the beginning of the path and then
26146 * append the remaining non-existing path.
26147 *
26148 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026149 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026150 * bufp - Pointer to an allocated buffer for the filename.
26151 * fnamelen - Length of the filename pointed to by fname
26152 *
26153 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026154 */
26155 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026156shortpath_for_invalid_fname(
26157 char_u **fname,
26158 char_u **bufp,
26159 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026160{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026161 char_u *short_fname, *save_fname, *pbuf_unused;
26162 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026163 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026164 int old_len, len;
26165 int new_len, sfx_len;
26166 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026167
26168 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026169 old_len = *fnamelen;
26170 save_fname = vim_strnsave(*fname, old_len);
26171 pbuf_unused = NULL;
26172 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026173
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026174 endp = save_fname + old_len - 1; /* Find the end of the copy */
26175 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026176
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026177 /*
26178 * Try shortening the supplied path till it succeeds by removing one
26179 * directory at a time from the tail of the path.
26180 */
26181 len = 0;
26182 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026183 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026184 /* go back one path-separator */
26185 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26186 --endp;
26187 if (endp <= save_fname)
26188 break; /* processed the complete path */
26189
26190 /*
26191 * Replace the path separator with a NUL and try to shorten the
26192 * resulting path.
26193 */
26194 ch = *endp;
26195 *endp = 0;
26196 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026197 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026198 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26199 {
26200 retval = FAIL;
26201 goto theend;
26202 }
26203 *endp = ch; /* preserve the string */
26204
26205 if (len > 0)
26206 break; /* successfully shortened the path */
26207
26208 /* failed to shorten the path. Skip the path separator */
26209 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026210 }
26211
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026212 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026213 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026214 /*
26215 * Succeeded in shortening the path. Now concatenate the shortened
26216 * path with the remaining path at the tail.
26217 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026218
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026219 /* Compute the length of the new path. */
26220 sfx_len = (int)(save_endp - endp) + 1;
26221 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026222
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026223 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026224 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026225 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026226 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026227 /* There is not enough space in the currently allocated string,
26228 * copy it to a buffer big enough. */
26229 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026230 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026231 {
26232 retval = FAIL;
26233 goto theend;
26234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026235 }
26236 else
26237 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026238 /* Transfer short_fname to the main buffer (it's big enough),
26239 * unless get_short_pathname() did its work in-place. */
26240 *fname = *bufp = save_fname;
26241 if (short_fname != save_fname)
26242 vim_strncpy(save_fname, short_fname, len);
26243 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026244 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026245
26246 /* concat the not-shortened part of the path */
26247 vim_strncpy(*fname + len, endp, sfx_len);
26248 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026249 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026250
26251theend:
26252 vim_free(pbuf_unused);
26253 vim_free(save_fname);
26254
26255 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026256}
26257
26258/*
26259 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026260 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026261 */
26262 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026263shortpath_for_partial(
26264 char_u **fnamep,
26265 char_u **bufp,
26266 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026267{
26268 int sepcount, len, tflen;
26269 char_u *p;
26270 char_u *pbuf, *tfname;
26271 int hasTilde;
26272
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026273 /* Count up the path separators from the RHS.. so we know which part
26274 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026275 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026276 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026277 if (vim_ispathsep(*p))
26278 ++sepcount;
26279
26280 /* Need full path first (use expand_env() to remove a "~/") */
26281 hasTilde = (**fnamep == '~');
26282 if (hasTilde)
26283 pbuf = tfname = expand_env_save(*fnamep);
26284 else
26285 pbuf = tfname = FullName_save(*fnamep, FALSE);
26286
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026287 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026288
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026289 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26290 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026291
26292 if (len == 0)
26293 {
26294 /* Don't have a valid filename, so shorten the rest of the
26295 * path if we can. This CAN give us invalid 8.3 filenames, but
26296 * there's not a lot of point in guessing what it might be.
26297 */
26298 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026299 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26300 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026301 }
26302
26303 /* Count the paths backward to find the beginning of the desired string. */
26304 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026305 {
26306#ifdef FEAT_MBYTE
26307 if (has_mbyte)
26308 p -= mb_head_off(tfname, p);
26309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026310 if (vim_ispathsep(*p))
26311 {
26312 if (sepcount == 0 || (hasTilde && sepcount == 1))
26313 break;
26314 else
26315 sepcount --;
26316 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026318 if (hasTilde)
26319 {
26320 --p;
26321 if (p >= tfname)
26322 *p = '~';
26323 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026324 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026325 }
26326 else
26327 ++p;
26328
26329 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26330 vim_free(*bufp);
26331 *fnamelen = (int)STRLEN(p);
26332 *bufp = pbuf;
26333 *fnamep = p;
26334
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026335 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026336}
26337#endif /* WIN3264 */
26338
26339/*
26340 * Adjust a filename, according to a string of modifiers.
26341 * *fnamep must be NUL terminated when called. When returning, the length is
26342 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026343 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026344 * When there is an error, *fnamep is set to NULL.
26345 */
26346 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026347modify_fname(
26348 char_u *src, /* string with modifiers */
26349 int *usedlen, /* characters after src that are used */
26350 char_u **fnamep, /* file name so far */
26351 char_u **bufp, /* buffer for allocated file name or NULL */
26352 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026353{
26354 int valid = 0;
26355 char_u *tail;
26356 char_u *s, *p, *pbuf;
26357 char_u dirname[MAXPATHL];
26358 int c;
26359 int has_fullname = 0;
26360#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026361 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026362 int has_shortname = 0;
26363#endif
26364
26365repeat:
26366 /* ":p" - full path/file_name */
26367 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26368 {
26369 has_fullname = 1;
26370
26371 valid |= VALID_PATH;
26372 *usedlen += 2;
26373
26374 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26375 if ((*fnamep)[0] == '~'
26376#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26377 && ((*fnamep)[1] == '/'
26378# ifdef BACKSLASH_IN_FILENAME
26379 || (*fnamep)[1] == '\\'
26380# endif
26381 || (*fnamep)[1] == NUL)
26382
26383#endif
26384 )
26385 {
26386 *fnamep = expand_env_save(*fnamep);
26387 vim_free(*bufp); /* free any allocated file name */
26388 *bufp = *fnamep;
26389 if (*fnamep == NULL)
26390 return -1;
26391 }
26392
26393 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026394 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026395 {
26396 if (vim_ispathsep(*p)
26397 && p[1] == '.'
26398 && (p[2] == NUL
26399 || vim_ispathsep(p[2])
26400 || (p[2] == '.'
26401 && (p[3] == NUL || vim_ispathsep(p[3])))))
26402 break;
26403 }
26404
26405 /* FullName_save() is slow, don't use it when not needed. */
26406 if (*p != NUL || !vim_isAbsName(*fnamep))
26407 {
26408 *fnamep = FullName_save(*fnamep, *p != NUL);
26409 vim_free(*bufp); /* free any allocated file name */
26410 *bufp = *fnamep;
26411 if (*fnamep == NULL)
26412 return -1;
26413 }
26414
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026415#ifdef WIN3264
26416# if _WIN32_WINNT >= 0x0500
26417 if (vim_strchr(*fnamep, '~') != NULL)
26418 {
26419 /* Expand 8.3 filename to full path. Needed to make sure the same
26420 * file does not have two different names.
26421 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26422 p = alloc(_MAX_PATH + 1);
26423 if (p != NULL)
26424 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026425 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026426 {
26427 vim_free(*bufp);
26428 *bufp = *fnamep = p;
26429 }
26430 else
26431 vim_free(p);
26432 }
26433 }
26434# endif
26435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026436 /* Append a path separator to a directory. */
26437 if (mch_isdir(*fnamep))
26438 {
26439 /* Make room for one or two extra characters. */
26440 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26441 vim_free(*bufp); /* free any allocated file name */
26442 *bufp = *fnamep;
26443 if (*fnamep == NULL)
26444 return -1;
26445 add_pathsep(*fnamep);
26446 }
26447 }
26448
26449 /* ":." - path relative to the current directory */
26450 /* ":~" - path relative to the home directory */
26451 /* ":8" - shortname path - postponed till after */
26452 while (src[*usedlen] == ':'
26453 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26454 {
26455 *usedlen += 2;
26456 if (c == '8')
26457 {
26458#ifdef WIN3264
26459 has_shortname = 1; /* Postpone this. */
26460#endif
26461 continue;
26462 }
26463 pbuf = NULL;
26464 /* Need full path first (use expand_env() to remove a "~/") */
26465 if (!has_fullname)
26466 {
26467 if (c == '.' && **fnamep == '~')
26468 p = pbuf = expand_env_save(*fnamep);
26469 else
26470 p = pbuf = FullName_save(*fnamep, FALSE);
26471 }
26472 else
26473 p = *fnamep;
26474
26475 has_fullname = 0;
26476
26477 if (p != NULL)
26478 {
26479 if (c == '.')
26480 {
26481 mch_dirname(dirname, MAXPATHL);
26482 s = shorten_fname(p, dirname);
26483 if (s != NULL)
26484 {
26485 *fnamep = s;
26486 if (pbuf != NULL)
26487 {
26488 vim_free(*bufp); /* free any allocated file name */
26489 *bufp = pbuf;
26490 pbuf = NULL;
26491 }
26492 }
26493 }
26494 else
26495 {
26496 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26497 /* Only replace it when it starts with '~' */
26498 if (*dirname == '~')
26499 {
26500 s = vim_strsave(dirname);
26501 if (s != NULL)
26502 {
26503 *fnamep = s;
26504 vim_free(*bufp);
26505 *bufp = s;
26506 }
26507 }
26508 }
26509 vim_free(pbuf);
26510 }
26511 }
26512
26513 tail = gettail(*fnamep);
26514 *fnamelen = (int)STRLEN(*fnamep);
26515
26516 /* ":h" - head, remove "/file_name", can be repeated */
26517 /* Don't remove the first "/" or "c:\" */
26518 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26519 {
26520 valid |= VALID_HEAD;
26521 *usedlen += 2;
26522 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026523 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026524 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026525 *fnamelen = (int)(tail - *fnamep);
26526#ifdef VMS
26527 if (*fnamelen > 0)
26528 *fnamelen += 1; /* the path separator is part of the path */
26529#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026530 if (*fnamelen == 0)
26531 {
26532 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26533 p = vim_strsave((char_u *)".");
26534 if (p == NULL)
26535 return -1;
26536 vim_free(*bufp);
26537 *bufp = *fnamep = tail = p;
26538 *fnamelen = 1;
26539 }
26540 else
26541 {
26542 while (tail > s && !after_pathsep(s, tail))
26543 mb_ptr_back(*fnamep, tail);
26544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026545 }
26546
26547 /* ":8" - shortname */
26548 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26549 {
26550 *usedlen += 2;
26551#ifdef WIN3264
26552 has_shortname = 1;
26553#endif
26554 }
26555
26556#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026557 /*
26558 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026559 */
26560 if (has_shortname)
26561 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026562 /* Copy the string if it is shortened by :h and when it wasn't copied
26563 * yet, because we are going to change it in place. Avoids changing
26564 * the buffer name for "%:8". */
26565 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026566 {
26567 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026568 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026569 return -1;
26570 vim_free(*bufp);
26571 *bufp = *fnamep = p;
26572 }
26573
26574 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026575 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026576 if (!has_fullname && !vim_isAbsName(*fnamep))
26577 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026578 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026579 return -1;
26580 }
26581 else
26582 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026583 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026584
Bram Moolenaardc935552011-08-17 15:23:23 +020026585 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026586 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026587 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026588 return -1;
26589
26590 if (l == 0)
26591 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026592 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026593 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026594 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026595 return -1;
26596 }
26597 *fnamelen = l;
26598 }
26599 }
26600#endif /* WIN3264 */
26601
26602 /* ":t" - tail, just the basename */
26603 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26604 {
26605 *usedlen += 2;
26606 *fnamelen -= (int)(tail - *fnamep);
26607 *fnamep = tail;
26608 }
26609
26610 /* ":e" - extension, can be repeated */
26611 /* ":r" - root, without extension, can be repeated */
26612 while (src[*usedlen] == ':'
26613 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26614 {
26615 /* find a '.' in the tail:
26616 * - for second :e: before the current fname
26617 * - otherwise: The last '.'
26618 */
26619 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26620 s = *fnamep - 2;
26621 else
26622 s = *fnamep + *fnamelen - 1;
26623 for ( ; s > tail; --s)
26624 if (s[0] == '.')
26625 break;
26626 if (src[*usedlen + 1] == 'e') /* :e */
26627 {
26628 if (s > tail)
26629 {
26630 *fnamelen += (int)(*fnamep - (s + 1));
26631 *fnamep = s + 1;
26632#ifdef VMS
26633 /* cut version from the extension */
26634 s = *fnamep + *fnamelen - 1;
26635 for ( ; s > *fnamep; --s)
26636 if (s[0] == ';')
26637 break;
26638 if (s > *fnamep)
26639 *fnamelen = s - *fnamep;
26640#endif
26641 }
26642 else if (*fnamep <= tail)
26643 *fnamelen = 0;
26644 }
26645 else /* :r */
26646 {
26647 if (s > tail) /* remove one extension */
26648 *fnamelen = (int)(s - *fnamep);
26649 }
26650 *usedlen += 2;
26651 }
26652
26653 /* ":s?pat?foo?" - substitute */
26654 /* ":gs?pat?foo?" - global substitute */
26655 if (src[*usedlen] == ':'
26656 && (src[*usedlen + 1] == 's'
26657 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26658 {
26659 char_u *str;
26660 char_u *pat;
26661 char_u *sub;
26662 int sep;
26663 char_u *flags;
26664 int didit = FALSE;
26665
26666 flags = (char_u *)"";
26667 s = src + *usedlen + 2;
26668 if (src[*usedlen + 1] == 'g')
26669 {
26670 flags = (char_u *)"g";
26671 ++s;
26672 }
26673
26674 sep = *s++;
26675 if (sep)
26676 {
26677 /* find end of pattern */
26678 p = vim_strchr(s, sep);
26679 if (p != NULL)
26680 {
26681 pat = vim_strnsave(s, (int)(p - s));
26682 if (pat != NULL)
26683 {
26684 s = p + 1;
26685 /* find end of substitution */
26686 p = vim_strchr(s, sep);
26687 if (p != NULL)
26688 {
26689 sub = vim_strnsave(s, (int)(p - s));
26690 str = vim_strnsave(*fnamep, *fnamelen);
26691 if (sub != NULL && str != NULL)
26692 {
26693 *usedlen = (int)(p + 1 - src);
26694 s = do_string_sub(str, pat, sub, flags);
26695 if (s != NULL)
26696 {
26697 *fnamep = s;
26698 *fnamelen = (int)STRLEN(s);
26699 vim_free(*bufp);
26700 *bufp = s;
26701 didit = TRUE;
26702 }
26703 }
26704 vim_free(sub);
26705 vim_free(str);
26706 }
26707 vim_free(pat);
26708 }
26709 }
26710 /* after using ":s", repeat all the modifiers */
26711 if (didit)
26712 goto repeat;
26713 }
26714 }
26715
Bram Moolenaar26df0922014-02-23 23:39:13 +010026716 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26717 {
26718 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26719 if (p == NULL)
26720 return -1;
26721 vim_free(*bufp);
26722 *bufp = *fnamep = p;
26723 *fnamelen = (int)STRLEN(p);
26724 *usedlen += 2;
26725 }
26726
Bram Moolenaar071d4272004-06-13 20:20:40 +000026727 return valid;
26728}
26729
26730/*
26731 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26732 * "flags" can be "g" to do a global substitute.
26733 * Returns an allocated string, NULL for error.
26734 */
26735 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026736do_string_sub(
26737 char_u *str,
26738 char_u *pat,
26739 char_u *sub,
26740 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026741{
26742 int sublen;
26743 regmatch_T regmatch;
26744 int i;
26745 int do_all;
26746 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026747 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026748 garray_T ga;
26749 char_u *ret;
26750 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026751 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026752
26753 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26754 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026755 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026756
26757 ga_init2(&ga, 1, 200);
26758
26759 do_all = (flags[0] == 'g');
26760
26761 regmatch.rm_ic = p_ic;
26762 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26763 if (regmatch.regprog != NULL)
26764 {
26765 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026766 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026767 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26768 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026769 /* Skip empty match except for first match. */
26770 if (regmatch.startp[0] == regmatch.endp[0])
26771 {
26772 if (zero_width == regmatch.startp[0])
26773 {
26774 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026775 i = MB_PTR2LEN(tail);
26776 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26777 (size_t)i);
26778 ga.ga_len += i;
26779 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026780 continue;
26781 }
26782 zero_width = regmatch.startp[0];
26783 }
26784
Bram Moolenaar071d4272004-06-13 20:20:40 +000026785 /*
26786 * Get some space for a temporary buffer to do the substitution
26787 * into. It will contain:
26788 * - The text up to where the match is.
26789 * - The substituted text.
26790 * - The text after the match.
26791 */
26792 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026793 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026794 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26795 {
26796 ga_clear(&ga);
26797 break;
26798 }
26799
26800 /* copy the text up to where the match is */
26801 i = (int)(regmatch.startp[0] - tail);
26802 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26803 /* add the substituted text */
26804 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26805 + ga.ga_len + i, TRUE, TRUE, FALSE);
26806 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026807 tail = regmatch.endp[0];
26808 if (*tail == NUL)
26809 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026810 if (!do_all)
26811 break;
26812 }
26813
26814 if (ga.ga_data != NULL)
26815 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26816
Bram Moolenaar473de612013-06-08 18:19:48 +020026817 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026818 }
26819
26820 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26821 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026822 if (p_cpo == empty_option)
26823 p_cpo = save_cpo;
26824 else
26825 /* Darn, evaluating {sub} expression changed the value. */
26826 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026827
26828 return ret;
26829}
26830
26831#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */